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

We normally write arithmetical expressions using infix notation, meaning that th

ID: 3765117 • Letter: W

Question

We normally write arithmetical expressions using infix notation, meaning that the operator appears between its two operands, as in "4 + 5". In postfix notation, the operator appears after its operands, as in "4.3 5.0 +". Here is a slightly more complex postfix expression: "25 12 7 - 2 * /". The equivalent infix expression is: "25 / ((12 - 7) * 2)". The result of that expression should be 2.5 (beware integer division). Postfix expressions don't require parentheses.

Write a function named postfixEval that uses a stack (from the Standard Template Library) to evaluate postfix expressions. It should take a C-style string parameter that represents a postfix expression. The only symbols in the string will be +, -, *, /, digits and spaces. '+' and '-' will only appear in the expression string as binary operators - not as unary operators that indicate the sign of a number. The return type should be double. You may find the isdigit() function useful in parsing the expression. You may also use strtok() and atof(). Hint: Read a postfix expression from left to right. When you read a number, push it on the stack. When you read an operand, pop the top two numbers off the stack, apply the operator to them, and push the result on top of the stack. At the end, the result of the expression should be the only number on the stack.

File must be called: postfixEval.cpp

Explanation / Answer

/*EVALUATION OF POSTFIX EXPRESSION*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<ctype.h>

#define max 20

void push(char);

float pop(void);

float eval(char[],float[]);

int i,j,top=-1;

float stack[max];

char postfix[max];

void main()

{

float value[max],result;

clrscr();

printf(" enter post fix expresion:");

gets(postfix);

while(postfix[i]!='')

{

if(isalpha(postfix[i]))

{

   fflush(stdin);

   printf("enter value of %c",postfix[i]);

   scanf("%f",&value[i]);

}

i++;

}

result=eval(postfix,value);

printf(" result of postfix %s = %f",postfix,result);

getch();

}

float eval(char postfix[],float data[])

{

float op1,op2,res;

char ch;

while((postfix[i]!=''))

{

ch=postfix[j];

if(isalpha(postfix[j]))

push(data[j]);

else

{

   op2=pop();

   op1=pop();

   switch(ch)

   {

    case '+': push(op1+op2);break;

    case '-': push(op1-op2);break;

    case '*': push(op1*op2);break;

    case '/': push(op1/op2);break;

    case '^': push(pow(op1,op2));break;

   }

}

j++;

}

res=pop();

return res;

}

void push(char num)

{

top=top+1;

stack[top]=num;

}

float pop()

{

int no;

no=stack[top];

top=top-1;

return no;

}

OUTPUT:

enter post fix expresion:ABCD^+*EF^GH/*-

enter value of A 4

enter value of B 5

enter value of C 4

enter value of D 2

enter value of E 2

enter value of F 2

enter value of G 9

enter value of H 3

result of postfix ABCD^+*EF^GH/*- = 72.000000

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