C++用栈计算表达式

基本思路:

(1)设置两个栈,一个运算符栈,一个操作数栈。初始化后将"#"压入操作符栈中。

(2)顺序扫描,当输入为操作数时就将其压入操作数栈。当输入为运算符时,则比较输入运算符和运算符栈的栈顶运算符的优先级的大小。若输入运算符的优先级高于运算符栈顶运算符的优先级时,则将其压入到运算符栈;若运算符栈顶运算符的优先级高于输入运算符的优先级,则将栈中的运算符弹出并从操作数栈中弹出两个操作数进行运算,将运算结果作为操作数输出到操作数栈;然后重新比较输入运算符和更新后的栈顶运算符的优先级的大小。注意:‘(’进栈之前优先级最高,但进栈之后其优先级最低,只有’)‘可使其出栈。

(7)扫描结束后,通过判断操作符栈中是否只有’#‘判断是否计算完成。

举例:
例如计算表达式1+(2*3+4)/2


下面是代码:

#include<iostream>
#include<stack>
#include<string>
using namespace std;
int priority(int state,char a)
{//计算操作符优先级的函数,注意state表示运算符状态:
//state=1表示还未进栈,state=0表示栈内优先级,注意
//这只对‘(’起作用
	int result;
	switch (a){
	case '+':
	case '-':
		result = 1;
		break;
	case '*':
	case '/':
		result = 2;
		break;
	case '(':
		if (state == 0)
			result = 3;
		else
			result = 0;
		break;
	case '#':
		result = 0;
		break;
	default:
		break;
	}
	return result;
}
double calculate(char op, double op1, double op2)
{
	double result;
	switch (op){
	case '+':
		result = op1 + op2;
		break;
	case '-':
		result = op1 - op2;
		break;
	case '*':
		result = op1*op2;
		break;
	case '/':
		result = op1 / op2;
		break;
	default:
		break;
	}
	return result;
}
int main()
{
	string s;
	while (cin >> s){//测试多组数据
		stack<char> operation;//存放操作符的栈
		stack<double> operand;//存放操作数的栈
		operation.push('#');//先将‘#’压栈
		string num;//存放操作数
		for (int i = 0; i < s.length(); i++){
			if (isdigit(s[i])){//出现数字
				while (isdigit(s[i]) || s[i] == '.'){//将操作数提取完全
					num.push_back(s[i]);
					i++;
				}
				double a = atof(num.c_str());//string->double
				operand.push(a);//操作数入栈
				num.clear();//num清空以备下次使用
				i--;//位置还原
			}
			else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '('){//出现运算符
				if (priority(0, s[i])>priority(1, operation.top()))//优先级比较
					operation.push(s[i]);//>,直接入栈
				else{
					while (priority(0, s[i]) <= priority(1, operation.top())){//<,出栈并进行计算直至>
						char temp = operation.top();
						operation.pop();
						double op2 = operand.top();
						operand.pop();
						double op1 = operand.top();
						operand.pop();
						operand.push(calculate(temp, op1, op2));
					}
					operation.push(s[i]);//不要忘了最后操作符入栈
				}
			}
			else if (s[i] == ')'){//扫描到‘)’
				while (operation.top() != '('){//出栈直至‘(’
					char temp = operation.top();
					operation.pop();
					double op2 = operand.top();
					operand.pop();
					double op1 = operand.top();
					operand.pop();
					operand.push(calculate(temp, op1, op2));
				}
				operation.pop();//‘(’出栈
			}
			else{//非法字符的处理
				cout << "error!" << endl;
				return 0;
			}
		}
		while (operation.top() != '#'){//扫尾工作
			char temp = operation.top();
			operation.pop();
			double op2 = operand.top();
			operand.pop();
			double op1 = operand.top();
			operand.pop();
			operand.push(calculate(temp, op1, op2));
		}
		cout << operand.top() << endl;//输出结果
	}
	return 0;
}

运行结果:



  • 12
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值