已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
数据结构表达式求值 #includeusing namespace std;#define TRUE 1#define FALSE 0#define Stack_Size 20#define Stack_Float 30/*建立字符栈*/ typedef struct char elemStack_Size;/存储定义 int top;Stack_char; void InitStack(Stack_char*S)/初始化顺序栈 S-top=-1; int Push(Stack_char *S,char x)/进栈 if(S-top=Stack_Size-1) return (FALSE); S-top+; S-elemS-top=x; return (TRUE);int Pop(Stack_char*S,char*x)/出栈 if(S-top=-1) return(FALSE); else *x=S-elemS-top; S-top-; return(TRUE); int GetTop(Stack_char*S,char*x)/ 取栈顶 if(S-top=-1) return(FALSE); else *x=S-elemS-top; return(TRUE); char GetTop(Stack_char S) char x; GetTop(&S,&x); return x;/建立数字栈 typedef struct/建立 float elemStack_Float; int top;Stack_float;void InitStack(Stack_float*S)/初始化 S-top=-1;int Push(Stack_float*S,float e) /进栈 if(S-top=Stack_Float-1) return(FALSE); else S-top+; S-elemS-top=e; return(TRUE); int Pop(Stack_float*S,float*x)/出栈 if(S-top=-1) return(FALSE); else *x=S-elemS-top; S-top-; return(TRUE); int GetTop(Stack_float*S,float*x)/ 取栈顶 if(S-top=-1) return(FALSE); else *x=S-elemS-top; return(TRUE); float GetTop(Stack_float S) float x; GetTop(&S,&x); return x; bool In(char ch)/判断字符 if(ch=+|ch=-|ch=*|ch=/|ch=(|ch=)|ch=#) return (TRUE); else return(FALSE); float GetNumber(char*ch)/转化数码 return (*ch-48);float Execute(float a,char op,float b) switch(op) case+:return(a+b);break; case-:return(a-b);break; case*:return(a*b);break; case/:return(a/b);break; default:cout); else return (); if(x=*|x=/) if(ch=() return(); if(x=() if(ch=) return(=); else return(); if(x=#) if(ch=#) return(=); else return();float ExpEvaluation() float n,v,a,b;char op; Stack_char OPTR; Stack_float OVS; InitStack(&OPTR); InitStack(&OVS); Push(&OPTR,#); cout请输入一个表达式串(以#结束)endl; char ch; ch=getchar(); while(ch!=#|GetTop(OPTR)!=#) if(!In(ch) n=GetNumber(&ch); Push(&OVS,n); ch=getchar(); else switch(Compare(GetTop(OPTR),ch) case: Pop(&OPTR,&op); Pop(&OVS,&b); Pop(&OVS,&a); v=Execute(a,op,b); Push(&OVS,v); break; case=: Pop(&OPTR,&op); ch=getchar(); break; v=GetTop(OVS); return(v); int main() coutExpEvaluation()endl; system(pause); return 0; 完善版#includeusing namespace std;#define TRUE 1#define FALSE 0#define Stack_Size 20#define Stack_Float 30/*建立字符栈*/ typedef struct char elemStack_Size;/存储定义 int top;Stack_char; void InitStack(Stack_char*S)/初始化顺序栈 S-top=-1; int Push(Stack_char *S,char x)/进栈 if(S-top=Stack_Size-1) return (FALSE); S-top+; S-elemS-top=x; return (TRUE);int Pop(Stack_char*S,char*x)/出栈 if(S-top=-1) return(FALSE); else *x=S-elemS-top; S-top-; return(TRUE); int GetTop(Stack_char*S,char*x)/ 取栈顶 if(S-top=-1) return(FALSE); else *x=S-elemS-top; return(TRUE); char GetTop(Stack_char S) char x; GetTop(&S,&x); return x;void ClearStack(Stack_char*S)/清空栈 if(S-top!=-1) S-top=-1;/*建立数字栈*/ typedef struct/建立 float elemStack_Float; int top;Stack_float;void InitStack(Stack_float*S)/初始化 S-top=-1;int Push(Stack_float*S,float e) /进栈 if(S-top=Stack_Float-1) return(FALSE); else S-top+; S-elemS-top=e; return(TRUE); int Pop(Stack_float*S,float*x)/出栈 if(S-top=-1) return(FALSE); else *x=S-elemS-top; S-top-; return(TRUE); int GetTop(Stack_float*S,float*x)/ 取栈顶 if(S-top=-1) return(FALSE); else *x=S-elemS-top; return(TRUE); float GetTop(Stack_float S) float x; GetTop(&S,&x); return x;void ClearStack(Stack_float*S)/清空栈 if(S-top!=-1) S-top=-1;/*一些函数*/ char a7= +,-,*,/,(,), #; char p77= /优先权集合 , , , , , ,=48&ch=57) return(TRUE); else return(FALSE); bool Inc(char ch)/判断字符 if(ch=+|ch=-|ch=*|ch=/|ch=(|ch=)|ch=#) return (TRUE); else return(FALSE); float GetNumber(char*ch)/转化数码 return (*ch-48);float Execute(float a,char op,float b) switch(op) case+:return(a+b);break; case-:return(a-b);break; case*:return(a*b);break; case/:return(a/b);break; default:cout不能运算;break; char Compare(char x,char ch) /char x; / GetTop(&S,&x); int i,j,k; for(i=0;i7;i+) if(x=ai) j=i; if(ch=ai) k=i; return pjk;Stack_char OPTR;Stack_float OVS;/InitStack(&OPTR);/InitStack(&OVS);void ExpEvaluation() InitStack(&OPTR); InitStack(&OVS); Push(&OPTR,#); cout请输入一个表达式串(以#结束)endl; char ch; int w=0,q=0,y=0,z=0,m=0;float n=0,v,a,b;char op; ch=getchar(); if(!Ins(ch) if(ch=() z=1; else if(ch=-) w=1;ch=getchar();/记录输入负号 else cout输入错误,请重新a; fflush(stdin); /清理缓存 ClearStack(&OPTR); ExpEvaluation(); exit(1); /else while(ch!=#|GetTop(OPTR)!=#) if(Ins(ch) n=n*10+GetNumber(&ch); q=1;/记录输入数字 y=0;/记录输入字符 ch=getchar(); /coutn值nendl; /system(pause); if(Inc(ch) if(ch=() z=1; if(w=1) / coutzzendl; if(q=0) if(z=1) Push(&OPTR,ch); /cout+GetTop(OPTR)endl; z=0; m=2;/记录负的左括号 ch=getchar(); continue; else if(ch=-) w=0;ch=getchar(); else cout输入错误,请重新b; fflush(stdin); /清理缓存 ClearStack(&OPTR); ExpEvaluation(); exit(1); else coutwwendl;n=-n;w=0; if(m=2)n=-n;m-;coutmmendl; /else if(q=1) Push(&OVS,n); cout栈顶GetTop(OVS)endl; n=0; q=0; if(y=1) if(GetTop(OPTR)=(&ch=-) w=1;ch=getchar(); else if(ch=() z=1;/记录左括号 else /coutcchendl; cout输入错误,请重新c; fflush(stdin); /清理缓存 ClearStack(&OPTR); ClearStack(&OVS); ExpEvaluation(); exit(1); if(y=0|z=1) /coutdchendl; /couto栈顶GetTop(OPTR)endl; /cout*Compare(GetTop(OPTR),ch)endl; switch(Compare(GetTop(OPTR),ch) case: Push(&OPTR,ch); y=1;z=0; ch=getchar(); /couteGetTop(OPTR): Pop(&OPTR,&op); Pop(&OVS,&b); Pop(&OVS,&a); if(op=/&b=0) cout输入错误,请重新d; fflush(stdin); /清理缓存 ClearStack(&OPTR); ClearStack(&OVS); ExpEvaluation(); exit(1); v=Execute(a,op,b); if(m=1)v=-v;m=0; Push(&OVS,v); /cout*栈顶GetTop(OVS)endl; break; case=: Pop(&OPTR,&op); ch=getchar(); break; case: cout括号不匹配,请重新e; ffl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 安全专工岗位职责培训课件
- 2025年教育行业用户画像构建
- 压强教学课件2025-2026学年人教版物理八年级下学期
- 安全风险预控管理办公室工作职责培训
- 责任平分协议书
- 货物仓储责任协议书
- 质保金转让协议书
- 购房分期还款协议书
- 2025年木工机械安全操作规范培训
- 2025年电力生产技术科专工安全职责培训
- DB3717∕T 30-2025 芍药鲜切花采后处理技术规程
- 2026年口腔诊所客户引流与服务品质提升
- 2025上海中考地理必考知识点清单
- 食品用洗涤剂产品生产许可证实施细则2025
- 2025年行政执法类专业科目考试真题(附答案)
- (行业典型)计量技术比武考试(选择题)试题库(附答案)
- 医院消防安全管理规范(2025版)
- 四川省拟任县处级党政领导职务政治理论水平任职资格考试题全套共12套
- 学堂在线批判性思维-方法和实践章节测试答案
- (新北师大版)数学八年级下册全册说课稿
- 养老护理卧床洗头操作规范
评论
0/150
提交评论