首先把中缀表达式转为后缀表达式,
1+4*2
如图所示,你需要准备两个栈,左边的栈用来装后缀表达式,右边的栈用来装运算符。
运算符进入左边栈的规则如下,
低进制见高进制时,立即反应。
比如+-一出现就把右边已有的全部进栈,自己独待右栈。
如果*号出现,并且前边紧贴着^,那么^也要出栈。但不pop 其他的。
置于括号,左括号没感觉,右括号出现的时候,把两括号夹着的运算符赶入栈。
就这样就行了。
#include <iostream>
#include <math.h>
using namespace std;
struct num_strT{
int num;
char s;
bool status;//status ==0 represent s, else num;
};
class screenC{
//用于接受后缀表达式
public:
int total,status;
num_strT what_on_screen[30];
void insert(char symbol)
{
what_on_screen[total].s=symbol;
what_on_screen[total].status=0;
total++;
}
void insert(int n)
{
what_on_screen[total].num=n;
what_on_screen[total].status=1;
total++;
}
void print()
{
for(int i=0;i<total;i++){
if(what_on_screen[i].status==0)
{
cout<<what_on_screen[i].s;
}
else
{
cout<<what_on_screen[i].num;
}
}
}
};
class zhanC{
public:
char zhan[30];
int total;
int num_of_brace;
void push(char symbol)
{
zhan[total]=symbol;
total+=1;
if(symbol=='(')num_of_brace++;
}
char pop()
{
total-=1;
return zhan[total];
}
};
class answer_zhanC{
public:
int zhan[30];
int total;
void push(float n)
{
zhan[total]=n;
total+=1;
}
int pop()
{
total-=1;
return zhan[total];
}
};
int main(){
screenC screen;
zhanC zhan;
answer_zhanC answer;
screen.total=0;
zhan.total=0;
answer.total=0;
zhan.num_of_brace=0;
char s[26],*p;
int temp,brace_left=0,brace_right=0;
int a,b;
bool button_minus,button_minusnum;
cin>>s;
p=s;
while(*p)
{
if(*p=='(')brace_left+=1;
else if(*p==')')brace_right+=1;
p++;
}
if(brace_left!=brace_right)
{
cout<<"Error";
return 0;
}
p=s;
while(*p!='\0')
{
//*P 1, + - * / ( )
if(*p>='0' &&*p<='9')
{
temp=0;
while(*p>='0'&&*p<='9')
{
temp=temp*10+*p-'0';
p++;
}
button_minus=false;
if(button_minusnum==true){button_minusnum=false;temp=-temp;}
screen.insert(temp);
}
else if(*p=='+'||*p=='-')
{
if(*p=='+')button_minus=true;
if(*p=='-'&&button_minus==true)
{
button_minusnum=true;
button_minus=false;
}
else{
if(zhan.num_of_brace==0)
{
while(zhan.total!=0)
{
screen.insert(zhan.pop());
}
}
zhan.push(*p);
}
p++;
}
else if(*p=='*'||*p=='/'||*p=='(')
{
if(*p!='('&&zhan.zhan[zhan.total-1]=='^')
{
screen.insert(zhan.pop());
}
if(*p=='/'&&zhan.zhan[zhan.total-1]=='*')
{
screen.insert(zhan.pop());
}
button_minus=true;
zhan.push(*p);
p++;
}
else if(*p==')')
{
button_minus=false;
char the_last_pop=zhan.pop();
while(the_last_pop!='(')
{
screen.insert(the_last_pop);
the_last_pop=zhan.pop();
}
zhan.num_of_brace-=1;
p++;
}
else if(*p=='^')
{
button_minus=true;
zhan.push(*p);
p++;
}
}
while(zhan.total!=0)
{
screen.insert(zhan.pop());
}
for(int i=0;i<screen.total;i++)
{
if(screen.what_on_screen[i].status==1)
{
//此时为数字
answer.push(screen.what_on_screen[i].num);
}
else
{
//此时为字符
switch(screen.what_on_screen[i].s)
{
case '+':b=answer.pop();a=answer.pop();answer.push(a+b);break;
case '-':b=answer.pop();a=answer.pop();answer.push(a-b);break;
case '*':b=answer.pop();a=answer.pop();answer.push(a*b);break;
case '/':b=answer.pop();a=answer.pop();if(b!=0){answer.push(a/b);}else{cout<<"Error";return 0;}break;
case '^':b=answer.pop();a=answer.pop();answer.push(pow(a,b));break;
}
}
}
cout<<int(answer.pop());
}