当前位置:文档之家› c语言计算器源代码

c语言计算器源代码

c语言计算器源代码 IMB standardization office【IMB 5AB- IMBK 08- IMB 2C】#i n c l u d e<> #include<>#include<>#definemaxsize100typedefdoubledatatype1;typedefchardatatype2;typedefstructstack1{datatype1data1[maxsize];inttop1; /*栈顶元素*/}seqstack1,*pseqstack1;/*顺序栈*/typedefstructstack2{datatype2data2[maxsize];inttop2; /*栈顶元素*/}seqstack2,*pseqstack2;/*顺序栈*//*栈的初始化*/pseqstack1init_seqstack1(void){pseqstack1S;S=(pseqstack1)malloc(sizeof(pseqstack1));if(S)S->top1=-1;returnS;}pseqstack2init_seqstack2(void){pseqstack2S;S=(pseqstack2)malloc(sizeof(pseqstack2));if(S)S->top2=-1;returnS;}/*判断栈空*/intempty_seqstack1(pseqstack1S)if(S->top1==-1)return1;elsereturn0;}intempty_seqstack2(pseqstack2S){if(S->top2==-1)return1;elsereturn0;}/*X入栈*/intpush_seqstack1(pseqstack1S,datatype1X) {if(S->top1==maxsize-1){printf("栈满,无法入栈!\n");return0;}else{S->top1++;S->data1[S->top1]=X;return1;}}intpush_seqstack2(pseqstack2S,datatype2X) {if(S->top2==maxsize-1){printf("栈满,无法入栈!\n");return0;}else{S->top2++;S->data2[S->top2]=X;return1;}/*X出栈*/intpop_seqstack1(pseqstack1S,datatype1*X) {if(empty_seqstack1(S))return0;else{*X=S->data1[S->top1];S->top1--;return1;}}intpop_seqstack2(pseqstack2S,datatype2*X) {if(empty_seqstack2(S))return0;else{*X=S->data2[S->top2];S->top2--;return1;}}/*求栈顶元素*/intgettop_seqstack1(pseqstack1S,datatype1*X) {if(empty_seqstack1(S))return0;else*X=S->data1[S->top1];return1;}intgettop_seqstack2(pseqstack2S,datatype2*X) {if(empty_seqstack2(S))return0;else*X=S->data2[S->top2];return1;}/*判断字符是否为操作数。

若是返回1,否则返回0*/intisnum(charc){if(c>='0'&&c<='9')return1;elsereturn0;}/*求后缀表达式的值*/doublepostfix_exp(char*A){pseqstack1S; /*定义栈S*/doubleoperand=0;doubleresult; /*存放栈顶元素*/doublea; /*运算符ch前的操作数出栈存入a*/doubleb; /*运算符ch后的操作数出栈存入b*/doublec; /*c==achb*/charch; /*存放读取到的表达式(A)的字符*/ch=*A++; /*读表达式字符=>A*/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) /*求achb==,将结果赋给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*/returnresult;}/*优先级判断函数*/intpriority(charop){switch(op){case'#':return1;case')':return2;case'+':case'-':return3;case'*':case'/':return4;case'(':return5;default:return0;}}/*将指针infixexp指向的中缀表达式转换为指针postfixexp指向的后缀表达式*/ intinfix_exp_value(char*infixexp,char*postfixexp){pseqstack2S; /*定义栈S*/intcount=0;charw; /*存放读取到的表达式(infixexp)的字符*/charc; /*存放栈顶元素*/chartopelement;/*存出栈元素*/S=init_seqstack2(); /*初始化栈*/if(!S) /*栈的初始化判断*/{printf("栈初始化失败!");return0;}push_seqstack2(S,'#'); /*将结束符'#'加入运算符栈S中*/w=*infixexp; /*读表达式字符=>w*/while((gettop_seqstack2(S,&c),c)!='#'||w!='#')/*<3>栈顶元素不等于'#'或w不等于'#'时循环*/{if(isnum(w))/*判断w是否为操作数,若是直接输出,读下一个字符=>w,转<3>*/{if(count){*postfixexp='@';postfixexp++;count=0;}*postfixexp=w;postfixexp++;w=*(++infixexp);}else /*w若是运算符分类如下*/{count=1;if((gettop_seqstack2(S,&c),c)=='('&&w==')')/*如果栈顶为'('并且w 为')'则'('出栈不输出,读下一个字符=>w,转<3>*/{pop_seqstack2(S,&topelement);/*将'('出栈存入topelement*/w=*(++infixexp);}elseif((gettop_seqstack2(S,&c),c)=='('||priority((gettop_seqstack2(S,&c),c))<priority(w ))/*如果栈顶为'('或者栈顶优先级小于w优先级,则w入栈,读下一个字符=>w,转<3>*/{push_seqstack2(S,w);w=*(++infixexp);}else/*否则*//*从运算符栈中出栈并输出,转<3>*/ {pop_seqstack2(S,&topelement);*postfixexp=topelement;postfixexp++;}}}*postfixexp='#';/*在指针postfixexp指向的后缀表达式结尾追加字符'#'*/*(++postfixexp)='\0';/*在指针postfixexp指向的后缀表达式最后追加结束符'\0'*/return1;}/*主函数*/intmain(){inti=0;charA[maxsize];charB[maxsize];printf("请输入表达式,如:20+13#,必须以#号结尾!\n");/*1+2*(9+7)-4/2#23+((12*3-2)/4+34*5/7)+108/9#*/A[i]=getchar();while(A[i++]!='#'){A[i]=getchar();}A[i]='\0';infix_exp_value(A,B);printf("A==%s\n",A);printf("B==%s\n",B);printf("上式的结果为:");printf("%g\n",postfix_exp(B));return0;getch();}。

相关主题