Below is the code I\'ve written to evaluate user-entered Postfix expressions. Ho
ID: 3624268 • Letter: B
Question
Below is the code I've written to evaluate user-entered Postfix expressions. However, the assignment requires that the program prompt the user to enter the Postfix expression using variables first, then, after parsing the expression, prompts the user to enter the values of the variables.For example, if ABC-* is entered, the program would then prompt the user to enter values for A, B, and C.
I will only rate Lifesaver for working code with explanation.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stackadt.h"
/* function prototypes */
bool isOperator( char token );
int calc( int operand1, int oper, int operand2 );
int main(void)
{
/* variable declarations */
char token;
int operand1;
int operand2;
int value;
int* dataPtr;
STACK* stack;
/* create stack */
stack = createStack();
/* read postfix expression one character at a time */
printf( "Input postfix formula: " );
while (( token = getchar()) != ' ' )
{
if ( !isOperator( token ))
{
dataPtr = (int*)malloc (sizeof(int));
*dataPtr = atoi( &token );
pushStack( stack, dataPtr );
} /* end if */
else
{
dataPtr = (int*)popStack( stack );
operand2 = *dataPtr;
dataPtr = (int*)popStack( stack );
operand1 = *dataPtr;
value = calc( operand1, token, operand2 );
dataPtr = (int*)malloc(sizeof(int));
*dataPtr = value;
pushStack( stack, dataPtr );
} /* end else */
} /* end while */
/* pop and print the stack */
dataPtr = (int*)popStack( stack );
value = *dataPtr;
printf( "The result is: %d ", value );
/* destroy the stack */
destroyStack( stack );
return 0;
} /* end main */
/* function to determine whether a character is an operator */
bool isOperator( char token )
{
if ( token == '*'
|| token == '/'
|| token == '+'
|| token == '-' )
return true;
else
{
return false;
}
} /* end isOperator */
/* function to determine value of two values and an operator */
int calc( int operand1, int oper, int operand2 )
{
int result;
switch (oper)
{
case '+' : result = operand1 + operand2;
break;
case '-' : result = operand1 - operand2;
break;
case '*' : result = operand1 * operand2;
break;
case '/' : result = operand1 / operand2;
break;
} /* end switch */
return result;
} /* end calc */
Explanation / Answer
#include < stdio.h >
/*program to evaluate the given postfix expression*/
typedef struct
{
int a[100];
int top;
}STACK;
void push(STACK *s,int x)
{
if(s->top==99)
printf("STACK OVERFLOW ");
else
s->a[++s->top]=x;
}
int pop(STACK *s)
{
int x;
if(s->top<0)
printf("STACK UNDERFLOW ");
else
{
x=s->a[s->top--];
return x;
}
}
int operation(int p1,int p2,char op)
{
switch(op)
{
case '+':return p1+p2;
case '*':return p1*p2;
case '-':return p1-p2;
case '/':return p1/p2;
}
}
int evaluate(char pos[])
{
STACK s1;
int p1,p2,result,i;
s1.top=-1;
for(i=0;pos[i]!='';i++)
if(isdigit(pos[i]))
push(&s1,pos[i]-'0');/*use to find the integer value of it*/
else
{
p2=pop(&s1);
p1=pop(&s1);
result=operation(p1,p2,pos[i]);
push(&s1,result);
}/*end of for loop*/
return pop(&s1);
}
void main()
{
char postfix[100];
//clrscr();
printf("Please Enter the VALID POSTFIX string Operands are SINGLE DIGIT ");
gets(postfix);
printf("The Result is==>%d",evaluate(postfix));
system("pause");
}/*end of main*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.