Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following C code. Develop a evaluate post fix function that calculates the converted post fix. #include #include #define max 10 char s[100]; int

Given the following C code. Develop a evaluate post fix function that calculates the converted post fix.

#include #include #define max 10 char s[100]; int top=-1; void push(char); char pop(); int precede(char); main(){ char infix[100],postfix[100],ch; int i=0,j=0; // Read infix expression from user printf("Enter infix expression:"); scanf("%s",infix); // evaluate each char in infix expression while((ch=infix[i++])!='\0'){ // if it is number, add it to postfix expression if(isalnum(ch)){ postfix[j++]=ch; }else if(ch=='('){ // if it open paranthesis, push it to stack push(ch); }else if(ch==')'){ // if it is closing paranthesis, pop all the // numbers from stack and add it postfix while(s[top]!='('){ postfix[j++]=pop(); } top--; }else{ /* if if is operator, if stack is empty or precedence of current operator is greater than precedence of top of the stack, then push it into stack. otherwise pop all the operators from stack until precedence of top of the stack is less than precedence of the current operator. */ if(top==-1||precede(ch)>precede(s[top])){ push(ch); }else{ while(precede(ch)<=precede(s[top])&&top!=-1){ postfix[j++]=pop(); } push(ch); } } } while(top!=-1){ postfix[j++]=pop(); } postfix[j]='\0'; printf("Infix expression = %s Postfix expression = %s",infix,postfix); } void push(char a) { s[++top]=a; } char pop(){ return s[top--]; } int precede(char a){ if(a=='('||a==')'){ return 1; }else if(a=='-'||a=='+'){ return 2; }else if(a=='%'||a=='/'||a=='*'){ return 3; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions