Ok so basically what i have to do is prompt the user for 3 numbers and push them onto a stack (this is to be done in the input() function)
i also need an evaluate function that calls the input() function and that pops the first two numbers on the stack, perform an operation inputted by the user (+, -, *...)etc and pushes that result back to the top of the stack...this happens one more time until only one item remains in the stack
so for example i enter the postfix expression " 4 3 5 * + " it should put these 3 numbers on a stack...pop 5..pop 3 then multiply them to get 15..push 15 back on top...pop 15..pop 4..add them together and push 19 back on the stack..so that 19 is the only thing on the stack
ok heres what i have so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <stack> //stack header file
using namespace std;
stack <int> operands;
int main()
{
}
void input() //user inputs 3 items into a stack
{
cout << "Input three numbers into a stack (Enter a number then press Enter)" << endl;
cin >> item1;
operands.push(item1);
cin >> item2;
operands.push(item2);
cin >> item3;
operands.push(item3);
}
void evaluate() //evaluate function operations included
{
item3 = operands.top();
operands.pop();
item2 = operands.top();
operands.pop();
item2 = item3*item2;
operands.push(item2);
operands.pop();
operands.pop();
item1 = item2+item1;
operands.push(item1);
cout << item1 << endl;
}
|
my problems are
what do i put in the main?, how can i read in operands (+, -, *, /...)instead of physcally doing it in the program myself (see where i did item3*item2, and item1=item2+1;)
and how do i call input() in the evaluate function i cant figure out how to pass item1 item2 and item3 to it because they arent defined until the user inputs a value