表达式求值c语言程序

表达式求值c语言程序

ID:13225401

大小:36.00 KB

页数:7页

时间:2018-07-21

表达式求值c语言程序_第1页
表达式求值c语言程序_第2页
表达式求值c语言程序_第3页
表达式求值c语言程序_第4页
表达式求值c语言程序_第5页
资源描述:

《表达式求值c语言程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、#include#includetypedefstructopLnode{charop;opLnode*next;}opLnode,*opLinkList;voidOpInitStack(opLinkList&L)//运算符生成栈{L=(opLinkList)malloc(sizeof(opLnode));L->next=NULL;}voidOpPush(opLinkList&L,charx)//运算符入栈{opLinkListp;p=(opLinkList)malloc(s

2、izeof(opLnode));p->op=x;p->next=L->next;L->next=p;}charOpPop(opLinkList&L,charx)//栈顶运算符元素出栈{opLinkListp;p=(opLinkList)malloc(sizeof(opLnode));p=L->next;x=p->op;L->next=p->next;free(p);returnx;}charOpGetTop(opLinkListL,charx)//读栈顶运算符元素{L=L->next;x=L->op;return

3、x;}typedefstructLnode{inti;Lnode*next;}Lnode,*LinkList;voidInitStack(LinkList&L)//数值生成栈{L=(LinkList)malloc(sizeof(Lnode));L->next=NULL;}voidPush(LinkList&L,intx)//数值入栈{LinkListp;p=(LinkList)malloc(sizeof(Lnode));p->i=x;p->next=L->next;L->next=p;}intPop(LinkLi

4、st&L,intx)//栈顶数值元素出栈{LinkListp;p=(LinkList)malloc(sizeof(Lnode));p=L->next;x=p->i;L->next=p->next;free(p);returnx;}intGetTop(LinkListL,intx)//读栈顶数值元素{L=L->next;x=L->i;returnx;}intGetNumber(charch)//字符转化为数字{return(ch-48);}intJudgeOp(chara)//判断输入是否为运算符{switch(a

5、){case'+':case'-':case'*':case'/':case'(':case')':case'=':return1;break;default:return0;break;}}intJudgeNum(chara)//判断输入是否为为数字{switch(a){case'0':case'1':case'2':case'3':case'4':case'5':case'6':case'7':case'8':case'9':return1;break;default:return0;break;}}char

6、Precede(chara,charb)//比较两运算符的“大小”{switch(a){case'+':case'-':{if('*'==b

7、

8、'/'==b

9、

10、'('==b){return'<';}else{return'>';}break;}case'*':case'/':{if('('==b){return'<';}else{return'>';}break;}case'(':{if(')'==b){return'=';}else{return'<';}break;}case')':{return'>';br

11、eak;}case'=':{if('='==b){return'=';}else{return'<';}break;}default:break;}}intOperate(inta,charop,intb)//运算{intc;switch(op){case'+':{c=a+b;break;}case'-':{c=a-b;break;}case'*':{c=a*b;break;}case'/':{c=a/b;break;}default:break;}returnc;}intmain(){printf("本程序只能

12、计算整型数据,小数部分自动舍弃。请输入一个表达式串(以=结束)");intnum,a,b,v;charch,op;intJudge=1;//Judge用于判断输入非法opLinkListOPTR;LinkListOPND;OpInitStack(OPTR);InitStack(OPND);OpPush(OPTR,'=');ch=getchar();while(ch

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。