I'm trying to solve this math problem using stacks:
I know I have to create two different stacks (numbers and operands).
I want to start very simple before I figure out how to do the whole problem.
So, I would like to use this problem:
I pushed 10 and 5 in the numbers stack and I also pushed + in the operands stack.
But I don't know exactly what to do from there.
I can figure out the top number, which is 5, but how do I get down to the 10?
Also, how do I get it to add each other?
I'm just new to stacks, so if anyone could explain, please do it so I can understand. Thanks.
Here's my attempt which obviously does not work like it's supposed to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> number;
stack<char> operand;
number.push(10);
number.push(5);
operand.push('+');
cout << number.top() << operand.top() << number.top() << endl;
system("pause");
}
|