当前位置:首页 > 利用栈求表达式的值课程设计最后老师改- 副本
参考文献
[1] 黄同成,黄俊民,董建寅.数据结构[M].北京:中国电力出版社,2008 [2] 董建寅,黄俊民,黄同成.数据结构实验指导与题解[M].北京:中国电力出版社,2008
[3] 严蔚敏,吴伟民. 数据结构(C语言版)[M]. 北京:清华大学出版社,2002 [4] 刘振鹏,张晓莉,郝杰.数据结构[M].北京:中国铁道出版社,
29
2003
程序源代码
#include
struct expression //表达式结构 { double result; char express[MAXSIZE]; }EXPRESS[N]; //表达式的一个整体容器s
typedef struct //操作码栈定义 { char code[MAXSIZE]; int top; }czm;
typedef struct //操作数栈定义 { double data[MAXSIZE]; int top; }czs;
//《--操作数栈栈操作--》: void Initstack(czs *nu) { nu->top=-1; }
int Empty(czs *nu)//判空 { if(nu->top==-1) return 0; else return 1; }
int Push(czs *nu,double da)//压栈 { if(nu->top==MAXSIZE-1)
30
{ printf(\提醒:操作数栈已满.\ return 0; } nu->top++; nu->data[nu->top]=da; return 1; }
double Pop(czs *nu)//出栈 { double a='\\0'; if(nu->top==-1) { printf(\提醒:操作数栈已满.\ return a; } a=nu->data[nu->top]; nu->top--; return a; }
double Get(czs *nu)//查看栈顶 { if(nu->top!=-1) {return nu->data[nu->top];} return 0; }
//《--操作码栈栈操作--》:
void initstack(czm *op)//初始化栈 { op->top=-1; }
int empty(czm *op)//判空 { if(op->top==-1) return 0; else return 1; }
int push(czm *op,char co)//压栈
31
{ if(op->top==MAXSIZE-1) { printf(\操作码栈已满.\ return 0; } op->top++; op->code[op->top]=co; return 1; }
char pop(czm *op)//出栈 { char a='\\0'; if(op->top==-1) { printf(\提醒:操作码栈是空的.\ return a; } a=op->code[op->top]; op->top--; return a; }
char get(czm *op)//查看栈顶 { char a='\\0'; if(op->top==-1) { printf(\提醒:操作码栈是空的.\ return a; } else return op->code[op->top]; }
//《--结束栈定义操作--》
//《--函数操作--》:
int change_opnd(char code)//将字符型操作码转换成优先级,非表达式字符反回-2 { switch(code) {
32
共分享92篇相关文档