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

PROGRAM language in c++ Purpose: Use a stack Background: There are 3 main techni

ID: 3786610 • Letter: P

Question

PROGRAM language in c++

Purpose: Use a stack
Background: There are 3 main techniques for evaluating mathematical expressions:
* prefix
* infix
* postfix
Most math classes teach equation evaluation using the in-fix technique. Some calculators use post-fix evaluations, which internally use a stack. Here is an example:
Infix:   (3+4)*5  
Postfix: 34+5*   
This labs purpose is to use a stack to evaluate an expression. The technique:
1. Read in a string that has an expression using post-fix notation
2. For each character in the string:  
3. If it is a number, push it on the number stack  
4. If it is an operator (like +, -, *, /),   
a. pop two numbers off the stack   
b. perform the operation   
c. push the result on the stack   

Implement a link-list based stack. Read in the Posfix.csv in the documents folder for this week for a list of post-fix expressions to evaluate. If there is an error in the expressions, your program should note the error and then move to the next expression.

Explanation / Answer

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<ctype.h>
using namespace std;
//Declares size of stack
float mystack[20], value[30];
char suffix[20];
//Initial value of top
int top = -1;
//Push operation
void push(char num)
{
//Increases the top position
top++;
//Adds the character at the top position
mystack [top] = num;
}//End of push function

//Pop operation
float pop()
{
float num;
//Extracts data from stack top position
num = mystack[top];
//Decreases the top by 1
top--;
return (num);
}//End of pop function

//Evaluates expression
float evaluate()
{
int c = 0;
char ch;
float op1, op2, res;
//Loops till end of string expression
while(suffix[c] != '')
{
//Extracts a character
ch = suffix[c];
//If it is a variable
if(isalpha(suffix[c]))
//Push it into value array
push(value[c]);
else
{
//Pops the operand
op2 = pop( );
op1 = pop( );
//Checks the operator
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;
} // End of switch
} //End of else
//Increase the counter by 1
c++;
}//End of while
//Extracts the result of evaluated result
res = pop( );
return (res);
}//End of function

int main( )
{
int c = 0;
float result;
//Accept an expression
cout<<" Enter a valid Postfix Expression ";
gets(suffix);
//Loops till end of expression
while(suffix[c] != '')
{
//Checks for the variable
if(isalpha(suffix[c]))
{
fflush(stdin);
//Accepts the value of the variable
cout<<" Enter the values of "<<suffix[c]<<": ";
cin>>value[c];
}
//Increase the counter by 1
c++;
}//End of while
result = evaluate();
cout<<" Result of "<<suffix<< " = " <<result;
return 0;
}

Output


Enter a valid Postfix Expression abc*+

Enter the values of a: 2

Enter the values of b: 3

Enter the values of c: 2

Result of abc*+ = 8

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