@@ -51,7 +51,11 @@ int main(int argc, char **argv) {
51
51
}
52
52
53
53
void manageFirstBracketAfterOperand (vector<string> &tokens) {
54
+ // cout << "Manage first bracket" << endl;
55
+ if (tokens.size () == 0 )
56
+ return ;
54
57
string multiplySign = " *" ;
58
+ if (tokens.size () == 0 ) return ;
55
59
for (int i = 0 ; i < tokens.size () - 1 ; i++) {
56
60
if (isOperand (tokens[i]) && tokens[i + 1 ] == " (" ) {
57
61
i++;
@@ -60,6 +64,9 @@ void manageFirstBracketAfterOperand(vector<string> &tokens) {
60
64
}
61
65
}
62
66
void manageLeadingNegativeSign (vector<string> &tokens) {
67
+ // cout << "Manage Leading Negative Sign" << endl;
68
+ if (tokens.size () == 0 )
69
+ return ;
63
70
string openBracket = " (" ;
64
71
string closeBracket = " )" ;
65
72
string zero = " 0" ;
@@ -104,6 +111,7 @@ double eval(string &exp) { return eval(exp, 0); }
104
111
105
112
double eval (string &exp, double x) {
106
113
vector<string> tokens = breakIntoTokens (exp);
114
+ // if(tokens.size() == 0) return -1;
107
115
manageFirstBracketAfterOperand (tokens);
108
116
manageLeadingNegativeSign (tokens);
109
117
tokens = convertToPostfixExp (tokens);
@@ -119,18 +127,19 @@ double eval(string &exp, double x) {
119
127
excStack.push (operation (op1, op2, token));
120
128
}
121
129
}
122
- return excStack.top ();
123
- return 0 ;
130
+ return excStack.empty () ? -1 : excStack.top ();
124
131
}
125
132
vector<string> breakIntoTokens (string &exp) {
133
+ // cout << "Breaking Into Tokens" << endl;
134
+
126
135
vector<string> tokens;
127
136
string currentToken = " " ;
128
137
129
138
for (char &ch : exp) {
130
139
if (ch == ' ' || ch == ' \n ' || ch == ' \t ' ) continue ;
131
140
132
141
if (!isValidCharacter (ch)) {
133
- cout << dye::light_red (" Invalid Expression" );
142
+ cout << dye::light_red (" Invalid Expression " );
134
143
return tokens;
135
144
}
136
145
@@ -151,9 +160,11 @@ vector<string> breakIntoTokens(string &exp) {
151
160
return tokens;
152
161
}
153
162
vector<string> convertToPostfixExp (vector<string> &tokens) {
163
+ // cout << "Convert to postfix" << endl;
154
164
vector<string> postfix;
155
165
stack<string> stk;
156
-
166
+ if (tokens.size () == 0 )
167
+ return tokens;
157
168
for (auto &token : tokens) {
158
169
if (isOperand (token))
159
170
postfix.push_back (token);
0 commit comments