200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > c语言计算器自动计算源代码 c语言计算器源代码

c语言计算器自动计算源代码 c语言计算器源代码

时间:2020-12-07 17:08:23

相关推荐

c语言计算器自动计算源代码 c语言计算器源代码

《c语言计算器源代码》由会员分享,可在线阅读,更多相关《c语言计算器源代码(7页珍藏版)》请在人人文库网上搜索。

1、 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 。

2、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;elsereturn 0;精品.int empty_seqsta。

3、ck2(pseqstack2 S)if(S-top2=-1)return 1;elsereturn 0;/*X入栈*/int push_seqstack1(pseqstack1 S,datatype1 X)if(S-top1=maxsize-1)printf(栈满,无法入栈!n);return 0;elseS-top1+;S-data1S-top1=X;return 1;int push_seqstack2(pseqstack2 S,datatype2 X)if(S-top2=maxsize-1)printf(栈满,无法入栈!n);return 0;elseS-top2+;S-data2S-t。

4、op2=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,datatype。

5、1 *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是。

6、否为数字字符,计算出操作数*/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。

7、=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。

8、 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(栈初始。

9、化失败!);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( (get。

10、top_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);*po。

11、stfixexp=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=getchar();Ai=0;infix_exp_value(A,B);printf(A=%sn,A);printf(B=%sn,B);printf(上式的结果为: );printf(%gn,postfix_exp(B);return 0;getch();如有侵权请联系告知删除,感谢你们的配合!精品。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。