C语言表达式计算器代码.docx

  1. 1、本文档共6页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C语言表达式计算器代码

#include stdio.h#include math.h#include string.htypedef struct Expression//定义表达式结构体{int num[64];char ch[64];}Exper;typedef struct node //定义字栈{ char e[100];int top;}linkstack;typedef struct Node //定义整数栈{ int c[100];int top;}link;char precede(char a,char b) //符号判断{int i=0,j=0;char sign[]=+-*/()^%;char prec[8][8]={(),(),(),(),((((~~((,))))~~)),(),()};if (b===){return 0;}while(sign[i]!=a){i++;}while (sign[j]!=b){j++;}return(prec[i][j]);}void push1(linkstack *p,char c) //字符入栈{ p-e[p-top++]=c;}void push2(link *p,int num) //整数入栈{p-c[p-top++]=num;}char gettop(linkstack *s) //读字符栈顶的元素{ char c;c=s-e[s-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_w[i]!=\0){if (n_w[i]=0 n_w[i]=9){temp=n_w[i]-0;i++;while (n_w[i]=0 n_w[i]=9){temp=temp*10+n_w[i]-0;i++;}n_exper.num[j]=temp;j++;} n_exper.ch[k]=n_w[i];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,#);do{if (j==0 C_exper.ch[j]==(){br++;}if (C_exper.ch[j]!==){if (k==1){push1(cha,C_exper.ch[j]);push2(Num,C_exper.num[i]);push2(Num,C_exper.num[++i]);dec=precede(gettop(cha),C_exper.ch[++j]);}else{dec=precede(gettop(cha),C_exper.ch[j]);}} else{temp1=pop2(Num);temp2=pop2(Num);push2(Num,operate(temp2,pop1(cha),temp1));printf(%d\n\n,pop2(Num));break;}switch(dec){case :{push1(cha,C_exper.ch[j]);push2(Num,operate(pop2(Num),pop1(cha),C_exper.num[++i]));j++;k++;break;}case :{temp1=pop2(Num);temp2=pop2(Num);push2(

文档评论(0)

xjj2017 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档