C++中缀转后缀代码

中缀转后缀
Time Limit: 1000 MSMemory Limit: 1000 KB

Description

由小写字母{'a','b','c','d',...,'z'}和{'+','-','*','/','(',')'}可以组成一个中缀表达式,现在需要你输出它的后缀表达式。

Input

输入的第一行是一个int型整数T,表示一个有T组数据。
接下来T行,每行一个一个不含空格的字符串,且每个字符串长度都小于1000。该字符串就是中缀表达式。
数据保证输入的表达式一定合法。

Output

输出T行,每行一个后缀表达式,表示求得的结果。输出的表达式同样不含空格。

Sample Input

2
a+b*c+(d*e+f)*g
(a+b)*c/d+f/g

Sample Output

abc*+de*f+g*+
ab+c*d/fg/+

代码结果:

#include<iostream>
#include<stack>
using namespace std;
int priority(char a)
{
	if (a == ')')return 0;
	else if (a == '+' || a == '-')return 1;
	else if (a == '*' || a == '/')return 2;
	else if (a == '(')return 3;
	else return -1;
}
int in_priority(char a)
{
	if (a == '+' || a == '-')return 1;
	else if (a == '*' || a == '/')return 2;
	else if (a == '(')return 0;
	else return -1;
}
int main()
{
	int T = 0;
	cin >> T;
	for (int t = 0; t < T; t++)
	{
		char ch[1000];
		cin >> ch;
		stack<char>aba;
		int i = 0;
		while (ch[i] != ' '&&ch[i]!= '\0')
		{
			if (97 <= ch[i] && ch[i] <= 122)
			{
				cout<<ch[i];
			}
			else if(ch[i]==')')
			{
				for (; aba.top() != '('&& !aba.empty(); aba.pop())cout <<aba.top();
				aba.pop();
			 }
			else 
			{
				
				if(!aba.empty())
				while( in_priority(aba.top()) >= priority(ch[i]))
					{
					 cout << aba.top();
					 aba.pop();
					 if (aba.empty())break;
					 }
				aba.push(ch[i]);
					
			}
			i++;
		}
		for (; !aba.empty(); aba.pop())cout << aba.top();
		cout << endl;
		
	}

	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是 C++ 实现中缀表达式后缀表达式代码,使用了栈来存储运算符: ```cpp #include <iostream> #include <stack> #include <string> using namespace std; // 判断是否是运算符 bool isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'; } // 判断运算符优先级 int getPriority(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else if (op == '^') return 3; else return 0; } // 中缀表达式后缀表达式 string infixToPostfix(string infix) { stack<char> s; string postfix; for (char c : infix) { if (isalnum(c)) { postfix += c; } else if (c == '(') { s.push(c); } else if (c == ')') { while (!s.empty() && s.top() != '(') { postfix += s.top(); s.pop(); } s.pop(); // 弹出左括号 } else if (isOperator(c)) { while (!s.empty() && getPriority(s.top()) >= getPriority(c)) { postfix += s.top(); s.pop(); } s.push(c); } } while (!s.empty()) { postfix += s.top(); s.pop(); } return postfix; } int main() { string infix = "a+b*c-(d/e+f)*g"; string postfix = infixToPostfix(infix); cout << postfix << endl; // 输出 abc*+de/f+g*- return 0; } ``` 代码中,`isOperator` 函数用于判断字符是否为运算符,`getPriority` 函数返回运算符的优先级。`infixToPostfix` 函数从左至右遍历中缀表达式,对于每个字符进行如下操作: - 如果是字母或数字,直接加入后缀表达式; - 如果是左括号,入栈; - 如果是右括号,弹出栈顶元素直到遇到左括号,弹出左括号; - 如果是运算符,弹出栈中优先级不低于当前运算符的元素,直到遇到优先级更低的元素或栈为空,将当前运算符入栈; - 最后,将栈中所有元素依次加入后缀表达式即可。 例如,对于中缀表达式 `a+b*c-(d/e+f)*g`,运行 `infixToPostfix` 函数后得到的后缀表达式为 `abc*+de/f+g*-`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

convK

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

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

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

打赏作者

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

抵扣说明:

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

余额充值