Write a program that will convert an infix expression to a postfix expression. T
ID: 3544275 • Letter: W
Question
Write a program that will convert an infix expression to a postfix expression. The program will then evaluate the postfix expression. The program is required to process expression until the user enters a blank expression. A blank expression is an expression that does not contain any operands or operators. The program must use a stack class implemented as a linked structure. The program must be able to handle input of numbers consisting of one or more digits. Inputs:
1. The digits 0...9
2. Left and Right Parenthesis ( )
3. The operators: + - * /
Space
End-of-line character ( )
The maximum length of an infix expression is 70 characters.
The first element of an expression cannot be an operator.
Outputs:
1. The postfix expression with parentheses removed.
2. The original infix expression.
3. Results of the evaluation.
4. If the program detects an error, write the infix expression to the screen with an indicator that directs the user to the location where the error was detected. The user should then be given the chance to correct the error.
Illegal Expression
Enter an Expression => 4.33 + 5(5+3)
4.33 + 5(5+3)
^
Error: Only operator can follow an operand
Legal Expression
Enter an Infix Expression => (15+4)*(2+1)
Postfix Expression is 15 4 + 2 1 + *
Results = 57
Error Conditions:
5.1. A right parentheses is encountered and there does not exist a corresponding left parenthesis.
5.2. The number of left parentheses does not agree with the number of right parentheses.
5.3. Two operands appear in succession.
5.4. Two operators appear in succession.
5.5. An illegal character was encountered in the infix expression.
A left parenthesis appears immediately after an operand.
A right parenthesis appears immediately before an operand.
Explanation / Answer
#include<stdio.h> #include<string.h> #include<math.h>#define oper(x) (x=='+' || x=='-' || x=='*' || x=='/')
char in[30], post[30], stack[30]; int top=-1;
void push(char x) { stack[++top]=x; }
char pop() { return stack[top--]; }
int precedence(char c) { if (c=='+' || c=='-') return 1; if (c=='*' || c=='/') return 2; if (c=='(') return 3; }
main() { char c; int l,i,j=0,st1[20],k,h,f,eval,s,N; printf("Enter the infix expression : "); scanf("%s",&in); l=strlen(in); for(i=0;i<=l;i++) { if(oper(in[i])) { post[j++]=' '; while(precedence(in[i])<precedence(stack[top])) { post[j++]=stack[top]; pop(); post[j++]=' ';
} push(in[i]); } else if(in[i]=='') { while(top!=-1) { post[j++]=' '; post[j++]=stack[top]; pop(); } } else post[j++]=in[i]; } post[j]=''; printf("Postfix Expression : %s ",post); i=0;top=-1;f=0;k=0; while(i<j) { if(oper(post[i])) { f=1; c=post[i]; eval=0; switch(c) { case '+': eval=st1[top-1]+st1[top]; break; case '-': eval=st1[top-1]-st1[top]; break; case '*': eval=st1[top-1]*st1[top]; break; case '/': eval=st1[top-1]/st1[top]; break; } top--; st1[top]=eval; } else if(post[i]==' ') { if(f==0) { h=i-k; s=0; while(post[h]!=' ') { N=(int)post[h]; N=N-48; s=s+N*(pow(10,(k-1))); k--; h++; } st1[++top]=s; } k=0; } else { k++; f=0; } i++; } printf("Value : %d ",st1[top]); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.