Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to convert infix artihmetic expressions to postfix and evalu

ID: 649588 • Letter: W

Question

Write a C++ program to convert infix artihmetic expressions to postfix and evaluate the expression using its postfix notation.

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 ? 78 / 3 is a valid infix expression //But ( 34 + 23) * 8 ? 78 / 3 is not valid since no space between 23 and ) //Print out the string after it was read for verification. 2. Convert infix to postfix notation //Print the expression in postfix notation with a space between //either two operands or an operand and an operator 3. Evaluate the expression using postfix notation. // Print the result of the evaluation of the expression // using its postfix notation // Note the division operation here assumes it is the integer division. 4. Exit Your program should apply operations described in each option according to the user choice, then go back to the main menu unless user chooses option 4 to quit the program.

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;

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote