这是数据结构老师在课上讲的例题,具体教程可以看我上传的ppt,问题要求如下:
输入一串字符串,为十以内带括号的加减乘除,输出后缀表达式和结果。
解决思路:
先将字符串转化为后缀表达式,然后利用后缀表达式得出结果。
中缀表达式转化为后缀表达式
当中缀表达式中有括号时,
后缀表达式计算结果:
代码实现:
#include "pch.h"
#include <iostream>
#include<string>
#include<stack>
using namespace std;
int getPrior(char c)
{
switch (c)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
case '(':
return 3;
case ')':
return 3;
default:
return 0;
}
}
string outputString(string input)
{
string numbers = "";
stack<char> oper;
for (in