Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #define size 100
- char infix [size] , postfix [size] , stack [size];
- int tops = -1,topp = -1,i=0;
- int precedence (char x)
- {
- if (x=='$')return 3;
- else if (x=='/'|| x=='*')return 2;
- else if (x=='-'||x=='+')return 1;
- else return 0;
- }
- int main()
- {
- FILE *fp;
- fp = fopen("074BCT035_LAB1.txt","a");
- printf("Enter Infix Expression \n");
- fprintf(fp,"\n Student Roll Number :074BCT035 \n Infix To Postfix Expression Coversion \n Enter Infix Expression \n");
- scanf("%s",infix);
- for (i = 0 ; infix [i] != '\0' ; i++)
- {
- if (i==0)
- {
- printf("\n Infix Expression Stack Postfix Expression \n");
- fprintf(fp,"%s \n",infix);
- fprintf(fp,"\n Infix Expression Stack Postfix Expression \n");
- }
- if (isalnum(infix [i]))
- {
- topp ++;
- postfix [topp] = infix [i];
- }
- else if ( infix [i] == ')' )
- {
- while (stack [tops] != '('){
- topp ++;
- postfix [topp] = stack [tops];
- stack [tops] = ' ';
- tops --;
- }
- tops --;
- }
- else {
- if(precedence(infix[i])<precedence(stack[tops])){
- topp ++;
- postfix [topp] = stack [tops];
- tops -- ;
- }
- else{
- tops ++;
- stack [tops] = infix [i];
- }}
- printf(" %c %s %s \n",infix [i],stack,postfix);
- fprintf(fp," %c %s %s \n",infix [i],stack,postfix);
- }
- if (tops != -1)
- {
- while(tops!=-1){
- topp ++;
- postfix [topp] = stack [tops];
- tops--;
- }}
- /*for (int d = 0; topp>=d ; d++)
- {
- printf(" %c %s %s \n",postfix [d]);
- } */
- printf(" Empty %s \n",postfix);
- fprintf(fp," Empty %s \n",postfix);
- fprintf(fp,"\n");
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment