From c65b642d5faaae9a0d50546bc0a41583e4dadcde Mon Sep 17 00:00:00 2001 From: pezy-pc Date: Fri, 19 Dec 2014 14:18:18 +0800 Subject: [PATCH] Finished assignment2 --- .../as2/assignment2_reading_pezy_version.cpp | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 forPezy/as2/assignment2_reading_pezy_version.cpp diff --git a/forPezy/as2/assignment2_reading_pezy_version.cpp b/forPezy/as2/assignment2_reading_pezy_version.cpp new file mode 100644 index 0000000..facdf81 --- /dev/null +++ b/forPezy/as2/assignment2_reading_pezy_version.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +using std::cout; using std::endl; using std::cerr; + +struct StackNode { + int val; + StackNode *next; + StackNode(int x) : val(x), next(nullptr) {} +}; + +class Stack { +public: + ~Stack() + { + for (StackNode *pDel=node; node; delete pDel) + node = node->next; + } + + bool empty() { return node == nullptr; } + void push(int val) + { + StackNode *new_node = new StackNode(val); + new_node->next = node; + node = new_node; + } + int pop() + { + StackNode *top = node; + assert(top); + int ret = top->val; + node = top->next; + delete top; + return ret; + } +private: + StackNode *node{nullptr}; +}; + +int main(int argc, char **argv) +{ + if (argc != 2) { cerr << "cannot read file" << endl; exit(0); } + std::ifstream ifs(argv[1]); + Stack stk; + for (std::string exp; getline(ifs, exp); ) { + if (isdigit(exp[0])) { + cout << "reading number " << exp << endl; + stk.push(stoi(exp)); + } + else if (exp[0] == '+' || exp[0] == '-' || exp[0] == '*' || exp[0] == '/') { + cout << "reading operator " << exp[0] << endl; + int op1{0}, op2{0}; + if (!stk.empty()) op2 = stk.pop(); + else { cerr << "too many operators" << endl; exit(0); } + if (!stk.empty()) op1 = stk.pop(); + else { cerr << "too many operators" << endl; exit(0); } + switch (exp[0]) { + case '+' : stk.push(op1 + op2); break; + case '-' : stk.push(op1 - op2); break; + case '*' : stk.push(op1 * op2); break; + case '/' : stk.push(op1 / op2); break; + } + } + } + int ret = stk.pop(); + if (stk.empty()) cout << "The result is " << ret << endl; + else cerr << "too many numbers" << endl; +} + + + + + + + + + + + + + + + + + + + +