-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.c
88 lines (80 loc) · 1.38 KB
/
calc.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "calc.h"
int st_ptr = 0; // óęŕçŕňĺëü âĺđřčíű ńňĺęŕ
double Calc(double _x, char _postfix[][100], double* _real_numb_arr)
{
double stack[10000]; // ńňĺę äë˙ őđŕíĺíč˙ îďĺđŕíäîâ
double temp;
int in_index = 0;
double op_a, op_b;
do
{
char c = _postfix[0][in_index];
if ((c >= '0') && (c <= '9') || (c == 'x'))
{
if (c == 'x')
temp = _x;
else
{
temp = _real_numb_arr[in_index];
}
}
else
{
if (c == 's' || c == 'c' || c == 't' || c == 'k')
{
op_a = pop_dou(stack);
switch (c)
{
case 's':
temp = sin(op_a);
break;
case 'c':
temp = cos(op_a);
break;
case 't':
temp = tan(op_a);
break;
case 'k':
temp = 1/tan(op_a);
break;
}
}
else{
op_b = pop_dou(stack);
op_a = pop_dou(stack);
switch (c)
{
case '+':
temp = op_a + op_b;
break;
case '-':
temp = op_a - op_b;
break;
case '*':
temp = op_a * op_b;
break;
case '/':
temp = op_a / op_b;
break;
case '^':
temp = pow(op_a, op_b);
break;
}
}
}
push_dou(stack, temp);
in_index++;
} while (_postfix[0][in_index] != -52);
return pop_dou(stack);
}
void push_dou(double *_stack, double data)
{
_stack[st_ptr++] = data;
}
double pop_dou(double *_stack)
{
return _stack[--st_ptr];
}