Wednesday, May 6, 2009

PROGRAM TO CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION

/* THIS IS A PROGRAM TO CONVERT INFIX EXPRESSION TO POSTFIX EXPRESSION */

#include
#include
#include
#include
int top=-1;
char s[20];

void push(char c) // STARTING OF PUSH
{
top++;
s[top]=c;
} // END OF PUSH FUNCTION
void pop(char c) //STARTING OF POP
{
if(c==')')
{
while(s[top]!='(')
{
printf("%c",s[top]);
top--;
}
}
else
{ if(s[top]=='(')
top--;
else
{
printf("%c",s[top]);
top--;
}
}
} // END OF POP FUNCTION
void priority(char c) // FUNCTION TO CHECK PRIORITY
{
int l=change1(c); // THE CHARACTER THAT IS INCOMMING
int m=change1(s[top]);
if(top==-1)
push(c);
else
{
if(l>m)
push(c);
else
{
if(m==4)
push(c);
else
{
pop(s[top]);
push(c);
}
}
}
} // END OF PRIORITY FUNCTION

int change1(char c) // FUNCTION TO CHANGE CHARACTERS TO NUMERICALS
{
int a;
a=(c=='('?4:c=='^'?3:c=='*'?2:c=='/'?2:c=='+'?1:c=='-'?1:-1);
return a;
}
int change2(char c) // ANOTHER FUNCTION TO CHANGE CHARACTERS TO NUMERICALS
{
int a;
if(c=='(')
a=0;
else
{
if(c==')')
a=2;
else a=1;
}
return a;
}
void main() // STARTING OF MAIN
{
char exp[20];
int i=0,j;
clrscr();
printf("Enter an infix expression\n");
// getc(exp);
scanf("%s",&exp);
printf("The postfix expression for %s is ",exp);
while(exp[i]!='\0')
{
if(isalpha(exp[i]))
printf("%c",exp[i]);
else
{
j=change2(exp[i]);
switch(j)
{
case 0:push(exp[i]);
break;
case 2:pop(exp[i]);
break;
case 1:priority(exp[i]);
break;
}
}
i++;
}
if(exp[i]=='\0')
{
while(top!=-1)
pop(s[top]);
}
getch();
} // END OF MAIN

PROGRAM FOR SORTING THE GIVEN STRINGS

/* take a set of strings as input from command line and
load them into an array of strings and print
the sorted list of strings.
*/
#include
#include
#define BUFSIZE 64
char *strings[BUFSIZE]; /* array of pointers to hold strings */
main()
{
char temp[BUFSIZE]; /* temporary string to hold user input */
int i=0; /*index into the strings list */
/* this loop runs till user inputs END */
/*initialize the strings to nulls */
for(i=0; istrings[i]=NULL;
i=0;
while(1)
{
/* zerofill temp to avoid any garbage later */
memset(temp, 0, BUFSIZE);
/* ask the user to give string input */
printf("enter a string. enter END to stop input:");
/* take user input into temp */
scanf("%s", temp);
/* compare user input with END.
if
equal to zero exit the loop and go to sort
else
get memory (size=BUFSIZE) to the next string pointer
load the string into the memory
*/
if(!strcmp(temp, "END"))
{
break;
}
if(NULL==(strings[i]=(char *)malloc(BUFSIZE)))
{
/* memory allocation failed */
perror("malloc():");
exit(0);
}
/* copy the string into strings list */
strcpy(strings[i], temp);
i++;
} /* while */
/* strings list */
//for(i=0; strings[i]!=NULL; i++)
// printf("%s\n", strings[i]);
/* now that we have got our list. Need to sort it
*/
for(i=0; strings[i]!=NULL; i++)
{
int j;
char *p; /* temp pointer */
for(j=i+1; strings[j]!=NULL; j++)
{
// printf("%s\n %s\n", strings[i], strings[j]);
if(strcmp(strings[i], strings[j])>0)
{
p=strings[j];
strings[j]=strings[i];
strings[i]=p;
}
}
} /* for */
printf("sorted list is .............\n");
/* print the ordered list */
for(i=0; strings[i]!=NULL; i++)
{
printf("%s\n", strings[i]);
}
}

C PROGRAM FOR GREP

#include
#include
#define BUFSIZE 256
#define FAILURE -1
#define SUCCESS 0
int
main(int argc, char *argv[])
{
void search_pattern(char *buf, char *pattern);
char buf[BUFSIZE];
char pattern[BUFSIZE];
char file_name[BUFSIZE];
FILE *fp;
/*get the command line arguments into local variables */
memset(pattern, 0, BUFSIZE);
memset(buf, 0, BUFSIZE);
memset(file_name, 0, BUFSIZE);
strcpy(pattern, argv[1]);
strcpy(file_name, argv[2]);
/* open the file in read mode */
fp=(FILE *)fopen(file_name, "r");
if(fp==NULL)
{
/* return in case of failure */
perror("fopen():");
return FAILURE;
}
/* read one line from the file till end of file is reached */
while(fgets(buf, BUFSIZE, fp)!=NULL)
{
search_pattern(buf, pattern);
}
fclose(fp);
return SUCCESS;
}
void
search_pattern(char *buf, char *pattern)
{
char *p, *q;
for(p=buf, q=pattern; *p!='\0'; p++)
{
if(*p != *q)
continue;
else
{
for( ; *p==*q; p++, q++);
if(*q=='\0')
{
/* pattern found. print it */
printf("%s\n", buf);
return;
}
q=pattern;
}
}
getch();
}

PROGRAM TO PRINT STARS IN THE FORM OF RHOMBUS

#include

main()
{
int i,j,k,n,a,b,c,x;
printf("enter the # of rows of graphical output");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\t");
for (k=1;k<=(n-i);k++)
{ printf(" ");
}
for(j=0;j>0;i--)
{
printf("\t");
for (k=(n-i);k>0;k--)
{
printf(" ");
}
for(j=i;j>0;j--)
{
printf("*");
printf(" ");
}
for(k=(n-i-1);k>0;k--)
{
printf(" ");
}
printf("\n");
}

PROGRAM TO PRINT MULTIPLICATION TABLE

#include
main()

{
int i,j,r,c;
printf("enter the # of rows of *table");
scanf("%d",&r);
printf("enter the # of columns of *table");
scanf("%d",&c);
printf("\t\t\t MULTIPLICATION TABLE\n");
printf("-------------------------------------------------------------------------------\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%5d",i*j);
}
printf("\n\n");
}
printf("________________________________________________________________________________\n");
}

PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER

#include
main()
{
int i,j,k,fact=1;
printf("\n\nenter a number");
scanf("%d",&i);
for(j=1;j<=i;j++)
{
for(k=j;k>0;k--)
{
fact=fact*k;
}
printf("%d\t %4d\n",j,fact);
fact=1;
}
getch();
}

PROGRAM TO GENERATE PASCAL TRIANGLE

#include
#include
void value(int n)
{
int i,j,val;
for(i=1;i<=n;i++) { for(j=0;j<=i;j++) { val= fact(i)/(fact(i-j)*fact(j)); printf("%d",val ); } printf("\n"); } } int fact(int k) { int mul=1; for(;k>0;k--)
{
mul*=k;
}
return mul;
}
void main()
{
int n;
clrscr();
printf("Enter the #of rows of graphical output\n");
scanf("%d",&n);
value(n);
getch();
}