大家可以参考这个大佬
//不过有几点需要更改一下
//根据题目是按空格中止 所以计算结果函数中的while应该以CH==’='为终止条件
//还有就是最后主函数修改一下
#include<iostream>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
stack<char> opter; //运算符栈
stack<double> opval; //操作数栈
int getIndex(char theta) //获取theta所对应的索引
{
int index = 0;
switch (theta)
{
case '+':
index = 0;
break;
case '-':
index = 1;
break;
case '*':
index = 2;
break;
case '/':
index = 3;
break;
case '(':
index = 4;
break;
case ')':
index = 5;
break;
case '=':
index = 6;
default:break;
}
return index;
}
char getPriority(char theta1, char theta2) //获取theta1与theta2之间的优先级
{
const char priority[][7] = //算符间的优先级关系
{
{ '>','>','<','<','<','>','>' },
{ '>','>','<','<','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '<','<','<','<','<','=','0' },
{ '>','>','>','>','0','>','>' },
{ '<','<','<','<','<','0','=' },
};
int index1 = getIndex(theta1);
int index2 = getIndex(theta2);
return priority[index1][index2];
}
double calculate(double b, char theta, double a) //计算b theta a
{
switch (theta)
{
case '+':
return b + a;
case '-':
return b - a;
case '*':
return b * a;
case '/':
return b / a;
default:
break;
}
}
double getAnswer() //表达式求值
{
opter.push('='); //首先将'='入栈opter
int counter = 0; //添加变量counter表示有多少个数字相继入栈,实现多位数的四则运算
char c = getchar();
while (c != '=' || opter.top() != '=') //终止条件
{
if (isdigit(c)) //如果c在'0'~'9'之间
{
if (counter == 1)/*counter==1表示上一字符
也是数字,所以要合并,比如12*12,要算12,
而不是单独的1和2*/
{
double t = opval.top();
opval.pop();
opval.push(t * 10 + (c - '0'));
counter = 1;
}
else
{
opval.push(c - '0'); //将c对应的数值入栈opval
counter++;
}
c = getchar();
}
else
{
counter = 0; //counter置零
switch (getPriority(opter.top(), c)) //获取运算符栈opter栈顶元素与c之间的优先级,用'>','<','='表示
{
case '<': //<则将c入栈opter
opter.push(c);
c = getchar();
break;
case '=':/* =将opter栈顶元素弹出,
用于括号的处理 */
opter.pop();
c = getchar();
break;
case '>': //>则计算
char theta = opter.top();
opter.pop();
double a = opval.top();
opval.pop();
double b = opval.top();
opval.pop();
opval.push(calculate(b, theta, a));
}
}
}
return opval.top(); //返回opval栈顶元素的值
}
int main()
{
double ans = getAnswer();
cout << ans;
return 0;
}