C语言表达式计算器代码.docx_第1页
C语言表达式计算器代码.docx_第2页
C语言表达式计算器代码.docx_第3页
C语言表达式计算器代码.docx_第4页
C语言表达式计算器代码.docx_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include #include #include typedef struct Expression/定义表达式结构体int num64;char ch64;Exper;typedef struct node /定义字栈 char e100;int top;linkstack;typedef struct Node /定义整数栈 int c100;int top; link;char precede(char a,char b) /符号判断int i=0,j=0;char sign=+-*/()%;char prec88=()()()()(),();if (b=)return 0;while(signi!=a)i+;while (signj!=b)j+;return(precij);void push1(linkstack *p,char c) /字符入栈 p-ep-top+=c;void push2(link *p,int num) /整数入栈p-cp-top+=num;char gettop(linkstack *s) /读字符栈顶的元素 char c;c=s-es-top-1;return(c);char pop1(linkstack *s) /字符出栈char c;c=s-e-s-top;return(c);int pop2(link *N) /整数入栈int num;num=N-c-N-top;return(num);int operate(int a, char oper,int b)/进行运算int num;switch (oper)case +: num=a+b;break;case -: num=a-b;break;case *: num=a*b;break;case /: num=a/b;break;case : num=pow(a,b);break;case %: num=a%b;break;return num;Exper number(char *n_w) /把输入的字符转换成整数和字符,并分别存在数组中int i=0;int j=0;int k=0;int temp;Exper n_exper;while (n_wi!=0)if (n_wi=0& n_wi=0& n_wi=9)temp=temp*10+n_wi-0;i+;n_exper.numj=temp;j+; n_exper.chk=n_wi;k+;i+;return n_exper;void Calculate(Exper C_exper)/计算函数int i=0,j=0,k=1;int br=0;int temp1,temp2;/临时变量char dec,CH;linkstack cha;/定义字符栈link Num;/定义整数栈Num.top=0;cha.top=0;push1(&cha,#);doif (j=0 & C_exper.chj=()br+;if (C_exper.chj!=)if (k=1)push1(&cha,C_exper.chj);push2(&Num,C_exper.numi);push2(&Num,C_exper.num+i);dec=precede(gettop(&cha),C_exper.ch+j);elsedec=precede(gettop(&cha),C_exper.chj); elsetemp1=pop2(&Num);temp2=pop2(&Num);push2(&Num,operate(temp2,pop1(&cha),temp1);printf(%dnn,pop2(&Num);break;switch(dec)case :temp1=pop2(&Num);temp2=pop2(&Num);push2(&Num,operate(temp2,pop1(&cha),temp1);push1(&cha,C_exper.chj);push2(&Num,C_exper.num+i);j+;k+;break;case (:if (br=1)push1(&cha,C_exper.chj);br+;j+;k+;i+; elsepush1(&cha,C_exper.chj);push1(&cha,C_exper.ch+j);push2(&Num,C_exper.num+i);j+;k+;break;case ):push1(&cha,C_exper.chj);CH=pop1(&cha);CH=pop1(&cha);temp1=pop2(&Num);temp2=pop2(&Num);push2(&Num,operate(temp2,CH,temp1);CH=pop1(&cha);j+;k+;if (gettop(&cha)=#)push1(&cha,C_exper.chj);push2(&Num,C_exper.numi);j+;break;default: break;while (gettop(&cha)!=#);int main()char w128;Ex

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论