-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.h
120 lines (113 loc) · 3.29 KB
/
calculator.h
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
//
// Created by yindong on 19-6-2.
//
#ifndef SRC_CALCULATOR_H
#define SRC_CALCULATOR_H
#include<string>
#include<stack>
#include<array>
#include<iostream>
#include<cctype>
using namespace std;
class Calculator {
public:
int op2card(char ch){
int card = -1;
if(ch == '+')
card = 0;
else if(ch == '-')
card = 1;
else if(ch == '*')
card = 2;
else if(ch == '/')
card = 3;
else
cout << "Error!!!" << endl;
return card;
}
// + - * /
// a[i][j] = 1 if priority of op[i] >= op[j]
int evaluate(char op, int a, int b){
int res = 0;
switch(op){
case '+':{
res = a + b;
break;
}
case '-':{
res = a - b;
break;
}
case '*':{
res = a * b;
break;
}
case '/':{
res = a / b;
break;
}
default:{
cout << "Error!!!" << endl;
}
}
return res;
}
array<array<bool , 4>, 4> priority = {{
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 1, 1},
{1, 1, 1, 1}
}};
int calculate(string s) {
stack<char> op_stack;
stack<int> num_stack;
int a, b, res;
char op;
string num_str;
auto ptr = s.begin();
while(ptr != s.end()){
if( *ptr == ' '){
ptr++;
}
else if( *ptr == '+' || *ptr == '-' || *ptr == '*' || *ptr == '/'){
if(op_stack.empty() or not priority[op2card(op_stack.top())][op2card(*ptr)]){
op_stack.push(*ptr);
// cout << "operator stack push: " << *ptr << endl;
ptr++;
}
else if(priority[op2card(op_stack.top())][op2card(*ptr)]){
b = num_stack.top(); num_stack.pop();
a = num_stack.top(); num_stack.pop();
// cout << "num stack pop: " << a << b << endl;
op = op_stack.top(); op_stack.pop();
// cout << "op stack pop: " << op << endl;
res = evaluate(op, a, b);
num_stack.push(res);
// cout << "num stack push: " << res << endl;
}
}
else if(isdigit(*ptr)){
while(ptr != s.end() and isdigit(*ptr)){
num_str.push_back(*ptr);
ptr++;
}
num_stack.push(stoi(num_str));
num_str.clear();
// cout << "num stack push: " << num_str << endl;
}
else{
cout << "Error!!!" << endl;
}
}
while(not op_stack.empty()){
op = op_stack.top(); op_stack.pop();
b = num_stack.top(); num_stack.pop();
a = num_stack.top(); num_stack.pop();
num_stack.push(evaluate(op, a, b));
}
// cout << "Size of number stack: " << num_stack.size() << endl;
// cout << "Size of operator stack: " << op_stack.size() << endl;
return num_stack.top();
}
};
#endif //SRC_CALCULATOR_H