版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、#include #include #include #include #define MAXSIZE 100#define N 1000int i=0; /表达式数typedef struct expression/表达式结构 long double result; char expstrMAXSIZE; expression;expression exprN;/表达式的一个整体容器stypedef struct/操作码栈定义 char codeMAXSIZE; int top; opnd;typedef struct/操作数栈定义 double dateMAXSIZE; int top;
2、num;/-opnd栈操作-:void initstack(opnd *op)/初始化栈 op-top=-1; int empty_opnd(opnd *op)/判空 if(op-top=-1) return 0; else return 1; int push_opnd(opnd *op,char co)/压栈 if(op-top=MAXSIZE-1) printf(The opnd stack is full.); return 0; op-top+; op-codeop-top=co; return 1; char pop_opnd(opnd *op)/出栈 char a=0; if(o
3、p-top=-1) printf(error:The opnd stack is empty.); return a; a=op-codeop-top; op-top-; return a; char get_opnd(opnd *op)/查看栈顶 char a=0; if(op-top=-1) printf(error:The opnd stack is empty.); return a; else return op-codeop-top; /-num栈操作-:void initstack(num *nu) nu-top=-1; int empty_num(num *nu)/判空 if(
4、nu-top=-1) return 0; else return 1; int push_num(num *nu,double da)/压栈 if(nu-top=MAXSIZE-1) printf(error:The date stack is full.); return 0; nu-top+; nu-datenu-top=da; return 1; double pop_num(num *nu)/出栈 double a=0; if(nu-top=-1) printf(error:The date stack is empty.); return a; a=nu-datenu-top; nu
5、-top-; return a; double get_num(num *nu)/查看栈顶 if(nu-top!=-1) return nu-datenu-top; /-结束栈定义操作-/-函数操作-:int change_opnd(char code)/将字符型操作码转换成优先级,非表达式字符反回-2 switch(code) case =:return 1;break; case ):return 2;break; case +:return 3;break; case -:return 3;break; case *:return 4;break; case /:return 4;bre
6、ak; case (:return 0;break;/操作码级别=0; case 1:case 2:case 3:case 4:case 5:case 6:case 7: case 8:case 9:case 0:case .: return -1;/操作数级别=-1; default: return -2;/其它符号级别=-2 char procede(char top,char code)/处理操作码,判断栈的操作 if(change_opnd(code)=0)/(入栈 return (); else if(change_opnd(code)=2&change_opnd(top)=0)/(
7、和)同时出现,(出栈,)不入栈 return (=); else if(change_opnd(code); else return ();/入栈 double change_num(char str)/数字字符串转成double型数字 char *s=str; int p=1,q=0;/p=小数点前位数,q=小数点后位数 char d=.,z=0; double da=0,p1; if(strstr(str,d)=0)/判断是否有小数点 p=strlen(str); else if(strstr(str,d)=str)/没有输入小数点前的数,如.032 p=1; q=strlen(str)-
8、1; strcpy(str,strcat(z,str); else p=strstr(str,d)-str; q=strlen(str)-p-1; for(int i=0;ip;i+) /小数点前的各位数乘以各自的阶数,然后叠加:123=1*100+2*10+3*1 da=da+(int)stri-48)*pow(10,p-i-1); for(int j=0;j0) printf(n表达式只能以“数字”或“(”开头。请重新输入:); gets(expri.expstr); p=expri.expstr; n=0; continue; else if(change_opnd(*p)=-2) pr
9、intf(n表达式%c为非法字符。请重新输入:,*p); gets(expri.expstr); p=expri.expstr; n=0; continue; else /合法刚跳到下一个字符 p=p+1; continue; if(change_opnd(*p)=-2)/非法字符判断 printf(n表达式%c为非法字符。请重新输入:,*p); gets(expri.expstr); p=expri.expstr; n=0; continue; if(change_opnd(*p)=0)/(前一个字符只能是+、-、*、/、( if(change_opnd(*(p-1)4) if(change
10、_opnd(*(p-1)!=0) printf(n表达式%c或%c不符合语法。请重新输入:,*(p-1),*p); gets(expri.expstr); p=expri.expstr; n=0; continue; if(change_opnd(*p)0)/+、-、*、/、=、)前一个字符只能是数字和) if(change_opnd(*(p-1)!=-1) if(change_opnd(*(p-1)!=2) printf(n表达式%c或%c不符合语法。请重新输入:,*(p-1),*p); gets(expri.expstr); p=expri.expstr; n=0; continue; i
11、f(change_opnd(*p)=1)/判断表达式中是否有=重复出现,最后括号是否配对 if(*(p+1)!=0) printf(n表达式中=,只能出现在表达式结束处。请重新输入:); gets(expri.expstr); p=expri.expstr; n=0; continue; if(n!=0) printf(n表达式括号不配。请重新输入:); gets(expri.expstr); p=expri.expstr; n=0; continue; p=p+1; return 1; double caculate(opnd *op,num *nu)/简单计算+,-,*,/ double
12、b=pop_num(nu),a=pop_num(nu); switch(pop_opnd(op) case +:return(a+b);break; case -:return(a-b);break; case *:return(a*b);break; case /:return(a/b);break; void result(opnd *op,num *nu)/计算结果 char str2MAXSIZE=,str32=0; char *ps=expri.expstr; initstack(op);/初始化栈 initstack(nu); push_opnd(op,=); while(!(*p
13、s=)&(get_opnd(op)=)/检查是表达式和操作码是否到尾 if(change_opnd(*ps)=-1)/操作数处理 while(change_opnd(*ps)=-1) strncpy(str3,ps,1);/数字字符一个个取出放在str2 strcat(str2,str3); ps+; push_num(nu,change_num(str2); strcpy(str2,); else /操作码处理 switch(procede(get_opnd(op),*ps) case :push_num(nu,caculate(op,nu);continue;break; if(*ps=)
14、&get_opnd(op)=) ps+; continue; if(*ps=|get_opnd(op)=) continue;/表达式和操作码有一个到尾,则跳出继续循环 ps+; expri.result=get_num(nu); printf(nt 表达式:%st计算结果:%lfn,expri.expstr,expri.result); printf(t-n); i+;/表达式个数加1; void check()/显示计算结果 for(int n=0;n0;n-)/记录最后一个#号位置,即未保存的结果的开始位置,重复保存只会追加 if(exprn-1.expstr0=#) break; st
15、rcpy(expri.expstr,#表达式个数:);/每次保存都统计计算次数 expri.result=i-n; i+; for(m=n;mi;m+) if(fwrite(&exprm,sizeof(struct expression),1,fp)!=1)/将表达式和计算结果存到文件中 printf(file write errorn); fclose(fp); printf(*提醒:计算记录已经保存n); void load()/显示所有计算记录 int m; expression eN; FILE *fp; printf(n); if(fp=fopen(calculate.dat,rb)
16、=NULL)/空文件 printf(t-n); printf(*提醒:没有记录信息,请进行计算并保存信息:n); return; for(m=0;fread(&em,sizeof(struct expression),1,fp);m+)/按照expression结构一个个读取 printf(t%d -n,m+1); printf(t 表达式:%st计算结果:%.2lfn,em.expstr,em.result); if(em.expstr0=#)/控制输出不同次计算的记录 m=-1; printf(n); printf(t-n); fclose(fp); printf(n); void hel
17、p() printf(t 表达式计算,请用户正确输入表达式,不得出现非法字符及字符nt重复出现,表达式以“=”号结尾,若计算到负数“-20”,请输nt入“(0-20)”。); printf(请用户及时保存计算结果以便查看,每次回到主菜单nt时,若没有保存结果,则当次计算结果会被清除。n); /void start(opnd *op,num *nu);void start2(opnd *op,num *nu)/第二层计算选择菜单 void start(opnd *op,num *nu); char r; while(1) printf(t-n); printf(nttt查看本次计算记录,请输入r
18、或Rnnttt保存本次计算记录,请输入s或Snnttt查看所有计算记录,请输入l或Lnnttt回到主菜单,按任意键返回ntt:); scanf(%s,&r); if(r=r|r=R) check(); else if(r=s|r=S) save(); else if(r=l|r=L) load(); else i=0; start(op,nu); void start(opnd *op,num *nu)/程序主导 char ch,c,g; g=Y; printf(nttt查看记录,请输入l或Lnnttt清空记录,请输入C或cnnttt计算式子,请输入Y或ynnttt退出程序,按任意键退出ntt
19、:); scanf(%s,&c); getchar(ch); if(c=l|c=L) load(); start(op,nu); else if(c=Y|c=y) while(1) if(g=Y|g=y) if(make_str()/语法检查 printf(t-n); result(op,nu);/计算 else printf(nt*提醒:计算结束!nn); break; printf(n继续计算,请输入Y或y,否则按任意键结束计算:); scanf(%s,&g); getchar(ch); start2(op,nu); else if(c=C|c=c)/清空文件 printf(t-n); r
20、emove(calculate.dat); printf(*提醒:所有记录已经清空。); start(op,nu); else printf(nt*提醒:结束!n); exit(0); void main() opnd op;/操作码栈#include #include #include #include #define MAXSIZE 100#define N 1000int i=0; /表达式数typedef struct expression/表达式结构 long double result; char expstrMAXSIZE; expression;expression exprN
21、;/表达式的一个整体容器stypedef struct/操作码栈定义 char codeMAXSIZE; int top; opnd;typedef struct/操作数栈定义 double dateMAXSIZE; int top; num;/-opnd栈操作-:void initstack(opnd *op)/初始化栈 op-top=-1; int empty_opnd(opnd *op)/判空 if(op-top=-1) return 0; else return 1; int push_opnd(opnd *op,char co)/压栈 if(op-top=MAXSIZE-1) pri
22、ntf(The opnd stack is full.); return 0; op-top+; op-codeop-top=co; return 1; char pop_opnd(opnd *op)/出栈 char a=0; if(op-top=-1) printf(error:The opnd stack is empty.); return a; a=op-codeop-top; op-top-; return a; char get_opnd(opnd *op)/查看栈顶 char a=0; if(op-top=-1) printf(error:The opnd stack is em
23、pty.); return a; else return op-codeop-top; /-num栈操作-:void initstack(num *nu) nu-top=-1; int empty_num(num *nu)/判空 if(nu-top=-1) return 0; else return 1; int push_num(num *nu,double da)/压栈 if(nu-top=MAXSIZE-1) printf(error:The date stack is full.); return 0; nu-top+; nu-datenu-top=da; return 1; doub
24、le pop_num(num *nu)/出栈 double a=0; if(nu-top=-1) printf(error:The date stack is empty.); return a; a=nu-datenu-top; nu-top-; return a; double get_num(num *nu)/查看栈顶 if(nu-top!=-1) return nu-datenu-top; /-结束栈定义操作-/-函数操作-:int change_opnd(char code)/将字符型操作码转换成优先级,非表达式字符反回-2 switch(code) case =:return 1;
25、break; case ):return 2;break; case +:return 3;break; case -:return 3;break; case *:return 4;break; case /:return 4;break; case (:return 0;break;/操作码级别=0; case 1:case 2:case 3:case 4:case 5:case 6:case 7: case 8:case 9:case 0:case .: return -1;/操作数级别=-1; default: return -2;/其它符号级别=-2 char procede(cha
26、r top,char code)/处理操作码,判断栈的操作 if(change_opnd(code)=0)/(入栈 return (); else if(change_opnd(code)=2&change_opnd(top)=0)/(和)同时出现,(出栈,)不入栈 return (=); else if(change_opnd(code); else return ();/入栈 double change_num(char str)/数字字符串转成double型数字 char *s=str; int p=1,q=0;/p=小数点前位数,q=小数点后位数 char d=.,z=0; doubl
27、e da=0,p1; if(strstr(str,d)=0)/判断是否有小数点 p=strlen(str); else if(strstr(str,d)=str)/没有输入小数点前的数,如.032 p=1; q=strlen(str)-1; strcpy(str,strcat(z,str); else p=strstr(str,d)-str; q=strlen(str)-p-1; for(int i=0;ip;i+) /小数点前的各位数乘以各自的阶数,然后叠加:123=1*100+2*10+3*1 da=da+(int)stri-48)*pow(10,p-i-1); for(int j=0;j
28、0) printf(n表达式只能以“数字”或“(”开头。请重新输入:); gets(expri.expstr); p=expri.expstr; n=0; continue; else if(change_opnd(*p)=-2) printf(n表达式%c为非法字符。请重新输入:,*p); gets(expri.expstr); p=expri.expstr; n=0; continue; else /合法刚跳到下一个字符 p=p+1; continue; if(change_opnd(*p)=-2)/非法字符判断 printf(n表达式%c为非法字符。请重新输入:,*p); gets(ex
29、pri.expstr); p=expri.expstr; n=0; continue; if(change_opnd(*p)=0)/(前一个字符只能是+、-、*、/、( if(change_opnd(*(p-1)4) if(change_opnd(*(p-1)!=0) printf(n表达式%c或%c不符合语法。请重新输入:,*(p-1),*p); gets(expri.expstr); p=expri.expstr; n=0; continue; if(change_opnd(*p)0)/+、-、*、/、=、)前一个字符只能是数字和) if(change_opnd(*(p-1)!=-1) i
30、f(change_opnd(*(p-1)!=2) printf(n表达式%c或%c不符合语法。请重新输入:,*(p-1),*p); gets(expri.expstr); p=expri.expstr; n=0; continue; if(change_opnd(*p)=1)/判断表达式中是否有=重复出现,最后括号是否配对 if(*(p+1)!=0) printf(n表达式中=,只能出现在表达式结束处。请重新输入:); gets(expri.expstr); p=expri.expstr; n=0; continue; if(n!=0) printf(n表达式括号不配。请重新输入:); get
31、s(expri.expstr); p=expri.expstr; n=0; continue; p=p+1; return 1; double caculate(opnd *op,num *nu)/简单计算+,-,*,/ double b=pop_num(nu),a=pop_num(nu); switch(pop_opnd(op) case +:return(a+b);break; case -:return(a-b);break; case *:return(a*b);break; case /:return(a/b);break; void result(opnd *op,num *nu)
32、/计算结果 char str2MAXSIZE=,str32=0; char *ps=expri.expstr; initstack(op);/初始化栈 initstack(nu); push_opnd(op,=); while(!(*ps=)&(get_opnd(op)=)/检查是表达式和操作码是否到尾 if(change_opnd(*ps)=-1)/操作数处理 while(change_opnd(*ps)=-1) strncpy(str3,ps,1);/数字字符一个个取出放在str2 strcat(str2,str3); ps+; push_num(nu,change_num(str2);
33、strcpy(str2,); else /操作码处理 switch(procede(get_opnd(op),*ps) case :push_num(nu,caculate(op,nu);continue;break; if(*ps=)&get_opnd(op)=) ps+; continue; if(*ps=|get_opnd(op)=) continue;/表达式和操作码有一个到尾,则跳出继续循环 ps+; expri.result=get_num(nu); printf(nt 表达式:%st计算结果:%lfn,expri.expstr,expri.result); printf(t-n)
34、; i+;/表达式个数加1; void check()/显示计算结果 for(int n=0;n0;n-)/记录最后一个#号位置,即未保存的结果的开始位置,重复保存只会追加 if(exprn-1.expstr0=#) break; strcpy(expri.expstr,#表达式个数:);/每次保存都统计计算次数 expri.result=i-n; i+; for(m=n;mi;m+) if(fwrite(&exprm,sizeof(struct expression),1,fp)!=1)/将表达式和计算结果存到文件中 printf(file write errorn); fclose(fp); printf(*提醒:计算记录已经保存n); void load()/显示所有计算记录 int m; express
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年云南机电职业技术学院单招职业技能考试题库及答案详解1套
- 2026年上海应用技术大学单招综合素质考试题库含答案详解(综合卷)
- 2026年三峡电力职业学院单招职业适应性考试题库含答案详解ab卷
- 2026年云南机电职业技术学院单招职业倾向性考试题库附参考答案详解(考试直接用)
- 2026年仙桃职业学院单招职业适应性测试题库附参考答案详解(达标题)
- 2026年临汾职业技术学院单招职业技能考试题库带答案详解(预热题)
- 2026年上海师范大学单招职业适应性考试题库含答案详解ab卷
- 2026年三亚城市职业学院单招职业技能测试题库附参考答案详解(完整版)
- 2026年中国计量大学单招职业倾向性考试题库附答案详解(培优b卷)
- 2026年三亚中瑞酒店管理职业学院单招职业技能测试题库含答案详解(能力提升)
- 国家深海基地管理中心招聘笔试题库2025
- 2025年工程监理招聘面试参考题库及答案
- 提高销售技巧培训
- 《涉外法治概论》课件 杜涛 第7-10章 对外贸易与经济制裁法律制度-涉外应急管理法律制度
- 智慧园艺课件
- 信号与系统(第2版)课件 4.9 抽样定理
- 食品营养与食品安全试题及答案
- 国家职业标准 6-11-01-03 化工总控工S (2025年版)
- 2025年江西省上饶市中考一模英语试题(含答案无听力原文及音频)
- 地基买卖合同范本
- 产房安全核查表常用指南
评论
0/150
提交评论