資源描述:
《c語(yǔ)言計(jì)算器源代碼.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、#include#include#include#definemaxsize100typedefdoubledatatype1;typedefchardatatype2;typedefstructstack1{datatype1data1[maxsize];inttop1;/*棧頂元素*/}seqstack1,*pseqstack1;/*順序棧*/typedefstructstack2{datatype2data2[maxsize];inttop2;/*棧頂元素*/}seqstack2,*pseq
2、stack2;/*順序棧*//*棧的初始化*/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
3、){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("棧滿,無(wú)法入棧!");return0;}else{S->top1++;S->data1[S->top1]=X;return1;}}intpush_seqstack2(pseqstac
4、k2S,datatype2X){if(S->top2==maxsize-1){printf("棧滿,無(wú)法入棧!");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
5、){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
6、->data2[S->top2];return1;}/*判斷字符是否為操作數(shù)。若是返回1,否則返回0*/intisnum(charc){if(c>='0'&&c<='9')return1;elsereturn0;}/*求后綴表達(dá)式的值*/doublepostfix_exp(char*A){pseqstack1S;/*定義棧S*/doubleoperand=0;doubleresult;/*存放棧頂元素*/doublea;/*運(yùn)算符ch前的操作數(shù)出棧存入a*/doubleb;/*運(yùn)算符ch后的操作數(shù)出棧存入b*/doublec;/*c==achb*/ch
7、arch;/*存放讀取到的表達(dá)式(A)的字符*/ch=*A++;/*讀表達(dá)式字符=>A*/S=init_seqstack1();/*初始化棧*/while(ch!='#')/*遇到元素!='#'時(shí)*/{if(isnum(ch))/*判斷ch是否為數(shù)字字符,計(jì)算出操作數(shù)*/operand=operand*10+(ch-'0');else/*否則*/{if(operand){push_seqstack1(S,operand);/*當(dāng)前字符不是數(shù)字,操作數(shù)結(jié)束,要入棧*/operand=0;}if(ch!='@'&&ch!=''){pop_seqstack1
8、(S,&b);/*運(yùn)算符ch后的操作數(shù)出棧存入b*/pop_seqstack1(S,&a);/