Write a C++ program to convert infix arithmetic expressions to postfix and evalu
ID: 649389 • Letter: W
Question
Write a C++ program to convert infix arithmetic expressions to postfix and evaluate the expression using its postfix notation. Your main function should display the following menu repeatedly:
1. Read an expression in infix notation. //Assume that input strings are infix arithmetic expressions //with or without parentheses. //Also assume that +, -, *, / are the only operators used //and the operands are positive integers consisting of one or more digits. //The expression uses space to separate each operator and operand. //For example, ( 34 + 23 ) * 8
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<iostream>
int main()
{
char ch ,co;
char post[20].stac[20],inf[20];
int i,j,count=-1,count=-1,op1,op2;
cout<<"enter your choice";
cin<ch;
switch(ch)
{
case "A":
cout<<"Reading Expression ";
gets(inf);
count<<"the Enter Expression is "<<inf<<endln;
break;
case "B":
cout<<"Express Convertion inot Postfix";
for(i=0;inf[i]!="";i++)
{
if(inf[i]=='(' || inf[i]=='+' || inf[i]=='-' || inf[i]=='*' || inf[i]=='/' || inf[i]==')')
{
countop++ ;
stack[countop]=inf[i];
}
}
else
{
if(inf[i]== ')')
{
while(stack[countop] != '(')
count++;
post[count]=stack[countop];
countop--;
}
countop--;
}
else
{
count++;
post[count]=inf[i];
}
}
while(countop !=-1)
{
count++;
post[count]=stack[countop];
count--;
}
coun<<"the Postfix Expression"<<endln;
for(i=0;i<=count;i++)
count<<post[i];
getch();
}
}
break;
case "C":
cout<<"Evalute the using postfix ";
oper(op1,op2,ch)
int s[max];
if(isalpha(co)
{
count<<:post[i];
}
else
{
if(ch=='*' || co =='+' || co=='-' || co=='/')
{
op2=pop();
op1=pop();
res=operate(op1,op2,co);
push(res);
}
}
i++;
}
int pop()
{
return(post[i];
}
int oper(int op1,int po2,charv co)
{
switch(co)
{
case '+':temp=po1+op2 break;
case '-':temp=po1-op2 break;
case '*':temp=po1+op2 break;
case '/':temp=po1+op2 break;
}
return(temp);
]
void push()
{
int val;
countop++;
post[i]=val;
}
case "D":
exit 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.