
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
class stack {
int a[10];
public:
bool init();
bool pop(int *i);
bool push(int i);
static int index;
int cal();
};
int stack::index = 0;
bool stack::init() { index = 0; return true; }
bool stack::pop(int *i) {
if (index > 0) { *i = a[index - 1]; index--; return true; }
else
return false;
}
bool stack::push(int i) {
if (index < 10) { a[index] = i; index++; return true; }
else
return false;
}
int stack::cal() {
int
i = 0, num = 0; for (; i < index; i++) { num = num + a[i] * pow(10, index - i - 1); }return num;
}
class cal {
public:
stack buf;
int num[20];
char ch[20];
string str;
cal() { for (int i = 0; i < 20; i++) { num[i] = NULL; ch[i] = NULL; } }
void solve(int i, int j);//分解输入字符窜
int resolve(); //求出一个不含括号的混合算术表达式的值
int check_ch(); //返回运算中第一个乘号除号的位置,否则返回-1
};
int cal::check_ch() {
int i, j;
for (i = 0; ch[i] != NULL; i++);
j = 0;
while (ch[j] != NULL && ch[j] != '*'&&ch[j] != '/') { j++; }
if (j == i) return -1;
else return j;
}
void cal::solve(int i = 0, int j = 0) {
buf.init();
while (str[i] > 47 && str[i] < 58)
{
buf.push((int)str[i] - 48);
i++;
}
if (buf.index != 0)
num[j] = buf.cal();
if (i < str.length()) { ch[j] = str[i]; i++; j++; solve(i, j); }
}
int cal::resolve() {
solve(0, 0);
while (check_ch() != -1) {
if (ch[check_ch()] == '*') {
num[check_ch()] = num[check_ch()] * num[check_ch() + 1];
for (int j = check_ch(); num[j + 1] != NULL && j < 18; j++) { num[j + 1] = num[j + 2]; }
for (j = check_ch(); ch[j] != NULL && j < 19; j++) { ch[j] = ch[j + 1]; }
}
if (ch[check_ch()] == '/') {
num[check_ch()] = num[check_ch()] / num[check_ch() + 1];
for (int j = check_ch(); num[j + 1] != NULL && j < 18; j++) { num[j + 1] = num[j + 2]; }
for (j = check_ch(); ch[j] != NULL && j < 19; j++) { ch[j] = ch[j + 1]; }
}
}
while (ch[0] != NULL) {
if (ch[0] == '+') {
num[0] = num[0] + num[1];
for (int j = 0; num[j + 1] != NULL && j < 18; j++) { num[j + 1] = num[j + 2]; }
for (j = 0; ch[j] != NULL && j < 19; j++) { ch[j] = ch[j + 1]; }
}
if (ch[0] == '-') {
num[0] = num[0] - num[1];
for (int j = 0; num[j + 1] != NULL && j < 18; j++) { num[j + 1] = num[j + 2]; }
for (j = 0; ch[j] != NULL && j < 19; j++) { ch[j] = ch[j + 1]; }
}
}
return num[0];
}
void main() {
L:cal a;
cin >> a.str;
cout << a.resolve() << endl;
goto L;
}
近期评论