逆波兰算法

自己动手写了一下,不解释,代码如下:

#include<iostream>
#include<stack>
#include<string>
using namespace std;
class Solution
{
public:
	int prior(char op)//运算优先级
	{
		if (op == '+' || op == '-')
			return 1;
		if (op == '*' || op == '/')
			return 2;
		return 0;
	}
	string middle2last(string middle)//中缀表达式转后缀表达式
	{
		stack<char> s;
		string ans;
		for (int i = 0; i < middle.size(); i++)
		{
			char c = middle[i];
			if (c >= '0'&&c <= '9')
			{
				ans.append(1, c);
				while (i + 1 < middle.size() && isdigit(middle[i + 1]))
					ans.append(1, middle[++i]);

				ans.append(1, '#');
			}
			else
			{
				if (c == '(')
					s.push('(');
				else
				{
					if (c == ')')
					{
						while (s.top() != '(')
						{
							ans.append(1, s.top());
							s.pop();
						}
						s.pop();
					}
					else
					{
						if (s.empty())
						{
							s.push(c);
						}
						else
						{
							if (prior(c) > prior(s.top()))
								s.push(c);
							else
							{
								while (!s.empty() && prior(c) <= prior(s.top()))
								{
									ans.append(1, s.top());
									s.pop();
								}
								s.push(c);
							}
						}
					}
				}
			}
		}
		while (!s.empty())
		{
			ans.append(1, s.top());
			s.pop();
		}
		return ans;
	}
	int deal_last(string last)//遇到数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈
	{
		stack<int>num_stack;
		for (int i = 0; i < last.size(); i++)
		{
			if (last[i] >= '0'&&last[i] <= '9')
			{
				int temp = last[i] - '0';
				while (last[++i] != '#')
				{
					temp = temp * 10 + last[i] - '0';
				}
				num_stack.push(temp);
			}
			else
			{
				if (last[i] == '+')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second + first);
				}
				else if (last[i] == '-')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second - first);
				}
				else if (last[i] == '*')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second * first);
				}
				else if (last[i] == '/')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second / first);
				}
			}


		}
		return num_stack.top();
	}
};
int main()
{
	Solution sloution;
	string middle("9+(3-1)*3+10/2"), last;
	
	last = sloution.middle2last(middle);
	cout << "后缀表达式:" << endl;
	for (int i = 0; i < last.size(); i++)
	{
		cout << last[i];
	}
	cout << endl;
	cout << "运算结果:" << endl;
	cout << sloution.deal_last(last) << endl;
	system("pause");
	return 0;
}

上面都编出来了,这道题就更不在话下了。
在这里插入图片描述

class Solution {
public:
	int prior(char op)
	{
		if (op == '+' || op == '-')
			return 1;
		if (op == '*' || op == '/')
			return 2;
		return 0;
	}
	string middle2last(string middle)//中缀表达式转后缀表达式
	{
		stack<char> s;
		string ans;
		for (int i = 0; i < middle.size(); i++)
		{
			if (middle[i] == ' ')continue;
			char c = middle[i];
			if (c >= '0'&&c <= '9')
			{
				ans.append(1, c);
				while (i + 1 < middle.size() && isdigit(middle[i + 1]))
					ans.append(1, middle[++i]);

				ans.append(1, '#');
			}
			else
			{
				if (c == '(')
					s.push('(');
				else
				{
					if (c == ')')
					{
						while (s.top() != '(')
						{
							ans.append(1, s.top());
							s.pop();
						}
						s.pop();
					}
					else
					{
						if (s.empty())
						{
							s.push(c);
						}
						else
						{
							if (prior(c) > prior(s.top()))
								s.push(c);
							else
							{
								while (!s.empty() && prior(c) <= prior(s.top()))
								{
									ans.append(1, s.top());
									s.pop();
								}
								s.push(c);
							}
						}
					}
				}
			}
		}
		while (!s.empty())
		{
			ans.append(1, s.top());
			s.pop();
		}
		return ans;
	}
	int deal_last(string last)//遇到数字就进栈,遇到是符号,就将处于栈顶两个数字出栈,进行运算,运算结果进栈
	{
		stack<long int>num_stack;
		for (int i = 0; i < last.size(); i++)
		{
			if (last[i] >= '0'&&last[i] <= '9')
			{
				long int temp = last[i] - '0';
				while (last[++i] != '#')
				{
					temp = temp * 10 + last[i] - '0';
				}
				num_stack.push(temp);
			}
			else
			{
				if (last[i] == '+')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second + first);
				}
				else if (last[i] == '-')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second - first);
				}
				else if (last[i] == '*')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second * first);
				}
				else if (last[i] == '/')
				{
					int first = num_stack.top();
					num_stack.pop();
					int second = num_stack.top();
					num_stack.pop();

					num_stack.push(second / first);
				}
			}


		}
		return num_stack.top();
	}
	int calculate(string s) {
		string last = middle2last(s);
		return deal_last(last);
	}
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值