如题:写出中缀表达式求值的算法:
要求:
①先乘除,后加减
②从左向后算
③先括号内,后括号外
代码实现
SqStack.h 此文件为封装完成栈的相关操作
#define MAXSIZE 100
/*定义顺序栈*/
typedef struct {
int *base; //用于栈存储的基地址
int *top; //指向该基地址的栈顶指针
int stackSize; //栈的大小
}SqStackInt;
/*定义顺序栈*/
typedef struct {
char *base; //用于栈存储的基地址
char *top; //指向该基地址的栈顶指针
int stackSize; //栈的大小
}SqStackChar;
/*初始化*/
int InitStack_Int(SqStackInt &S){
S.base = (int *)malloc(MAXSIZE*sizeof(int)); //给基地址分配一个内存空间
S.top = S.base; //将栈顶指针指向这个基地址
S.stackSize = MAXSIZE; //设置栈的大小
return 0;
}
int InitStack_Char(SqStackChar &S){
S.base = (char *)malloc(MAXSIZE*sizeof(char)); //给基地址分配一个内存空间
S.top = S.base; //将栈顶指针指向这个基地址
S.stackSize = MAXSIZE; //设置栈的大小
return 0;
}
/*进栈*/
int Push_Int(SqStackInt &S,int e){
if(S.top-S.base==S.stackSize) return -1;
*S.top = e; //将输入的值压入栈中
S.top++; //指针上移一个单位
printf("操作数%d进S1栈\n",e) ;
return 0;
}
int Push_Char(SqStackChar &S,char e){
if(S.top-S.base==S.stackSize) return -1;

最低0.47元/天 解锁文章
3610





