c++中缀表达式转后缀表达式-实现四则运算

c++实现带括号的四则运算

计算表达式求值时,先将表达式由中缀表达式转换为后缀表达式,再进行运算求值

比如:9+(3-1)*3+10/2,如果用后缀表示法就是9 3 1 - 3 * + 10 2 / +,这样的表达式称为后缀表达式,叫后缀的原因在于所有的符号都是要在运算数字的后面出现。后缀表达式的计算方式

为了解释后缀表达式的好处,我们先来看看,计算机是如何计算后缀表达式的。
后缀表达式 9 3 1 - 3 * + 10 2 / +

规则:从左到右遍历表达式的每个数字和符号,遇到数字就进栈,遇到是符号,就将处于栈顶的两个数字出栈,进行计算,然后计算结果进栈,一直到最终获得结果。

中缀表达式转后缀表达式我们把平时标准的四则运算表达式比如9+(3-1)*3+10/2叫做中缀表达式。
中缀表达式:9+(3-1)*3+10/2转换后缀表达式: 9 3 1 - 3 * + 10 2 / +
规则:

1、当当前字符为数字时,直接输出;
2、当当前字符为”(“时,将其压栈;
3、当当前字符为”)”时,则弹出堆栈中最上的”(“之前的所有运算符并输出,然后删除堆栈中的”(” ;
4、当当前字符为运算符时,则依次弹出堆栈中优先级大于等于当前运算符的(到”(“之前为止),输出,再将当前运算符压栈;
最后弹出所有栈中的内容输出

#include<iostream>
#include<stack>
#include<string>
#include<queue>
#include<map>
using namespace std;

struct node
{
	double num;
	char op;
	bool flag;//true 数字,false 字符
};
int Convert();
int main() {
	Convert();
	system("pause");
	return 0;
}

int Convert() {

	map<char, int> m;
	m['+'] = m['-'] = 1;
	m['*'] = m['/'] = 2;

	string s2 = "1+2*3-9/3";
	string s1 = "9+(3-1)*3+10/2";
	string s3 = "1+(3-1)*2+4";

	queue<node> q;//存放后缀表达式
	stack<char> op;//临时存放操作符
	for (int i = 0; i < s1.length();) {
		//cout << "s1[i]: " << s1[i] << endl;
		node temp;
		//cout << s1[i];
		if (s1[i] >= '0'&&s1[i] <= '9') {
			temp.num = s1[i++] - '0';
			temp.flag = true;
			
			
			while (i < s1.length()&&s1[i] >= '0'&&s1[i] <= '9') {
				temp.num = temp.num * 10 + (s1[i] - '0');
				i++;
			}
			q.push(temp);
			cout << temp.num << " ";
		}
		else {
			//cout << s1[i] << endl;
			if (op.empty() || s1[i] == '(') {
				op.push(s1[i]);
			}
			else if (s1[i] == ')') {
				while (op.top() != '(') {
					temp.op = op.top();
					temp.flag = false;
					q.push(temp);
					cout << op.top() << " ";
					op.pop();
					
				}
				op.pop();
			}
			else if (m[s1[i]]>m[op.top()]) {//弹出>=当前op
				op.push(s1[i]);
				
			}
			else if (m[s1[i]] <= m[op.top()]) {
				while (!op.empty() && m[s1[i]] <= m[op.top()]&&op.top()!='(') {
					temp.op = op.top();
					temp.flag = false;
					q.push(temp);
					cout << op.top() << " ";
					op.pop();
					
				}
				op.push(s1[i]);
			}
			i++;
		}
	}
	int op_size = op.size();
	for (int i = 0; i < op_size; i++) {
		cout << op.top() << " ";
		node temp;
		temp.op = op.top();
		temp.flag = false;
		q.push(temp);
		op.pop();
	}


	/*cout << "后缀表达式: " << endl;
	int q_size = q.size();
	for (int i = 0; i < q_size; i++) {
		if (q.front().flag == true)
			cout << q.front().num << " ";
		else cout << q.front().op << " ";
		q.pop();
	}*/
	//计算

	int q_size = q.size();
	cout << q_size << endl;
	stack<double> num;//存放数字
	for (int i = 0; i < q_size; i++) {
		if (q.front().flag == true) {
			num.push(q.front().num);
			q.pop();
		}
		else {
			double t1, t2;
			t2 = num.top();
			num.pop();
			t1 = num.top();
			num.pop();

			if (q.front().op == '+') num.push(t1 + t2);
			else if (q.front().op == '-') num.push(t1 - t2);
			else if (q.front().op == '*') num.push(t1 * t2);
			else if (q.front().op == '/') num.push(t1 / t2);

			q.pop();

		}
	}
	cout <<"result: " << num.top() << endl;
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值