-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (157 loc) · 3.84 KB
/
main.cpp
File metadata and controls
173 lines (157 loc) · 3.84 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <utility>
#include <sstream>
// This is a file that will help you test your classes.
// There is no need to submit this file and you can edit
// and change it as you wish.
// Run the main with an expression as parameter.
#include "Expression.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;
// This collection of functions is used to parse a simple
// structure of expressions and build expressions.
// Notice that the parsing is very basic.
// This parsing is far from perfect, but
// for now it is sufficient for our purposes.
size_t split(const string& source);
bool nestedInParen(const string& s, size_t l);
size_t splitOnOp(const string& s,char op);
bool parseNumber(const string& source, int& val);
Expression* parseExpression(const string& source);
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "Usage: main expression" << endl;
cout << "For example: main \"Abs(342*(29+Abs(-12)+Square(5)))\"" << endl;
return 1;
}
else {
Expression *e=parseExpression(argv[1]);
if (e!=nullptr) {
cout << e->print() << " = " << e->evaluate() << endl;
delete e;
}
else {
cout << "Something went wrong with parsing." << endl;
}
}
return 0;
}
bool nestedInParen(const string& s, size_t l) {
unsigned int nesting=0;
for (size_t i=0 ; i<l ; ++i) {
if (s[i]=='(') {
++nesting;
}
else if (s[i]==')') {
--nesting;
}
}
return nesting;
}
size_t splitOnOp(const string& s,char op) {
size_t loc=0;
for (size_t start = 0 ;
(loc=s.find(op,start)) != std::string::npos ;
start = loc+1) {
if (!nestedInParen(s,loc) &&
(op != '+' || loc==0 || (s[loc-1]!='e' && s[loc-1]!='E'))) {
return loc;
}
}
return std::string::npos;
}
size_t split(const string& source) {
size_t loc=1;
if (source.substr(0,4) == "Abs(") {
loc = 4;
}
else if (source.substr(0,7) == "Square(") {
loc = 7;
}
else if (source[0] != '(') {
if (source.find_first_not_of("-0123456789") == std::string::npos) {
return std::string::npos;
}
// As * has precedence over +, if there are no parentheses
// try to find a + then, the * will bind stronger
if (((loc=splitOnOp(source,'+')) != std::string::npos) ||
((loc=splitOnOp(source,'*')) != std::string::npos)) {
return loc;
}
else {
return std::string::npos;
}
}
unsigned int nesting=1;
for (; nesting && loc<source.length() ; ++loc) {
if (source[loc] == '(') {
++nesting;
}
else if (source[loc]==')') {
--nesting;
}
}
if (!nesting && loc < source.length()-1) {
return loc;
}
return std::string::npos;
}
bool parseNumber(const string& source, int& val) {
stringstream s(source);
char c;
if (s >> val && !(s >> c)) {
return true;
}
return false;
}
Expression* parseExpression(const string& source) {
size_t place = split(source);
if (place != std::string::npos) {
Expression* l=parseExpression(source.substr(0,place));
if (!l) {
return nullptr;
}
Expression* r=parseExpression(source.substr(place+1,source.length()-place-1));
if (!r) {
delete l;
return nullptr;
}
switch (source[place]) {
case '*':
return new Multiply(l,r);
case '+':
return new Add(l,r);
default:
delete l;
delete r;
return nullptr;
}
}
else {
if (source.substr(0,4)=="Abs(" && source[source.length()-1]==')') {
Expression* t=parseExpression(source.substr(4,source.length()-5));
if (!t) {
return nullptr;
}
return new Abs(t);
}
if (source.substr(0,7)=="Square(" && source[source.length()-1]==')') {
Expression* t=parseExpression(source.substr(7,source.length()-8));
if (!t) {
return nullptr;
}
return new Square(t);
}
if (source[0]=='(' && source[source.length()-1]==')') {
return parseExpression(source.substr(1,source.length()-2));
}
int temp;
if (parseNumber(source,temp)) {
return new Number(temp);
}
return nullptr;
}
}