思路还是很简单的 就是数字与字符的出栈入栈
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define SElemtype char
typedef struct{
SElemtype *base;
SElemtype *top;
int stacksize;
}Sqstack;
typedef struct{
int *base;
int *top;
int stacksize;
}Sqstack1;
int initSqstack(Sqstack &optr)//运算符栈初始化
{
optr.base=(SElemtype *)malloc(STACK_INIT_SIZE*sizeof(SElemtype));
if(!optr.base)
exit(-1);
optr.top=optr.base;
optr.stacksize=STACK_INIT_SIZE;
return 1;
}
int InitSqstack1(Sqstack1 &opnd)//数字栈初始化
{
opnd.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!opnd.base)
exit(-1);
opnd.top=opnd.base;
opnd.stacksize=STACK_INIT_SIZE;
return 1;
}
int Push(Sqstack1 &opnd,int r)//数字入栈
{
if(opnd.top-opnd.base>=opnd.stacksize)
{
opnd.base=(int *)realloc(opnd.base,(opnd.stacksize+STACKINCREMENT)*sizeof(int));
if(!opnd.base)
exit(OVERFLOW);
opnd.top=opnd.stacksize+opnd.base;
opnd.stacksize+=STACKINCREMENT;
}
*opnd.top++=r;
return 1;
}
int push(Sqstack &optr,char str)//运算符 入栈
{
if(optr.top-optr.base>=optr.stacksize)
{
optr.base=(SElemtype *)realloc(optr.base,(optr.stacksize+STACKINCREMENT)*sizeof(SElemtype));
if(!optr.base)
exit(OVERFLOW);
optr.top=optr.stacksize+optr.base;
optr.stacksize+=STACKINCREMENT;
}
*optr.top=str;
*optr.top++;
return 1;
}
int Pop(Sqstack1 &opnd,int b)//数字 出栈
{
if(opnd.top==opnd.base)
{
return 0;
}
b=*--opnd.top;
return b;
}
int pop(Sqstack &optr,SElemtype &theta)//运算符 出栈
{
if(optr.top==optr.base)
{
return 0;
}
theta=*--optr.top;
return theta;
}
int precede(char a,char e)//a是运算符栈顶元素,e当前输入的字符,判断优先级
{
int i=0,j=0;
char pre[][7]={
/*运算符之间的优先级表格*/
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','0'},
{'>','>','>','>','0','>','>'},
{'<','<','<','<','<','0','='}
};
switch(a){//顶
case '+': i=0; break;
case '-': i=1; break;
case '*': i=2; break;
case '/': i=3; break;
case '(': i=4; break;
case ')': i=5; break;
case '#': i=6; break;
}
switch(e){//str
case '+': j=0; break;
case '-': j=1; break;
case '*': j=2; break;
case '/': j=3; break;
case '(': j=4; break;
case ')': j=5; break;
case '#': j=6; break;
}
return pre[i][j];
}
int operate(int a,SElemtype &theta,int c)//进行两个数的加减运算
{
int s=0,m,n;
m=a;
n=c;
switch(theta) {
case '+': s = m + n; break;
case '-': s = m - n; break;
case '*': s = m * n; break;
case '/': s = m / n; break;
}
return s;
}
int gettop(Sqstack &optr)//定位到栈顶位置
{
return *(optr.top-1);
}
int main()
{
Sqstack optr;
Sqstack1 opnd;//optr存运算符,opnd存数字
char str;
SElemtype theta;
int a,b,p;
int flag=0;
initSqstack(optr);
InitSqstack1(opnd);
str=getchar();//输入首字符
while(str!='#'||gettop(optr)!='#')
{
if(str-'0'>=0&&str-'0'<=9){
Push(opnd,str-'0');
str=getchar();
}
else
{
if(str=='#'&&flag==0)
{
push(optr,str);
str=getchar();
flag=1;
continue;
}
switch(precede(gettop(optr),str))
{
case '<':
push(optr,str);
str=getchar();
break;
case '=':
pop(optr,str);
str=getchar();
break;
case '>'://优先级大 出栈进行运算
theta=pop(optr,theta);
b=Pop(opnd,b);
a=Pop(opnd,a);
p=operate(a,theta,b);
Push(opnd,p);
break;
default :
cout<<"Expression error!";
return -1;
}
}
}
cout<<p;
}