-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
79 lines (71 loc) · 1.91 KB
/
main.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "Expr_Command_Visitor.h"
#include "Add_Command.h"
#include "Subtract_Command.h"
#include "Multiply_Command.h"
#include "Divide_Command.h"
#include "Modulus_Command.h"
#include "Number_Command.h"
#include "Expr_Tree_Builder.h"
#include "Eval_Expr_Tree.h"
using namespace std;
int create_expression(vector <string> & equStr)
{
int num;
Expr_Tree_Builder etb = Expr_Tree_Builder();
for (string ss : equStr) {
try {
num = stoi(ss);
etb.build_number(num);
} catch (invalid_argument & e) {
if (ss == "+") {
etb.build_add_operator();
} else if (ss == "-") {
etb.build_subtract_operator();
} else if (ss == "*") {
etb.build_multiply_operator();
} else if (ss == "/") {
etb.build_divide_operator();
} else if (ss == "%") {
etb.build_modulus_operator();
} else if (ss == "(") {
etb.build_open_parenthesis();
} else if (ss == ")") {
etb.build_close_parenthesis();
} else {
cout << "Invalid Expression" << endl;
equStr.clear();
return 0;
}
}
}
equStr.clear();
Expr_Command * expr = etb.get_expression();
Eval_Expr_Tree * eval = new Eval_Expr_Tree();
expr->accept(eval);
int res = eval->result();
delete eval;
return res;
}
int main()
{
string s;
Stack <string> equ = Stack<string>();
vector<string> equv{};
string delim = " ";
int pos;
while (true) {
cout << "Enter the expression:" << endl;
getline(cin, s);
if (s == "QUIT") break;
while ((pos = s.find(delim)) != (int) string::npos) {
equv.push_back(s.substr(0, pos));
s.erase(0, pos + delim.length());
}
equv.push_back(s);
cout << create_expression(equv) << endl;
}
}