




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
。# include # include # include # define maxsize 100typedef double datatype1;typedef char datatype2;typedef struct stack1 datatype1 data1maxsize; int top1;/*栈顶元素*/seqstack1,*pseqstack1; /*顺序栈*/typedef struct stack2 datatype2 data2maxsize; int top2;/*栈顶元素*/seqstack2,*pseqstack2; /*顺序栈*/*栈的初始化*/pseqstack1 init_seqstack1(void) pseqstack1 S; S=(pseqstack1)malloc(sizeof(pseqstack1); if(S) S-top1=-1; return S;pseqstack2 init_seqstack2(void) pseqstack2 S; S=(pseqstack2)malloc(sizeof(pseqstack2); if(S) S-top2=-1; return S;/*判断栈空*/int empty_seqstack1(pseqstack1 S) if(S-top1=-1) return 1; else return 0;int empty_seqstack2(pseqstack2 S) if(S-top2=-1) return 1; else return 0;/*X入栈*/int push_seqstack1(pseqstack1 S,datatype1 X) if(S-top1=maxsize-1) printf(栈满,无法入栈!n); return 0; else S-top1+; S-data1S-top1=X; return 1; int push_seqstack2(pseqstack2 S,datatype2 X) if(S-top2=maxsize-1) printf(栈满,无法入栈!n); return 0; else S-top2+; S-data2S-top2=X; return 1; /*X出栈*/int pop_seqstack1(pseqstack1 S,datatype1 *X) if(empty_seqstack1(S) return 0; else *X=S-data1S-top1; S-top1-; return 1; int pop_seqstack2(pseqstack2 S,datatype2 *X) if(empty_seqstack2(S) return 0; else *X=S-data2S-top2; S-top2-; return 1; /*求栈顶元素*/int gettop_seqstack1(pseqstack1 S,datatype1 *X) if(empty_seqstack1(S) return 0; else *X=S-data1S-top1; return 1;int gettop_seqstack2(pseqstack2 S,datatype2 *X) if(empty_seqstack2(S) return 0; else *X=S-data2S-top2; return 1;/*判断字符是否为操作数。若是返回1,否则返回0*/int isnum(char c) if(c=0 & cA*/ S=init_seqstack1();/*初始化栈*/ while(ch!=#)/*遇到元素!=#时*/ if(isnum(ch)/*判断ch是否为数字字符,计算出操作数*/ operand=operand*10+(ch-0); else/*否则*/ if(operand)push_seqstack1(S,operand);/*当前字符不是数字,操作数结束,要入栈*/operand=0;if(ch!= & ch!= )pop_seqstack1(S,&b);/*运算符ch后的操作数出栈存入b*/pop_seqstack1(S,&a);/*运算符ch前的操作数出栈存入a*/switch(ch)/*求 a ch b=? ,将结果赋给 c */ case + : c=a+b;break;case - : c=a-b;break;case * : c=a*b;break;case / :if(b!=0)c=a/b;elseprintf(分母为零!); push_seqstack1(S,c);/*将c压入栈中*/ ch=*A+;/*指针向下移动一位*/ /*遇到#循环结束*/ gettop_seqstack1(S,&result);/*此时栈顶元素即为计算结果result*/ return result;/*优先级判断函数*/int priority(char op)switch(op)case #: return 1;case ): return 2;case +: case -: return 3;case *: case /: return 4;case (: return 5;default : return 0; /*将指针infixexp指向的中缀表达式转换为指针postfixexp指向的后缀表达式*/int infix_exp_value(char *infixexp,char *postfixexp)pseqstack2 S;/*定义栈S*/int count=0;char w;/*存放读取到的表达式(infixexp)的字符*/char c;/*存放栈顶元素*/char topelement;/*存出栈元素*/S=init_seqstack2();/*初始化栈*/if(!S)/*栈的初始化判断*/printf(栈初始化失败!);return 0;push_seqstack2(S,#);/*将结束符# 加入运算符栈S中*/w=*infixexp;/*读表达式字符=w*/while(gettop_seqstack2(S,&c),c)!=#|w!=#)/*栈顶元素不等于#或w不等于#时循环*/if(isnum(w)/*判断w是否为操作数,若是直接输出,读下一个字符=w,转*/if(count)*postfixexp=;postfixexp+;count=0;*postfixexp=w;postfixexp+;w=*(+infixexp);else/*w若是运算符分类如下*/count=1;if( (gettop_seqstack2(S,&c),c)=( & w=) )/*如果栈顶为(并且w为)则(出栈不输出,读下一个字符=w,转*/ pop_seqstack2(S,&topelement); /*将(出栈存入topelement*/w=*(+infixexp);elseif(gettop_seqstack2(S,&c),c)=(|priority(gettop_seqstack2(S,&c),c) ) w,转*/ push_seqstack2(S,w);w=*(+infixexp);else/*否则*/*从运算符栈中出栈并输出,转*/ pop_seqstack2(S,&topelement);*postfixexp=topelement;postfixexp+;*postfixexp=#;/*在指针postfixexp指向的后缀表达式结尾追加字符#*/*(+postfixexp)=0;/*在指针postfixexp指向的后缀表达式最后追加结束符0*/return 1;/*主函数*/int main() int i=0;char Amaxsize;char Bmaxsize;printf(请输入表达式,如:20+13#,必须以#号结尾!n); /* 1+2*(9+7)-4/2# 23+(12*3-2)/4+34*5/7)+108/9# */Ai=getchar();while(Ai+!=#)Ai=getcha
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 社区书记发言稿
- 规划建设管理培训大纲
- 教师语言行为培训
- 脚手架安全培训
- 2025版建筑工程用方木材料采购合同
- 2025版LED户外广告新媒体营销活动策划与执行合同
- 2025版钢结构场地租赁合同(含仓储、装卸及搬运服务)
- 2025版电商平台电商人员保密协议与竞业禁止合同
- 2025版木材行业信息化建设与技术支持合同
- 二零二五年度商铺租赁合同范本解读
- 棋牌室消防安全管理制度范本
- (2025年)河北省邢台市辅警协警笔试笔试真题含答案
- 渝22TS02 市政排水管道附属设施标准图集 DJBT50-159
- 新生儿科护理进修
- 2025至2030中国生物反应器和发酵罐行业产业运行态势及投资规划深度研究报告
- 上海市闵行区2024-2025学年下学期期末考试六年级数学试卷及答案(新教材沪教版)
- 华为廉洁培训课件
- 园中园安全管理制度
- 失眠的预防与中医护理
- 2025年云南高考真题化学试题含答案
- 2025届中考物理全真模拟卷【云南专用】(含解析)
评论
0/150
提交评论