Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //program to infix to post fix
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- char infix[50],postfix[50],stack[50];
- int top=-1;
- void push(char ch)
- {
- top++;
- stack[top]=ch;
- }
- char pop()
- {
- char ch;
- ch=stack[top];
- top--;
- return ch;
- }
- int main()
- {
- int i,j=0;
- clrscr();
- printf("\nENter the Infix EXPRESSION: ");
- gets(infix);
- for(i=0;i<strlen(infix);i++)
- {
- switch(infix[i])
- {
- case '[':
- push('[');
- break;
- case ']':
- while(stack[top]!='[')
- {
- postfix[j]=pop();
- j++;
- }
- top--;
- break;
- case '(':
- push('(');
- break;
- case ')':
- while(stack[top]!='(')
- {
- postfix[j]=pop();
- j++;
- }
- top--;
- break;
- case '^':
- while(stack[top]=='^')
- {
- postfix[j]=pop();
- j++;
- }
- push(infix[i]);
- break;
- case '*':
- case '/':
- while(stack[top]=='^' || stack[top]=='*' || stack[top]=='/')
- {
- postfix[j]=pop();
- j++;
- }
- push(infix[i]);
- break;
- case '+':
- case '-':
- while(stack[top]=='^' || stack[top]=='*' || stack[top]=='/'|| stack[top]=='-'|| stack[top]=='+')
- {
- postfix[j]=pop();
- j++;
- }
- push(infix[i]);
- break;
- default:
- postfix[j]=infix[i];
- j++;
- }
- }while(top!=-1)
- {
- postfix[j]=pop();
- j++;
- }
- postfix[j]=NULL;
- printf("\n %s <--- INFIX TO POSTFIX ---> %s",infix,postfix);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement