AlexandreLea @ 2021-03-19 00:00:13
#include <iostream>
#include <string>
using namespace std;
string inp;
struct op
{
bool type; //false is number,true is operator
union
{
int number;
char oper;
};
} sufx[1001], prfx[1001];
int r = 0, e = 0;
int Calc()
{
if (prfx[e].type == false)
{
cout<<"Calc get number!"<<prfx[e].number;
return prfx[e].number;
}
else
{
int x, y;
switch (prfx[e].oper)
{
case '+':
++e;
x = Calc();
++e;
y = Calc();
return x + y;
case '-':
++e;
x = Calc();
++e;
y = Calc();
return x - y;
case '*':
++e;
x = Calc();
++e;
y = Calc();
return x * y;
case '/':
++e;
x = Calc();
++e;
y = Calc();
return x / y;
}
}
}
int main()
{
cin >> inp;
int num = 0;
for (int i = 0; i <= inp.length(); i++)
{
if (inp[i] >= '0' && inp[i] <= '9')
{
num = num * 10 + inp[i] - '0';
}
else if (inp[i] == '.')
{
sufx[r].type = false;
sufx[r].number = num;
r++;
//Save number.
num = 0;
}
else if (inp[i] != '@')
{
sufx[r].type = true;
sufx[r].oper = inp[i];
++r;
//Save operator.
}
}
//So we want to the PREFIX.
for (int i = 0; i < r; i++)
{
prfx[i] = sufx[r - i - 1];
}
//And we can operation the end.
int d=Calc();
cout<<d<<endl;
return 0;
}
我的代码运行时没有Calc函数,怎么办?