int main()
{
char expression[100]; // 表达式字符串
do
{
cout << "请输入一个表达式(若要退出,请直接回车):";
cin.getline(expression, 100);
if (!strcmp(expression, "")) break;
strcat(expression, OPERATORS + NUM_OPRT - 1); //在尾部添加一个 '#'
cout << "表达式为: #" << expression << endl;
// TODO: 在此完成函数代码
SqStack OPTR, OPND;
InitStack(OPTR); Push(OPTR, '#');
InitStack(OPND);
char* p = expression;
while (*p != '\0'){
if (IsNumeric(*p)){
ElemType num = 0;
while (IsNumeric(*p)){
num = num * 10 + (*p - '0');
p++;
}
Push(OPND, num);
}
else if (IsOperator(*p)){
char top;
GetTop(OPTR, top);
int relation = Precede(top, *p);
if (relation == LT){
Push(OPTR, *p);
p++;
}
else if (relation == EQ){
Pop(OPTR, top);
p++;
}
else if (relation == GT){
char theta;
Pop(OPTR, theta);
ElemType b, a;
Pop(OPND, b);
Pop(OPND, a);
ElemType result = Operate(a, theta, b);
Push(OPND, result);
}
else{
cout << "表达式错误!" << endl;
break;
}
}
else{
cout << "非法字符!" << endl;
break;
}
}
ElemType result;
if (GetTop(OPND, result)){
cout << "计算结果: " << result << endl;
}
} while (true);
return 0;
}


