This Proram makes an Infix expression to its Postfix form.
Infix to postfix
- #include<stdio.h>
- #include<string.h>
- #define size 10
- struct stack
- {
- char stkel[size];
- int tos;
- };
- struct stack stk;
- int ele;
- char infix[50],output[50];
- void push(struct stack *ps,char ele);
- char pop(struct stack *ps);
- void show_stack(struct stack *ps);
- void show_output(char output[]);
- int prec(char);
- int main()
- {
- int i=0,j=0,k=0,length;
- char temp;
- struct stack *ps;
- FILE *fp;
- fp=fopen("data1.txt","r");
- if(fp==NULL)
- {
- printf("\nError Openning File\n");
- exit(1);
- }
- else
- printf("Data from the file data1.txt has been taken.\n");
- ps=&stk;
- ps->tos=-1;
- while(!feof(fp))
- {
- ps->tos=-1;j=0;
- fscanf(fp,"%s\n",infix);
- printf("\nThe infix expresson is %s\n",infix);
- length=strlen(infix);
- printf("\nInp\t\tPostfx\t\t\tStk\n");
- for(i=0;i<length;i++)
- {
- printf("%c\t\t",infix[i]);//Input string Printed
- //Numbers are added to the out put string
- if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!='$' && infix[i]!=')' && infix[i]!='(' )
- {
- output[j]=infix[i];
- j++;
- }
- //If an operator or a bracket is encountered
- else
- {
- if(ps->tos==-1) //If there are no elements in the stack,the operator added to stack
- {
- push(ps,infix[i]);
- }
- else
- { //Operators or pushed or poped based on the precedence
- if(infix[i]!=')' && infix[i]!='(')
- {
- if(prec(infix[i]) <= prec(ps->stkel[(ps->tos)]))
- {
- temp=pop(ps);
- output[j]=temp;
- j++;
- push(ps,infix[i]);
- }
- else
- {
- push(ps,infix[i]);
- }
- }
- else //if bracket occurs
- {
- if(infix[i]=='(')
- {
- push(ps,infix[i]);
- }
- if(infix[i]==')')
- {
- temp=pop(ps);
- while(temp!='(')
- {
- output[j]=temp;
- j++;
- temp=pop(ps);
- }
- }
- }
- }
- }
- output[j]='\0';
- show_output(output);
- printf("\t\t\t");
- show_stack(ps);
- }
- //rest of the elemets of stack added to postfix string
- while(ps->tos!=-1)
- {
- output[j++]=pop(ps);
- output[j]='\0';
- printf("\t\t");
- show_output(output);
- printf("\t\t\t");
- show_stack(ps);
- }
- printf("\nThe postfix expression is\t");
- for(i=0;i<length;i++)
- printf("%c",output[i]);
- printf("\n");
- }
- return 0;
- }
- //Functions for operations on stack
- void push(struct stack *ps,char ele)
- {
- ps->tos++;
- ps->stkel[ps->tos]=ele;
- }
- char pop(struct stack *ps)
- {
- char var;
- var=ps->stkel[ps->tos];
- ps->tos--;
- return var;
- }
- //showing postfix string
- void show_output(char output[])
- {
- int i;
- for(i=0;output[i]!='\0';i++)
- printf("%c",output[i]);
- }
- //showing the elements of stack
- void show_stack(struct stack *ps)
- {
- int x=0;
- if(ps->tos>-1)
- while(x<=ps->tos)
- {
- printf("%c",ps->stkel[x]);
- x++;
- }
- printf("\n");
- }
- //Function to get the precedence of an operator
- int prec(char symbol)
- {
- if(symbol== '(')
- return 0;
- if(symbol== ')')
- return 0;
- if(symbol=='+' || symbol=='-')
- return 1;
- if(symbol=='*' || symbol=='/')
- return 2;
- if(symbol=='^')
- return 3;
- //return 0;
- }
0 Responses to Infix to Postfix – C Programming And Algorithom