Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #define MAX 20
- struct queue {
- char stack[MAX];
- int top;
- }q;
- char pop();
- void push(char item);
- int prcd(char symbol)
- {
- switch(symbol)
- {
- case '+':
- case '-':return 2;
- break;
- case '*':
- case '/':return 4;
- break;
- case '^':
- return 6;
- break;
- case '(':
- case ')':
- case '#':return 1;
- break;
- }
- }
- int isoperator(char symbol)
- {
- switch(symbol)
- {
- case '+':
- case '-':
- case '*':
- case '/':
- case '^':
- case '(':
- case ')':return 1;
- break;
- default:return 0;
- }
- }
- void convertip(char infix[],char postfix[])
- {
- int i,symbol,j=0;
- q.stack[q.top=q.top+1]='#';
- for(i=0;i<strlen(infix);i++)
- {
- symbol=infix[i];
- if(isoperator(symbol)==0)
- {
- postfix[j]=symbol;
- j++;
- }
- else{
- if(symbol=='(')push(symbol);
- else if(symbol==')')
- {
- while(q.stack[q.top]!='(')
- {
- postfix[j]=pop();
- j++;
- }
- pop();
- }
- else{
- if(prcd(symbol)>prcd(q.stack[q.top]))
- push(symbol);
- else{
- while(prcd(symbol)<=prcd(q.stack[q.top]))
- {
- postfix[j]=pop();
- j++;
- }
- push(symbol);
- }
- }
- }
- }
- while(q.stack[q.top]!='#')
- {
- postfix[j]=pop();
- j++;
- }
- postfix[j]='\0';
- }
- void main()
- {
- q.top=-1;
- char infix[20],postfix[20];
- printf("Enter the valid infix string:\n");
- gets(infix);
- convertip(infix,postfix);
- printf("The corresponding postfix string is:\n");
- puts(postfix);
- }
- void push(char item)
- {
- q.top++;
- q.stack[q.top]=item;
- }
- char pop()
- {
- char a;
- a=q.stack[q.top];
- q.top--;
- return a;
- }
Advertisement
Add Comment
Please, Sign In to add comment