-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.cpp
228 lines (201 loc) · 3.91 KB
/
Calculator.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "Auxiliary.h"
#include "Calculator.h"
Calculator::~Calculator()
{
//std::cout << "Destructor" << std::endl;
expr_ptr = store_ptr;
free(expr_ptr);
}
double Calculator::calc(const char* str)
{
double result = 0.0;
if (check_exponent(str) < 2)
{
size_t size = __strlen(str);
expr_ptr = (char*)malloc(size + 1);
if (expr_ptr == nullptr)
throw std::runtime_error("Allocation error");
size_t i = 0;
for (i = 0; i < size; ++i)
expr_ptr[i] = str[i];
expr_ptr[i] = '\0';
}
else
insert_parenthesises(str);
store_ptr = expr_ptr;
get_token();
if (!*token)
throw std::runtime_error("No Expression Present"); // no expression present
evaluate_expr2(result);
if (*token) // last token must be null
throw std::runtime_error("Syntax Error");
return result;
}
void Calculator::evaluate_expr2(double& result)
{
char op;
double temp;
evaluate_expr3(result);
while ((op = *token) == '+' || op == '-')
{
get_token();
evaluate_expr3(temp);
switch (op)
{
case '-':
result = result - temp;
break;
case '+':
result = result + temp;
break;
}
}
}
void Calculator::evaluate_expr3(double& result)
{
char op;
double temp;
evaluate_expr4(result);
while ((op = *token) == '*' || op == '/')
{
get_token();
evaluate_expr4(temp);
switch (op)
{
case '*':
result = result * temp;
break;
case '/':
result = result / temp;
break;
}
}
}
void Calculator::evaluate_expr4(double& result)
{
double temp;
evaluate_expr5(result);
while (*token == '^')
{
get_token();
evaluate_expr5(temp);
result = pow(result, temp);
}
}
void Calculator::evaluate_expr5(double& result)
{
char op;
op = 0;
if ((token_type == static_cast<char>(types::DELIMITER)) && *token == '+' || *token == '-')
{
op = *token;
get_token();
}
evaluate_expr6(result);
if (op == '-')
result = -result;
}
void Calculator::evaluate_expr6(double& result)
{
if ((*token == '('))
{
get_token();
evaluate_expr2(result);
if (*token != ')')
throw std::runtime_error("Unbalanced Parentheses");
get_token();
}
else
{
switch (token_type)
{
case static_cast<char>(types::NUMBER) :
result = __atof(token);
get_token();
return;
default:
throw std::runtime_error("Syntax Error");
}
}
}
void Calculator::get_token()
{
char* temp;
token_type = 0;
temp = token;
*temp = '\0';
if (!*expr_ptr) // end of expression
return;
while (__isspace(*expr_ptr)) // skip over white space
++expr_ptr;
if (__strchr("+-*/%^=()", *expr_ptr))
{
token_type = static_cast<char>(types::DELIMITER);
*temp++ = *expr_ptr++; // to next char
}
else if (__isdigit(*expr_ptr) || *expr_ptr == '.')
{
while (!__strchr(" +-/*%^=()\t\r", *expr_ptr) && (*expr_ptr))
*temp++ = __toupper(*expr_ptr++);
token_type = static_cast<char>(types::NUMBER);
}
*temp = '\0';
}
void Calculator::insert_parenthesises(const char* s)
{
size_t count = check_exponent(s);
size_t size = __strlen(s);
expr_ptr = (char*)malloc(size + 2 * count + 1);
if (expr_ptr == nullptr)
throw std::runtime_error("Allocation error");
size_t curr_count = 0;
size_t i = 0;
size_t j = 0;
while (s[i] != '\0')
{
if (s[i] != '^' && curr_count < count)
{
expr_ptr[j] = s[i];
++i;
++j;
}
if (s[i] == '^' && (curr_count >= 0 && curr_count < count))
{
expr_ptr[j] = s[i];
++curr_count;
++j;
expr_ptr[j] = '(';
++i;
++j;
}
if (__strchr("+-/*%=()", s[i]) && (curr_count >= count && curr_count < 2 * count))
{
expr_ptr[j] = ')';
++j;
expr_ptr[j] = s[i];
++i;
++j;
++curr_count;
}
if (__strchr(" \t\r", s[i]) && (curr_count >= count && curr_count < 2 * count))
{
expr_ptr[j] = s[i];
++i;
++j;
}
if (!__strchr(" +-/*%=()\t\r", s[i]) && curr_count >= count)
{
expr_ptr[j] = s[i];
++i;
++j;
}
}
while (curr_count < 2 * count)
{
expr_ptr[j] = ')';
++j;
++curr_count;
}
expr_ptr[j] = '\0';
//std::cout << expr_ptr << std::endl;
}