表达式求值:
主要思路:
先将中缀表达式转换为后缀表达式 preTopost()函数
主要问题:两位数或者多位数怎么办?
答案:找到一个数字时,继续向后扫描,直到不是数字;将扫描到的拼成一个数字,在后缀字符串post中以#号分割,
计算的时候自动忽略#号即可。
代码如下:
//
//Author NEFUZYJ
//created time 2019-04-30
//title "表达式求值"
//
#include<iostream>
#include<stack>
#include<string>
using namespace std;
//转化为后缀表达式
string s;
char post[200];
int z = 0;
stack<int> num;
stack<char> opt;//算符栈
int getPriority(char ch)
{
if(ch == '*' ||ch =='/')
return 2;
if(ch == '+' ||ch == '-')
return 1;
return 0;
}
void preTopost()
{
int len = s.size();
int nowOrder = 0,topOrder = 0;
for(int i = 0; i < len; i++)
{
if(s[i]