-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.c
333 lines (324 loc) · 8.96 KB
/
calculator.c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include "structures.h"
#include "calculator.h"
#include "tokenizer.h"
int KnownVarsPositions[28] = { 0 };
double KnownVars[28] = { '\0' };
int TabsCounter = 0;
void ClearVariables() {
for (int i = 0; i < sizeof(KnownVarsPositions) / sizeof(KnownVarsPositions[0]); i++) {
KnownVarsPositions[i] = 0;
}
}
int PriorityDefiner(Token token) { //def
switch (token.type)
{
case OPERAND:
if (token.data == '-' || token.data == '+') {
return 1;
}
else if (token.data == '*' || token.data == '/') {
return 2;
}
else if (token.data == '!') {
return 3;
}
else if (token.data == '^') {
return 5;
}
case FUNCTION:
return 4;
}
return 0; // case indefined
}
Queue* ConvertToPolishs(Token* tokens, int size) {
int i = 0;
Stack* stack = NewStack();
initStack(stack);
Queue* que = NewQueue();
initQueue(que);
int BracketFlag = 0;
Token temp;
while (tokens[i].type != END) { // semestr.online/informatics/polish.php
if (tokens[i].type == VALUE || tokens[i].type == VARIABLE) {
enqueue(que, tokens[i]);
}
else if (tokens[i].type == BRACKET_OPEN) {
push(stack, tokens[i]);
}
else if (tokens[i].type == BRACKET_CLOSE) {
while (!(isEmptyStack(stack)) && peek(stack).type != BRACKET_OPEN) {
enqueue(que, pop(stack));
}
pop(stack); // remove '('
if (!isEmptyStack(stack) && peek(stack).type == FUNCTION) {
enqueue(que, pop(stack)); // Add the function to the queue after the last argument inside the brackets.
}
}
else if (tokens[i].type == OPERAND) {
while (!isEmptyStack(stack) && peek(stack).type != BRACKET_OPEN &&
(PriorityDefiner(peek(stack)) > PriorityDefiner(tokens[i]) ||
(PriorityDefiner(peek(stack)) == PriorityDefiner(tokens[i]) &&
peek(stack).data != '^'))) { // Правильная обработка операндов и ассоциативности '^'
enqueue(que, pop(stack));
}
push(stack, tokens[i]);
}
//else if (tokens[i].type == BRACKET_CLOSE) {
// while (!(isEmptyStack(stack)) && peek(stack).type != BRACKET_OPEN) {
// enqueue(que, pop(stack));
// }
// pop(stack); // remove (
//} else if (!isEmptyStack(stack) && peek(stack).type == FUNCTION) {
// enqueue(que, pop(stack)); // Add the function to the queue after the last argument inside the brackets.
//}
//else if (tokens[i].type == OPERAND) {
// while (!isEmptyStack(stack) && peek(stack).type != END &&
// PriorityDefiner(peek(stack)) >= PriorityDefiner(tokens[i]) &&
// (peek(stack).data != '^')) { // Проверяем, что на вершине стека не операция '^'
// enqueue(que, pop(stack));
// }
// push(stack, tokens[i]);
//}
/*else if (tokens[i].type == OPERAND) {
while (!isEmptyStack(stack) && peek(stack).type != END) {
if (PriorityDefiner(tokens[i]) <= PriorityDefiner(peek(stack))) {
enqueue(que, pop(stack));
}
else {
break;
}
}
push(stack, tokens[i]);
}*/
else if (tokens[i].type == FUNCTION) {
push(stack, tokens[i]);
}
i++;
}
while (stack->start != NULL) { // while empty stack
enqueue(que, pop(stack));
}
free(stack); // we are good boys
return que;
}
Token FunctionCalculate(Token func, Token val) {
Token token;
ClearToken(&token);
if (func.type == FUNCTION) {
token.type == VALUE;
switch (func.func) {
case __sin:
token.value = sin(val.value);
return token;
case __cos:
token.value = cos(val.value);
return token;
case __tg:
token.value = tan(val.value);
return token;
case __arctg:
token.value = atan(val.value);
return token;
case __arcctg:
token.value = (3.14159265/2) - atan(val.value); // there isnt arcctg in math.h :(
return token;
case __arcsin:
token.value = asin(val.value);
return token;
case __arccos:
token.value = acos(val.value);
return token;
case __sqrt:
token.value = sqrt(val.value);
return token;
case __factorial: //add
if (val.value < 0 || val.value != (int)val.value) {
token.type = __ERROR;
return token;
}
else {
double result = 1.0;
for (int i = 2; i <= val.value; ++i) {
result *= i;
}
token.value = result;
return token;
}
case __pow: //add
token.value = pow(val.value, val.data);
return token;
}
}
return token;
}
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
Token OperatorCalculation(Token val1, Token val2, Token oper) {
Token token;
ClearToken(&token);
token.type = VALUE;
switch (oper.data) {
case '+':
token.value = val1.value + val2.value;
return token;
case '-':
token.value = val1.value - val2.value;
return token;
case '*':
token.value = val1.value * val2.value;
return token;
case '/':
if (val2.value != 0) { // It is impossible to divide by zero, otherwise an __ERROR Делить на ноль нельзя, иначе ошибка
if (val2.value != 0) {
token.value = val1.value / val2.value;
return token;
}
else {
printf("Trying divide on zer0\n");
exit(-1);
}
}
else {
token.type = __ERROR;
return token;
}
case '^': //add
token.value = pow(val1.value, val2.value);
return token;
}
token.type = __ERROR;
return token;
}
double calculate(Queue* que) {
Stack* stack = NewStack();
while (peek(que).type != END) {
Token token;
ClearToken(&token);
token = dequeue(que);
if (token.type == VALUE) {
push(stack, token); // If the token is a number, put it on the stack Если токен - число, поместите его в стек
}
else if (token.type == OPERAND) {
// If the token is an operator, extract the top two numbers from the stack Если токен - оператор, извлеките два верхних числа из стека
Token resultToken;
resultToken.type = VALUE;
if (token.data == '!') {
Token value = pop(stack);
if (floor(value.value) == value.value && value.value >=0) { // fucktorial works only with integer
resultToken.value = (double)factorial((int)value.value);
}
else {
printf("Trying take factorial from bad number!");
}
}
else {
Token operand2 = pop(stack);
Token operand1 = pop(stack);
resultToken = OperatorCalculation(operand1, operand2, token);
}
if (resultToken.type != __ERROR) {
push(stack, resultToken);
}
else {
printf("__ERROR: Division by zero\n");
exit(EXIT_FAILURE);
}
}
else if (token.type == FUNCTION) {
Token ValToken = pop(stack);
Token res;
ClearToken(&res);
res = FunctionCalculate(token, ValToken);
if (res.type != __ERROR) {
push(stack, res);
}
else {
printf("__ERROR: Division by zero or something else illigal\n");
exit(-1);
}
}
}
Token finalResultToken = pop(stack);
double finalResult = finalResultToken.value;
free(stack);
return finalResult;
}
int VariableFinder(Token* array) {
while (array->type != END) {
if (array->type == VARIABLE) return 1;
array++;
}
return 0;
}
int GetIndexLetter(char letter) {
letter = tolower(letter);
if (letter >= 'a' && letter <= 'z') {
return letter - 'a';
}
else {
return -1;
}
}
int IsPreviouslyKnownVariable(Token token) {
int index = GetIndexLetter(token.data);
if (KnownVarsPositions[index]) { //if variable was defined earlier
return 1;
}
return 0;
}
int InVariableLvl = 0;
void DefineNewVariable(Token* array) {
for (int i = 0; array[i].type != END; i++) {
if (array[i].type == VARIABLE) {
if (IsPreviouslyKnownVariable(array[i])) {
array[i].type = VALUE;
array[i].value = KnownVars[GetIndexLetter(array[i].data)]; // grab known value to our variable and make it type = VALUE
}
else {
TabsCounter += 5;
printf("\n");
for (int i = 0; i < TabsCounter; i++) { //UI :3
printf("-");
}
char str[256] = { '\0' };
printf("[%c]: ", array[i].data);
scanf("%255[^\n]%*c", str);
int length = strlen(str) + 10;
Token* newtokens = tokenizer(str, length);
if (VariableFinder(newtokens)) {
if (KnownVarsPositions[GetIndexLetter(array[i].data)] == -1) {
printf("Recursion with variables is not supported :/\n\n");
exit(-1);
}
InVariableLvl += 1;
DefineNewVariable(newtokens);
TabsCounter -= 5;
Queue* PolishTokens = ConvertToPolishs(newtokens, length);
double CalculatedValue = calculate(PolishTokens);
KnownVars[GetIndexLetter(array[i].data)] = CalculatedValue;
array[i].type = VALUE;
array[i].value = KnownVars[GetIndexLetter(array[i].data)];
KnownVarsPositions[GetIndexLetter(array[i].data)] = 1;
}
else {
TabsCounter -= 5;
Queue* PolishTokens = ConvertToPolishs(newtokens, length);
double CalculatedValue = calculate(PolishTokens);
KnownVars[GetIndexLetter(array[i].data)] = CalculatedValue;
array[i].type = VALUE;
array[i].value = KnownVars[GetIndexLetter(array[i].data)];
KnownVarsPositions[GetIndexLetter(array[i].data)] = 1;
}
free(newtokens);
}
}
}
return;
}