中缀表达式转后缀表达式与逆波兰表达式求值

一、中缀表达式转后缀表达式

用map容器或者pair对组对运算符设定优先级
遍历字符串,数字直接加入后缀表达式字符串,否则入栈,当遇到’)'时,则必然已入左括号,此时出栈,将栈中元素加入数组中,直到遇到左括号,并将左括号pop掉
遍历完字符串后,将栈中剩余元素加入后缀字符串。

string change(string&s)
{
	stack<char>st;
	string res="";
	unordered_map<char, int>mp;
	mp['+'] = 1, mp['-'] = 1, mp['*'] = 2, mp['/'] = 2;//设置优先级
	for (auto i : s)
	{
		if (isdigit(i))
			res += i;
		else
		{
			if (st.empty())           
				st.push(i);
			else if (i == '(')   
				st.push(i);
			else if (i == ')')//遇到右括号出栈
			{
				while (st.top() != '(')//直到匹配左括号
				{
					res += st.top();
					st.pop();
				}
				st.pop();
			}
			else
			{
				while (!st.empty() && mp[i] <= mp[st.top()])
				{
					res += st.top();
					st.pop();
				}
				st.push(i);
			}
		}
	}
	while (!st.empty())
	{
		res += st.top();
		st.pop();
	}
	cout << res << endl;
	return res;
}

二、逆波兰表达式求值

遍历后缀字符串,当遇到运算符时,取栈中数据进行相关运算并将结果入栈,其余则将字符转化为数字并入栈,栈中最后剩余的一个元素即为运算结果。

int calc(string str)
{
	stack<int>s;
	for (auto i : str)
	{
		if (i == '+' || i == '-' || i == '*' || i == '/')
		{
			int num1 = s.top();
			s.pop();
			int num2 = s.top();
			s.pop();
			switch (i)
			{				   
			case '+':s.push(num2 + num1); break;//注意运算顺序
			case '-':s.push(num2 - num1); break;
			case '*':s.push(num2*num1); break;
			case '/':s.push(num2 / num1); break;
			}
		}
		else
			s.push(i-'0');
	}
	return s.top();
}

这里存在一个问题,仅支持个位数运算,用多位数运算需要将每个数据元素做一个字符串,

三、完整代码

#include<iostream>
#include<string>
#include<stack>
#include<unordered_map>
using namespace std;
string change(string&s)
{
	stack<char>st;
	string res="";
	unordered_map<char, int>mp;
	mp['+'] = 1, mp['-'] = 1, mp['*'] = 2, mp['/'] = 2, mp['('] = 0;
	for (auto i : s)
	{
		if (isdigit(i))
			res += i;
		else
		{
			if (s.empty())           
				st.push(i);
			else if (i == '(')   
				st.push(i);
			else if (i == ')')
			{
				while (st.top() != '(')
				{
					res += st.top();
					st.pop();
				}
				st.pop();
			}
			else
			{
				while (!st.empty() && mp[i] <= mp[st.top()])
				{
					res += st.top();
					st.pop();
				}
				st.push(i);
			}
		}
	}
	while (!st.empty())
	{
		res += st.top();
		st.pop();
	}
	cout << res << endl;
	return res;
}
int calc(string str)
{
	stack<int>s;
	for (auto i : str)
	{
		if (i == '+' || i == '-' || i == '*' || i == '/')
		{
			int num1 = s.top();
			s.pop();
			int num2 = s.top();
			s.pop();
			switch (i)
			{				   
			case '+':s.push(num2 + num1); break;
			case '-':s.push(num2 - num1); break;
			case '*':s.push(num2*num1); break;
			case '/':s.push(num2 / num1); break;
			}
		}
		else
			s.push(i-'0');
	}
	return s.top();
}
int main()
{
	string str;
	cin >> str;
	cout << calc(change(str));
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值