栈的应用——中缀表达式转后缀表达式

DS栈的应用——四则表达式

我们把平时所用的标准四则运算表达式,即 “9+ (3-1) ×3 + 10 ÷ 2” 叫做中缀表达式。
因为所有的运算符号都在两数字中间,现在我们要进行中缀到后缀的转化。
中缀表达式 “9+(3-1) × 3+10÷2” 转化为后缀表达式 “9 3 1 - 3* + 10 2 / +”
算法思想:从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分;若是符号,则判断其与栈顶符号的优先级,是右括号或优先级低于栈顶符号则栈顶元素依次出栈并输出,并将当前符号进栈,一直到最终输出后缀表达式为止。

代码实现:

void NiBolan(string str)
{
	stack<char>stk;
	char ch;
	for (int i = 0; i < str.length(); i++)
	{
		ch = str[i];
		int j = i;
		if (ch >= 48 && ch <= 57)
		{
			cout << ch;
			while (str[j + 1] >= 48 && str[j + 1] <= 57)
			{
				cout << str[++j];
			}
			i = j;
			cout << ' ';
			continue;
		}
		else
		{
			if (stk.empty())
			{
				stk.push(ch);
				continue;
			}
			else if (pairs[ch] > pairs[stk.top()])
			{
				stk.push(ch);
				continue;
			}
			else if (ch == '(')
			{
				stk.push(ch);
			}
			else if (ch == ')')
			{
				while (stk.top() != '(')
				{
					cout << stk.top() << ' ';
					stk.pop();
				}
				stk.pop();
			}
			else
			{
				if (stk.empty())
				{
					stk.push(ch);
					continue;
				}
				if (pairs[ch] >= pairs[stk.top()])
				{
					stk.push(ch);
					continue;
				}
				else
				{
					while (pairs[ch] <= pairs[stk.top()])
					{
						cout << stk.top() << ' ';
						stk.pop();
						if (stk.empty())
							break;
					}
					stk.push(ch);
				}
			}
		}
	}
	while (!stk.empty())
	{
		if (stk.size() > 1)
			cout << stk.top() << ' ';
		else
			cout << stk.top();
		stk.pop();
	}
	cout << endl;
}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
中缀表达式转后缀表达式的过程可以通过来实现。具体步骤如下: 1. 初始化两个,一个用于存储运算符的OPTR,一个用于存储转换后的后缀表达式OPND。 2. 从左到右扫描中缀表达式,如果遇到操作数,则直接压入OPND中;如果遇到运算符,则与OPTR顶元素进行比较,如果该运算符优先级高于OPTR顶元素,则将该运算符压入OPTR中;否则,将OPTR顶元素弹出并压入OPND中,直到该运算符优先级高于OPTR顶元素或OPTR为空,然后将该运算符压入OPTR中。 3. 如果遇到左括号,则直接压入OPTR中;如果遇到右括号,则将OPTR顶元素弹出并压入OPND中,直到遇到左括号,然后将左括号弹出。 4. 当扫描完整个中缀表达式后,将OPTR中剩余的运算符依次弹出并压入OPND中。 5. 最后,将OPND中的元素依次弹出,即可得到转换后的后缀表达式。 下面是一个示例代码,供参考: ``` #include <iostream> #include <stack> #include <string> using namespace std; int priority(char op) { if (op == '+' || op == '-') { return 1; } else if (op == '*' || op == '/') { return 2; } else { return 0; } } string infixToPostfix(string infix) { stack<char> optr; string postfix = ""; for (int i = 0; i < infix.length(); i++) { char ch = infix[i]; if (isdigit(ch)) { postfix += ch; } else if (ch == '(') { optr.push(ch); } else if (ch == ')') { while (optr.top() != '(') { postfix += optr.top(); optr.pop(); } optr.pop(); } else { while (!optr.empty() && priority(ch) <= priority(optr.top())) { postfix += optr.top(); optr.pop(); } optr.push(ch); } } while (!optr.empty()) { postfix += optr.top(); optr.pop(); } return postfix; } int main() { string infix = "1+(2-3)*4+10/5"; string postfix = infixToPostfix(infix); cout << postfix << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值