Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list> /* Linked Lists */
- #include <stack> /* Stacks */
- #include <iostream> /* cout cin */
- int main() {
- std::stack< double, std::list<double> > postfixStack;
- std::string input;
- std::cout << "Enter a postfix expression: ";
- std::getline(std::cin, input);
- while (input != "") {
- std::cout << "Input expression: " << input << std::endl;
- for (int i = 0; i<input.length()-1; i++) {
- if (input.compare(i, 1, " ")) { // should ignore spaces, but doesn't
- std::cout << "Skipping element " << i << " n";
- } else if (static_cast<int>(input[i]) == input[i]) { // push numbers onto the stack
- postfixStack.push(static_cast<double>(input[i]));
- std::cout << "Pushing " << input[i] << " onto the stack.n";
- } else if (input.compare(i, 1, "+")) { // pop two numbers off the stack (1), apply the operator to them (2), and push that onto the stack (3)
- double operand1 = postfixStack.top();
- postfixStack.pop();
- double operand2 = postfixStack.top();
- postfixStack.pop();
- postfixStack.push(operand1 + operand2);
- std::cout << "Adding " << operand1 << " and " << operand2 << std::endl;
- }
- }
- std::getline(std::cin, input);
- }
- if (!postfixStack.empty()) {
- std::cout << "Result of expression: " << postfixStack.top() << std::endl;
- } else {
- std::cout << "It appears that you did not enter an expression to evaluate.n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement