-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
361 lines (337 loc) · 11.2 KB
/
source.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Macros
#include <iostream>
#include <string>
#include <stack>
#include <unordered_map>
#include <deque>
// GMP and MPFR dependencies
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <gmp.h> // GMP
#include <mpfr.h> // MPFR
// header files
#include "check.hpp"
#include "compute.hpp"
#include "assign.hpp"
class Operation_Priority
{
public:
unordered_map<char,int> priority_map = {
{'+',1},
{'-',1},
{'*',2},
{'/',2},
{'%',2},
{'^',3}
};
};
//constants
#define BUFF_SIZE 1024
const mpfr_prec_t prec = 1024 * 8;
const mpfr_rnd_t rnd = MPFR_RNDN;
const Operation_Priority op;
// global variable
stack<mpz_t*> nums_int;
stack<mpfr_t*> nums_float;
using namespace std;
typedef struct _result_tuple{
bool if_error;
bool if_int;
} result_tuple;
result_tuple split(const string &str, const Operation_Priority &op, deque<string> &element_queue)
{
// split the string and get to know int mode
int last_position = 0;
bool int_mode = true;
for (int i = 0; i < str.size(); i++)
{
if (i == 0 && str[i] == '-')
{
continue;
}
if(op.priority_map.contains(str[i]) || str[i] == '(' || str[i] == ')')
{
if (op.priority_map.contains(str[i-1]) || str[i-1] == '(' || str[i-1] == ')')
{
last_position = i+1;
element_queue.push_back(str.substr(i,1));
continue;
}
string tmp = str.substr(last_position, i-last_position);
if(!detect_valid(tmp))
{
cerr << "Your input contains invalid operands!" << tmp << endl;
result_tuple ptr = {true, false};
return ptr;
}
else
{
if(int_mode)
{
if ( str[i] == '/' || !detect_int(tmp))
int_mode = false;
}
element_queue.push_back(tmp);
element_queue.push_back(str.substr(i,1));
last_position = i+1;
}
}
if (i == str.size() - 1)
{
string tmp = str.substr(last_position, i-last_position + 1);
if(!detect_valid(tmp))
{
cerr << "Your input contains invalid operands!" << tmp << endl;
result_tuple ptr = {true, false};
return ptr;
}
if(int_mode)
{
if (!detect_int(tmp))
int_mode = false;
}
element_queue.push_back(tmp);
}
}
result_tuple ptr = {false, int_mode};
return ptr;
}
result_tuple calculate(const string &str, mpz_t answer_int, mpfr_t answer_float)
{
// create instance op and element queue
Operation_Priority op;
deque<string> element_queue = {};
bool int_mode = true;
bool error_mode = false;
result_tuple ptr = split(str, op, element_queue);
int_mode = ptr.if_int;
error_mode = ptr.if_error;
// calculate
stack<char> operators;
if (int_mode)
{
// add 0 in the front
mpz_t zero;
mpz_init(zero);
nums_int.push(&zero);
// total number of elements
int n = element_queue.size();
// assign pointers
mpz_t * A = (mpz_t *) malloc((2 * n) * BUFF_SIZE); // buffer = 1024
int index = 0; // index of pointers
for (int i = 0; i < n; i++)
{
string element = element_queue.at(i); // this element
// if element is a number
if (element.length() != 1 || isdigit(element[0]))
{
assign(A[index], element);
nums_int.push(&A[index]);
index++;
}
// if element is '('
else if (element.starts_with('(') == 1)
{
operators.push('(');
if (element_queue.at(i+1).compare("-") == 0)
{
i++;
nums_int.push(&zero);
operators.push('-');
}
}
// if element is ')'
else if(element.compare(")") == 0)
{
while(operators.top()!='(')
{
assign(A[index+1], *nums_int.top()); nums_int.pop();
assign(A[index], *nums_int.top()); nums_int.pop();
mpz_init(A[index+2]);
char oper = operators.top(); operators.pop();
compute_int(A[index], A[index+1], oper, A[index+2]);
nums_int.push(&A[index+2]);
index += 3;
}
operators.pop();
}
// operators
else
{
// if possible calculate first!
while(!operators.empty() && operators.top()!= '(' && op.priority_map[operators.top()] >= op.priority_map[element[0]])
{
assign(A[index+1], *nums_int.top()); nums_int.pop();
assign(A[index], *nums_int.top()); nums_int.pop();
mpz_init(A[index+2]);
char oper = operators.top(); operators.pop();
compute_int(A[index], A[index+1], oper, A[index+2]);
nums_int.push(&A[index+2]);
index += 3;
}
operators.push(element[0]);
}
}
// we have iterate all characters, now calculate
while(!operators.empty() && operators.top()!='('){
assign(A[index+1], *nums_int.top()); nums_int.pop();
assign(A[index], *nums_int.top()); nums_int.pop();
mpz_init(A[index+2]);
char oper = operators.top(); operators.pop();
compute_int(A[index], A[index+1], oper, A[index+2]);
nums_int.push(&A[index+2]);
index += 3;
}
result_tuple result_ptr = {false, true};
mpz_set(answer_int, *(nums_int.top()));
// memory management
for (int k = 0; k < index; k ++)
{
mpz_clear (A[k]);
}
delete[](A);
return result_ptr;
}
else
{
// add 0 in the front
mpfr_t zero;
mpfr_init2 (zero, prec);
nums_float.push(&zero);
// total number of elements
int n = element_queue.size();
// assign pointers
mpfr_t * A = (mpfr_t *) malloc((2 * n) * BUFF_SIZE); // buffer = 1024
int index = 0; // index of pointers
for (int i = 0; i < n; i++)
{
string element = element_queue.at(i); // this element
// if element is a number
if (element.length() != 1 || isdigit(element[0]))
{
assign(A[index], element, prec, rnd);
nums_float.push(&A[index]);
index++;
}
// if element is '('
else if (element.starts_with('(') == 1)
{
operators.push('(');
if (element_queue.at(i+1).compare("-") == 0)
{
i++;
nums_float.push(&zero);
operators.push('-');
}
}
// if element is ')'
else if(element.compare(")") == 0)
{
while(operators.top()!='(')
{
assign(A[index+1], *nums_float.top(), prec, rnd); nums_float.pop();
assign(A[index], *nums_float.top(), prec, rnd); nums_float.pop();
mpfr_init2(A[index+2], prec); // result
char oper = operators.top(); operators.pop();
compute_float(A[index], A[index+1], oper, A[index+2]);
nums_float.push(&A[index+2]);
index += 3;
}
operators.pop();
}
// operators
else
{
// if possible calculate first!
while(!operators.empty() && operators.top()!= '(' && op.priority_map[operators.top()] >= op.priority_map[element[0]])
{
assign(A[index+1], *nums_float.top(), prec, rnd); nums_float.pop();
assign(A[index], *nums_float.top(), prec, rnd); nums_float.pop();
mpfr_init2(A[index+2], prec); // result
char oper = operators.top(); operators.pop();
compute_float(A[index], A[index+1], oper, A[index+2]);
nums_float.push(&A[index+2]);
index += 3;
}
operators.push(element[0]);
}
}
// we have iterate all characters, now calculate
while(!operators.empty() && operators.top()!='('){
assign(A[index+1], *nums_float.top(), prec, rnd); nums_float.pop();
assign(A[index], *nums_float.top(), prec, rnd); nums_float.pop();
mpfr_init2(A[index+2], prec); // result
char oper = operators.top(); operators.pop();
compute_float(A[index], A[index+1], oper, A[index+2]);
nums_float.push(&A[index+2]);
index += 3;
}
result_tuple result_ptr = {false, false};
mpfr_set(answer_float, *(nums_float.top()), rnd);
mpfr_exp_t exp;
string s = mpfr_get_str(NULL, &exp, 10, 0, answer_float, rnd);
if (s.compare("@NaN@") == 0)
{
result_ptr.if_error = true;
}
// memory management
for (int k = 0; k < index; k++)
{
mpfr_clear (A[k]);
}
mpfr_free_cache ();
delete[](A);
return result_ptr;
}
}
int main()
{
stack<string> var;
stack<string> exp;
string input;
getline(cin, input);
while (input.find('=') != std::string::npos)
{
for(int i =0; i < input.length(); i++)
{
if(input[i] == '=')
{
var.push(input.substr(0, i));
exp.push(input.substr(i+1, input.length() - i));
}
}
getline(cin, input);
}
while(!var.empty()&&!exp.empty())
{
if(var.top().empty())
{
var.pop();
exp.pop();
continue;
}
size_t start_pos = 0;
while((start_pos = input.find(var.top(), start_pos)) != string::npos) {
input.replace(start_pos, var.top().length(), exp.top());
start_pos += exp.top().length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
var.pop();
exp.pop();
}
mpz_t answer_int;
mpfr_t answer_float;
mpz_init(answer_int);
mpfr_init2(answer_float, prec);
result_tuple ptr = calculate(input, answer_int, answer_float);
if (!ptr.if_error && ptr.if_int)
{
cout << "answer is: " << answer_int << endl;
}
else if(!ptr.if_error && !ptr.if_int)
{
cout << "floating point calculation, answer is: ";
mpfr_out_str (stdout, 10, 10, answer_float, rnd);
cout << endl;
}
}