Repository navigation
Expand file tree
/
Copy pathcalcer.cpp
More file actions
188 lines (168 loc) · 7.12 KB
/
Copy pathcalcer.cpp
File metadata and controls
188 lines (168 loc) · 7.12 KB
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
#include "calcer.h"
#include <math.h>
std::map<Calcer::ErrorType, std::string>Calcer::ErrorType2Name = {
{ErrorType::BracketNotMatched, "Bracket Not Matched"},
{ErrorType::FunctionNotFound, "Function Not Found"},
{ErrorType::EvalError,"Evaluate Error"},
{ErrorType::SyntaxError,"Syntax Error"}
};
const TokenLevel Calcer::tokenLevel{
{'+', 1},{'-', 1},
{'*', 2},{'/', 2},{'^', 2},{'%', 2},{'\\', 2},
{'(', 0},{')', 0}
};
const std::map<std::string, int> Calcer::NoteTable = {
{"+",2},{"-",2},
{"*",2},{"/",2},{"^", 2},{"%", 2},{"\\", 2},
{"sqrt",1}, {"abs",1},
{"sin",1},{"cos", 1}, {"tan", 1},
{"asin",1},{"acos", 1}, {"atan", 1},
{"ln",1},{"log", 1}, {"log2", 1},
{"floor", 1}, {"ceil", 1},
{"sign", 1},
{"PI", 0},
{"E", 0},
};
Calcer::Calcer(QObject *parent) : QObject(parent)
{
}
std::string Calcer::calc(std::string expression)
{
Error e;
const auto res = reversePolishNotation(expression, tokenLevel, e);
if (e.type != ErrorType::Well) {
return "[" + ErrorType2Name[e.type] + "]: " + e.msg + ".";
}
const auto val = evalNotation(res, e);
if (e.type != ErrorType::Well) {
return "[" + ErrorType2Name[e.type] + "]: " + e.msg + ".";
} else {
char buffer[100] = "";
sprintf(buffer,"%lf",val);
return std::string(buffer);
}
}
Notation Calcer::reversePolishNotation(const std::string &expression, const TokenLevel &tokenLevel, Calcer::Error &e)
{
Notation result;
std::vector<std::string::const_iterator> buffer;
std::stack<std::pair<std::string, std::string::const_iterator>> tmpFunc;
for (auto it = expression.begin(); it != expression.end();) {
auto ch = *it;
if (!checkNumber(it, expression)) {
if (checkFunc(it, expression)) {
tmpFunc.push({ readFunc(it, expression), it });
continue;
}
if (ch == '(' || buffer.empty()) {
buffer.push_back(it);
} else if (ch == ')') {
while (*buffer.back() != '(') {
result.push_back(std::string{ *buffer.back() });
buffer.pop_back();
}
if (!tmpFunc.empty() && buffer.back() == tmpFunc.top().second) {
result.push_back(tmpFunc.top().first); tmpFunc.pop();
}
buffer.pop_back();
} else {
auto search_res_ch = tokenLevel.find(ch);
auto search_res_back = tokenLevel.find(*buffer.back());
if (search_res_ch == tokenLevel.end()) { e = { ErrorType::SyntaxError, std::string({ch}) }; return result; }
if (search_res_back == tokenLevel.end()) { e = { ErrorType::SyntaxError, std::string({*buffer.back()}) }; return result; }
if (tokenLevel.at(ch) <= tokenLevel.at(*buffer.back())) {
while (!buffer.empty() && tokenLevel.at(ch) <= tokenLevel.at(*buffer.back())) {
result.push_back(std::string{ *buffer.back() });
buffer.pop_back();
}
}
buffer.push_back(it);
}++it;
} else result.push_back(readNumber(it, expression));
}
while (!buffer.empty()) {
result.push_back(std::string{ *buffer.back() });
buffer.pop_back();
}
return result;
}
bool Calcer::checkNumber(std::string::const_iterator &it, const std::string &expression)
{
return isdigit(*it)
|| ((it == expression.begin() || *(it - 1) == '(')
&& ((*it == '-' || *it == '+') && (it + 1) != expression.end()))
|| *it == '.' && (it + 1) != expression.end() && isdigit(*(it + 1));
}
std::string Calcer::readNumber(std::string::const_iterator &it, const std::string &expression)
{
std::string rtn;
while (it != expression.end() && checkNumber(it, expression))
if (*it != '+') rtn += *it++; else ++it;
return rtn;
}
bool Calcer::checkFunc(std::string::const_iterator &it, const std::string &expression)
{
return isalnum(*it);
}
std::string Calcer::readFunc(std::string::const_iterator &it, const std::string &expression)
{
std::string rtn;
while (it != expression.end() && checkFunc(it, expression))
rtn += *it++;
return rtn;
}
double Calcer::evalNotation(const Notation ¬ation, Calcer::Error &e)
{
if (notation.empty()) return INFINITY;
std::stack<double> resultStack;
for (const auto& note : notation) {
//cout << note << " ";
std::string::const_iterator it = note.begin();
if (checkNumber(it, note)) {
resultStack.push(atof(note.c_str()));
} else {
if (note == "(" || note == ")") {
e = { ErrorType::BracketNotMatched, note };
return INFINITY;
}
auto it = NoteTable.find(note);
if (it == NoteTable.end()) {
e = { ErrorType::FunctionNotFound, note }; return INFINITY;
}
const auto pCount = it->second;
const auto param = new double[pCount];
for (auto i = pCount - 1; i >= 0; i--) {
if (resultStack.empty()) { e = { ErrorType::SyntaxError, note }; return INFINITY; }
param[i] = resultStack.top(); resultStack.pop();
}
if (note == "hello") { printf("Hi!"); resultStack.push(0);}
else if (note == "PI") { resultStack.push(3.1415926537579);}
else if (note == "E") { resultStack.push(2.718281828);}
else if (note == "abs") resultStack.push(abs(param[0]));
else if (note == "sqrt") resultStack.push(sqrt(param[0]));
else if (note == "sin") resultStack.push(sin(param[0]));
else if (note == "cos") resultStack.push(cos(param[0]));
else if (note == "tan") resultStack.push(tan(param[0]));
else if (note == "asin") resultStack.push(asin(param[0]));
else if (note == "acos") resultStack.push(acos(param[0]));
else if (note == "atan") resultStack.push(atan(param[0]));
else if (note == "ln") resultStack.push(log(param[0]));
else if (note == "log") resultStack.push(log10(param[0]));
else if (note == "log2") resultStack.push(log2(param[0]));
else if (note == "floor") resultStack.push(floor(param[0]));
else if (note == "ceil") resultStack.push(ceil(param[0]));
else if (note == "sign") resultStack.push(abs(param[0]) < 1e-10 ? 0 : param[0] > 0 ? 1 : -1);
else if (note == "+") resultStack.push(param[0] + param[1]);
else if (note == "-") resultStack.push(param[0] - param[1]);
else if (note == "*") resultStack.push(param[0] * param[1]);
else if (note == "/") resultStack.push(param[0] / param[1]);
else if (note == "^") resultStack.push(pow(param[0], param[1]));
else if (note == "%") resultStack.push(fmod(param[0], param[1]));
delete[] param;
}
}
if (resultStack.size() > 1) {
e = { ErrorType::EvalError, "Can't Evaluate Correctly" };
}
return resultStack.top();
}