-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCalculatorClass.cpp
More file actions
174 lines (160 loc) · 4.75 KB
/
CustomCalculatorClass.cpp
File metadata and controls
174 lines (160 loc) · 4.75 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
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <fstream>
#include <sstream>
#include <stack>
using namespace std;
ifstream inf("/home/goodzone/CLionProjects/Calculator/input.txt");
ofstream ouf("/home/goodzone/CLionProjects/Calculator/output.txt");
vector<string> adv_tokenizer(std::string s, char del)
{
std::stringstream ss(s);
std::string word;
std::vector<std::string> v;
while (!ss.eof()) {
getline(ss, word, del);
v.push_back(word);
}
return v;
}
void print_queue(std::queue<char> q)
{
while (!q.empty())
{
std::cout << q.front() << " ";
q.pop();
}
std::cout << std::endl;
}
inline int intDivEx (int numerator, int denominator) {
if (denominator == 0)
throw std::overflow_error("Divide by zero exception");
return numerator / denominator;
}
void calculate_expression(queue<char> & outp, map<char, pair<int, char>> map) {
stack<int> stack;
while(!outp.empty())
{
char c = outp.front();
outp.pop();
switch(c)
{
case '+':
{
int rhs=stack.top();
stack.pop();
int lhs=stack.top();
stack.pop();
int result=lhs+rhs;
stack.push(result);
break;
}
case '-':
{
int rhs=stack.top();
stack.pop();
int lhs=stack.top();
stack.pop();
int result;
result=lhs-rhs;
stack.push(result);
break;
}
case '*':
{
int rhs=stack.top();
stack.pop();
int lhs=stack.top();
stack.pop();
int result=lhs*rhs;
stack.push(result);
break;
}
case '/':
{
int rhs = stack.top();
stack.pop();
int lhs = stack.top();
stack.pop();
int result;
try { result = intDivEx(lhs, rhs); }
catch (const overflow_error e) { ouf << "ERROR" << endl;
return;}
stack.push(result);
break;
}
default:
int number=(c-'0');
stack.push(number);
break;
}
}
ouf << stack.top() << endl;
}
void Shunting_yard_algorithm(string s, map<char, pair<int, char>> map) {
stack<char> operations;
queue<char> outp;
for (char & it : s) {
if (isdigit(it))
outp.push(it);
else if (it == '+' || it == '-' || it == '*' || it == '/') {
if (!operations.empty())
while (operations.top() != '(' && ( (map[operations.top()].first > map[it].first) ||
(map[operations.top()].first == map[it].first and
map[it].second == 'L'))) {
char value = operations.top();
operations.pop();
outp.push(value);
if (operations.empty())
break;
}
operations.push(it);
}
else if (it == '(') {
operations.push(it);
}
else if (it == ')') {
if (!operations.empty())
while (operations.top() != '(') {
char value = operations.top();
operations.pop();
outp.push(value);
if (operations.empty())
break;
}
// Удаляем евые скобки
if (!operations.empty())
operations.pop();
}
}
while (!operations.empty()) {
// assert the operator on top of the stack is not a parenthesis
char value = operations.top();
operations.pop();
outp.push(value);
}
// print_queue(outp);
calculate_expression(outp, map);
}
int main() {
string s;
map<char, pair<int, char>> operations;
if (inf.is_open()) {
// свойства операций
for (int i=0; i<4; i++) {
getline(inf, s);
auto v = adv_tokenizer(s, ' ');
char oper_code = v[0][0];
char oper_type = v[2][0]; // R - ассоциативность справа, L - слева
int oper_priority = atoi(v[1].c_str());
operations[oper_code] = make_pair(oper_priority, oper_type);
}
getline(inf, s);
Shunting_yard_algorithm(s, operations);
}
else {
cout << "Input file doesn't exists!" << endl;
}
}