HDU1237
相关操作可以去看一下后缀表达式
这俩大同小异
代码:
#include <bits/stdc++.h>
using namespace std;
inline void calculate(string str) {
double n = 0.0;
double num[200], st[200];
char ch[200], st_ch[200];
int step_n = 0, step_c = 0;
char save = '0';
for (int i = 0; i < str.size(); i++) {
if (str[i] >= '0' && str[i] <= '9') {
n = n * 10 + str[i] - '0';
} else {
if (str[i] != ' ') {
num[step_n++] = n;
n = 0.0;
ch[step_c++] = str[i];
i++;
}
}
}
num[step_n] = n;
st[0] = num[0];
step_n = 0;
for (int i = 0; i < step_c; i++) {
if (ch[i] == '+' || ch[i] == '-') {
st_ch[step_n++] = ch[i];
st[step_n] = num[i + 1];
} else {
if (ch[i] == '*') {
st[step_n] *= num[i + 1];
} else {
st[step_n] /= num[i + 1];
}
}
}
n = st[0];
for (int i = 0; i < step_n; i++) {
if (st_ch[i] == '+') {
n += st[i + 1];
} else {
n -= st[i + 1];
}
}
printf("%.2f\n", n);
}
int main() {
string str;
while (getline(cin, str), str != "0") {
calculate(str);
}
return 0;
}