-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication1.cpp
More file actions
160 lines (151 loc) · 2.8 KB
/
ConsoleApplication1.cpp
File metadata and controls
160 lines (151 loc) · 2.8 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
stack<char> opt; //Operator stack
stack<double> val; //The operand stack
char opt_set[10] = "+-*/()=.";
bool in_set(char theChar) {
for (int i = 0; i < 8; i++) {
if (theChar == opt_set[i])
return true;
}
return false;
}
int level(char theOpt)
{
switch (theOpt) {
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case ')':
return 3;
}
}
bool del_space(string& theString)
{
string res;
for (int i = 0; i < theString.length(); i++) {
if (in_set(theString[i]) || isdigit(theString[i]))
res += theString[i];
else if (theString[i] == ' ') {}
else {
cout << "The enter is Wrong" << endl;
return false;
}
}
theString = res;
return true;
}
const int IN = 0; // in the numbers
const int OUT = 1; // outside the numbers
bool change(string& from, string& to)
{
int theInt = 0;
int state = OUT;
char c;
for (int i = 0; i < from.length(); i++)
{
c = from[i];
if (isdigit(c)) {
to = to + c;
theInt *= 10;
theInt += c - '0';
state = IN; //The status is changed to within the number
}
else {
if (state == IN && c == '.') {
to = to + '.';
theInt = 0;
continue;
}
if (state == IN && c != '.') {
to += ' ';
theInt = 0;
}
if (c == '=')
break;
else if (c == '(')
opt.push(c);
else if (c == ')') {
while (!opt.empty() && opt.top() != '(') {
to += opt.top();
to += ' ';
opt.pop();
}
opt.pop();
}
else {
while (true) {
if (opt.empty() || opt.top() == '(')
opt.push(c);
else if (level(c) > level(opt.top()))
opt.push(c);
else {
to += opt.top();
to += ' ';
opt.pop();
continue;
}
break;
}
}
state = OUT; //The status is outside the number
}
}
while (!opt.empty()) {
to += opt.top();
to += ' ';
opt.pop();
}
return true;
}
bool compute(string& theExp)
{
int theInt = 0; //Temporary number
int state = OUT;//The initial state is outside the number
char c;
bool dot = false;
double count = 1.0;
for (int i = 0; i < theExp.length(); i++)
{
c = theExp[i];
if (isdigit(c) || c == '.') {
if (isdigit(c)) {
theInt *= 10;
theInt += c - '0';
state = IN; //The status is within the number
if (dot == true)
count *= 10.0;
}
if (c == '.') {
dot = true;
continue;
}
}
else {
dot = false;
double ans = theInt / count;
count = 1.0;
if (state == IN) {
val.push(ans);
theInt = 0;
}
double x, y;
if (c != ' ') {
x = val.top(); val.pop();
y = val.top(); val.pop();
switch (c) {
case '+':val.push(x + y); break;
case '-':val.push(y - x); break;
case '*':val.push(x * y); break;
case '/':val.push(y / x); break;
default:cout << "Unknown error!" << endl;
}
}
state = OUT;
}
}
return true;
}