Infix to postfix without '(' and ')'

Posted KennyRom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Infix to postfix without '(' and ')'相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<stack>
#include<string>
#include<deque>
using namespace std;

char compare(char tp, char op)
{
	if (((tp == ‘+‘ || tp == ‘-‘) && (op == ‘*‘ || op == ‘/‘)) || tp == ‘#‘)
		return ‘<‘;
	return ‘>‘;
}

int main()
{
	stack<char>num;
	stack<char>oper;

	oper.push(‘#‘);

	string s;
	cin >> s;

	for (int i = 0; i<s.length(); i++)
	{
		if (s[i] == ‘0‘ || s[i] == ‘1‘ || s[i] == ‘2‘ || s[i] == ‘3‘ || s[i] == ‘4‘ || s[i] == ‘5‘ || s[i] == ‘6‘ || s[i] == ‘7‘ || s[i] == ‘8‘ || s[i] == ‘9‘)
			num.push(s[i]);
		else
		{
			char comp = compare(oper.top(), s[i]);
			if (comp == ‘<‘)
				oper.push(s[i]);
			else if (comp == ‘>‘)
			{
				num.push(oper.top());
				oper.pop();
				oper.push(s[i]);
			}
		}
	}

	while (oper.top() != ‘#‘)
	{
		num.push(oper.top());
		oper.pop();
	}

	deque<char> d;

	while (num.size() != 0)
	{
		d.push_front(num.top());
		num.pop();
	}


	for (auto i = d.begin(); i != d.end(); i++)
		cout << *i;

	cout << endl;

	return 0;
}

  

以上是关于Infix to postfix without '(' and ')'的主要内容,如果未能解决你的问题,请参考以下文章

如何在Postfix和Infix表示法中接受负值?

调用 `target_link_libraries(target_name, library_name_without_postfix)` 时,`Cmake` 更喜欢链接到哪个库?

Infix to posfix 自己写stack,没有()

How to configure postfix to send email via Gmail

How to configure postfix to send email via Gmail

在 SML Alice 中创建中缀/后缀/前缀解析器