Need help with my function. Heres the question:
you should write a function evaluate() that performs the task of evaluating a postfix expression. The function should read in the operands by calling a function input() described below. After the operands have been read, the operators will be read one at a time. Now, for each operator read, the function should pop two elements from the stack operands, perform the specified operation on the two operands and push the result back onto the stack operands. this should be repeated untill there is only one element on the stack operands. You should define a function input() that reads in the postfix expression one element at a time. To simplify things the operands should be input first and pushed onto the stack operands.
#include<iostream>
#include<stack>
usingnamespace std;
stack <int> operands;
void evaluate(int);
void input();
void main()
{
}
void evaluate()
{
int var;
int var1;
input();
var =operands.top();
operands.pop();
var1 =operands.top();
operands.pop();
switch(op)
{
case"+": operands.push(var+var1);
break;
case"-": operands.push(var-var1);
break;
case"*": operands.push(var*var1);
break;
case"/": operands.push(var/var);
break;
default: cout << "That is not a valid operator." <<endl;
break;
}
}
void input( char)
{
int x;
int y;
char op;
int count=0;
int num;
cout << "Please enter how many numbers you would like to postfix evaluate." <<endl;
cin >> x;
while(count <x)
{
operands.push(x);
cout << "Please enter the numbers you would like to evaluate." <<endl;
cin >> num;
count++;
}
cout << "Please enter how many operands you would like to use." << endl;
cin >> y;
while (count <y)
{
operands.push(y);
cout << "Please enter the operands you would like to use." << endl;
cin >> op;
count++;
}
}