/****************************************************************************** * * Name : Scott Kristjanson * Number : 123456789 * email : skristja@sfu.ca * Course : Cmpt135 * Assignment : 1 * Question : B4 * * File : PostFix.cpp * * This implements a postfix calculator by Toby Donaldson. * Modified slightly by Scott Kristjanson for Cmpt135 Spring 2016 Class * * This program implements a postfix calculator and has been enhanced to support * the following additional features as part of assignment 1: * % operator - must convert doubles to ints, then compute (a % b) * dup - duplicates current element on the tos * inc - adds one to current element on the tos * dec - subtracts one from element on the tos * ^2 - squares the element on the tos * * Implementing this new functionality required that the student modify the * procedure process_token to support this new functionality. * ******************************************************************************/ #include "error.h" #include #include #include using namespace std; //////////////////////////////////////////////// // // Global variables. // //////////////////////////////////////////////// const string prompt = "--> "; string token; // global variable vector stack; // global variable bool Error = false; //////////////////////////////////////////////// // // Helper functions. // //////////////////////////////////////////////// // Converts string s to a double. double string_to_double(const string& s) { return stod(s); } void report_error(const string& s) { cout << "Error: " << s << endl; if (!Error) { cout << "Sorry, errors not handled yet!" << endl; cout << "We will look at error handling in Chapter 16" << endl; } Error = true; } //////////////////////////////////////////////// // // Stack functions. // //////////////////////////////////////////////// // return top of stack without removing it double peek() { if (stack.size() == 0) { report_error("tried to peek at an empty stack"); return 0; // never called: needed to satisfy compiler } else { return stack.back(); } } // remove and return the element of the stack double pop() { double tos = 0; if (stack.size() == 0) { report_error("tried to pop an empty stack"); } else { tos = stack.back(); stack.pop_back(); } return tos; } // put x on the top of the stack void push(double x) { stack.push_back(x); } //////////////////////////////////////////////// // // Token processing functions. // //////////////////////////////////////////////// void process_token() { if (token == "+") { double b = pop(); double a = pop(); push(a + b); } else if (token == "-") { double b = pop(); double a = pop(); push(a - b); } else if (token == "*") { double b = pop(); double a = pop(); push(a * b); } else if (token == "/") { double b = pop(); double a = pop(); push(a / b); } else if (token == "%") { int b = pop(); int a = pop(); push(a % b); } else if (token == "inc") { double a = pop(); push(a + 1); } else if (token == "dec") { double a = pop(); push(a - 1); } else if (token == "^2") { double a = pop(); push(a * a); } else if (token == "dup") { double a = peek(); push(a); } else if (token == "=") { cout << "tos = " << peek() << "\n"; cout << prompt; } else { push(string_to_double(token)); } // if } //////////////////////////////////////////////// // // Main function. // //////////////////////////////////////////////// int main() { cout << "Postfix expression evaluator\n"; cout << prompt; while ((!Error) && (cin >> token) && (token != "Q")) { process_token(); } cout << "Bye!" << endl; }