-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.cpp
162 lines (150 loc) · 2.79 KB
/
calc.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
#include "calc.h"
#include <exception>
#include <cmath>
std::map<char, int> operation_priority {
{'(', 0},
{')', 0},
{'+', 1},
{'-', 1},
{'*', 2},
{'/', 2},
{'^', 3}
};
std::string reverse_pol_exp(std::string exp)
{
std::string res, num;
std::stack<char> operations;
exp.erase( std::remove( exp.begin(), exp.end(), ' ' ), exp.end() );
for (int i = 0; i < exp.length(); ++i)
{
if (exp[i] >= '0' && exp[i] <= '9' || exp[i] == '.')
{
num += exp[i];
}
else
{
if (!num.empty())
{
res += num + ' ';
num.clear();
}
switch (exp[i])
{
case '*':
case '/':
case '+':
case '^':
if (!operations.empty() && operation_priority[exp[i]] <= operation_priority[operations.top()] )
{
res += operations.top();
res += ' ';
operations.pop();
}
operations.push(exp[i]);
break;
case '-':
if ((i == 0 || exp[i-1] == '(') && (exp[i + 1] >= '0' && exp[i + 1] <= '9'))
{
num += '~';
}
else
{
if (!operations.empty() && operation_priority['-'] <= operation_priority[operations.top()] )
{
res += operations.top();
res += ' ';
operations.pop();
}
operations.push('-');
}
break;
case '(':
operations.push('(');
break;
case ')':
while (operations.top() != '(')
{
res += operations.top();
operations.pop();
res += ' ';
}
operations.pop();
break;
default:
std::cout << "Error" << std::endl;
break;
}
}
}
if (!num.empty())
{
res += num + ' ';
}
while (!operations.empty())
{
res += operations.top();
res += ' ';
operations.pop();
}
std::cout << res << std::endl;
return res;
}
double calc (std::string exp)
{
exp = reverse_pol_exp(exp);
std::string num;
double tmp = 0, first = 0, second = 0, res = 0;
std::stack<double> data;
for (int i = 0; i <= exp.length(); ++i)
{
if (exp[i] == '~')
num += '-';
else if (exp[i] >= '0' && exp[i] <= '9' || exp[i] == '.')
{
num += exp[i];
}
else
{
if (!num.empty())
{
tmp = atof(num.c_str());
num.clear();
data.push(tmp);
}
if (exp[i] == ' ')
continue;
if (data.size() >= 2)
{
second = data.top();
data.pop();
first = data.top();
data.pop();
switch (exp[i])
{
case '*':
res = first * second;
break;
case '/':
if (second == 0)
throw std::invalid_argument("division by zero");
res = first / second;
break;
case '+':
res = first + second;
break;
case '^':
res = pow(first, second);
break;
case '-':
res = first - second;
break;
default:
std::cout << "Operation error" << std::endl;
break;
}
data.push(res);
}
}
}
return data.top();
}