Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Program for Transforming infix expression to postfix expression with Algorithm.
- Successfully Tested.*/
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int stack[20], TOP=-1;
- void push(char e)
- {
- stack[TOP++]=e;
- }
- int pop(){
- if(TOP==-1)
- return -1;
- else
- return stack[TOP--];
- }
- int highPrecedence(char operater)
- {
- if(operater=='^')
- return 3;
- else if(operater=='*'||operater=='/')
- return 2;
- else if(operater=='+'||operater=='-')
- return 1;
- else
- return 0;
- }
- //Main fuunction execution starts here.
- int main()
- {
- char expression[20],x,*e;
- e=expression;
- printf("Enter Expression : ");
- scanf("%s",e); printf("%s",e);
- /*Step1 : Push left parenthesis '(' on stack[TOP] and append right parenthesis at the end of expression. */
- push('(');
- strcat(e,")");
- /*Step2 : Scan each element of an expression till null character not found. */
- while(*e!='\0'){
- /*Step3 : If an operand is encountered the print it.*/
- if(isalnum(*e))
- printf("%c",*e);
- if(*e=='(')
- push(*e);
- if(*e==')'){
- while((x=pop())!='(')
- printf("%c",x); }
- else{
- while(highPrecedence(stack[TOP])>=highPrecedence(*e))
- printf("%c",pop());
- push(*e);
- }
- e++;
- }
- }
Add Comment
Please, Sign In to add comment