-
Notifications
You must be signed in to change notification settings - Fork 104
/
5.6.cpp
84 lines (78 loc) · 2.26 KB
/
5.6.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
/*
* 题目名称:简单计算器
* 题目来源:浙江大学复试上机题
* 题目链接:http://t.cn/AiKoGS94
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <cctype>
#include <string>
#include <stack>
using namespace std;
int Priority(char c) { //优先级顺序 #,$,+-,*/
if (c == '#') {
return 0;
} else if (c == '$') {
return 1;
} else if (c == '+' || c == '-') {
return 2;
} else {
return 3;
}
}
double GetNumber(string str, int& index) { //获得下个数字
double number = 0;
while (isdigit(str[index])) {
number = number * 10 + str[index] - '0';
index++;
}
return number;
}
double Calculate(double x, double y, char op) {
double result = 0;
if (op == '+') {
result = x + y;
} else if (op == '-') {
result = x - y;
} else if (op == '*') {
result = x * y;
} else if (op == '/') {
result = x / y;
}
return result;
}
int main() {
string str;
while (getline(cin, str)) {
if (str == "0") {
break;
}
stack<char> operation; //运算符栈
stack<double> number; //运算数栈
str += '$'; //字符串尾部添加$
operation.push('#'); //运算符栈底添加#
int index = 0; //字符串下标
while (index < str.size()) {
if (str[index] == ' ') {
index++;
} else if (isdigit(str[index])) {
number.push(GetNumber(str, index));
} else {
if (Priority(operation.top()) < Priority(str[index])) {
operation.push(str[index]);
index++;
} else {
double y = number.top();
number.pop();
double x = number.top();
number.pop();
number.push(Calculate(x, y, operation.top()));
operation.pop();
}
}
}
printf("%.2f\n", number.top());
}
return 0;
}