用C语言写的计算器源代码.docx
用C语言写的计算器源代码用C语言写的计算器源代码.txt什么叫神话?请听男人向你表达爱意;什么叫传说?请听男人对你的承诺;什么叫梦境?请看你自己听到前两者时的反应。用C语言写的计算器源代码 #include<stdio.h> #include<iostream.h> #include<stdlib.h> #include<string.h> #include<ctype.h> typedef float DataType; typedef struct DataType *data; int max; int top; Stack; void SetStack(Stack *S,int n) S->data=(DataType*)malloc(n*sizeof(DataType); if(S->data=NULL) printf("overflow"); exit(1); S->max=n; S->top=-1; void FreeStack(Stack *S) free(S->data); int StackEmpty(Stack *S) if(S->top=-1) return(1); return(0); DataType Peek(Stack *S) if(S->top=S->max-1) printf("Stack is empty!n"); exit(1); return(S->dataS->top); void Push(Stack *S,DataType item) if(S->top=S->max-1) printf("Stack is full!n"); exit(1); S->top+; S->dataS->top=item; DataType Pop(Stack *S) if(S->top=-1) printf("Pop an empty stack!n"); exit(1); S->top-; return(S->dataS->top+1); typedef struct char op; int inputprecedence; int stackprecedence; DataType1; typedef struct DataType1 *data; int max; int top; Stack1; void SetStack1(Stack1 *S,int n) S->data=(DataType1*)malloc(n*sizeof(DataType1); if(S->data=NULL) printf("overflow"); exit(1); S->max=n; S->top=-1; void FreeStack1(Stack1 *S) free(S->data); int StackEmpty1(Stack1 *S) if(S->top=-1) return(1); return(0); DataType1 Peek1(Stack1 *S) if(S->top=S->max-1) printf("Stack1 is empty!n"); exit(1); return(S->dataS->top); void Push1(Stack1 *S,DataType1 item) if(S->top=S->max-1) printf("Stack is full!n"); exit(1); S->top+; S->dataS->top=item; DataType1 Pop1(Stack1 *S) if(S->top=-1) printf("Pop an empty stack!n"); exit(1); S->top-; return(S->dataS->top+1); DataType1 MathOptr(char ch) DataType1 optr; optr.op=ch; switch(optr.op) case'+': case'-': optr.inputprecedence=1; optr.stackprecedence=1; break; case'*': case'/': optr.inputprecedence=2; optr.stackprecedence=2; break; case'(': optr.inputprecedence=3; optr.stackprecedence=-1; break; case')': optr.inputprecedence=0; optr.stackprecedence=0; break; return(optr); void Evaluate(Stack *OpndStack,DataType1 optr) DataType opnd1,opnd2; opnd1=Pop(OpndStack); opnd2=Pop(OpndStack); switch(optr.op) case'+': Push(OpndStack,opnd2+opnd1); break; case'-': Push(OpndStack,opnd2-opnd1); break; case'*': Push(OpndStack,opnd2*opnd1); break; case'/': Push(OpndStack,opnd2/opnd1); break; int isoptr(char ch) if(ch='+'|ch='-'|ch='*'|ch='/'|ch='(') return(1); return(0); void Infix(char *str) int i,k,n=strlen(str); char ch,numstr10; DataType opnd; DataType1 optr; Stack OpndStack; Stack1 OptrStack; SetStack(&OpndStack,n); SetStack1(&OptrStack,n); k=0; ch=strk; while(ch!='=') if(isdigit(ch)|ch='.') for(i=0;isdigit(ch)|ch='.'i+) numstri=ch; k+; ch=strk; numstri='0' opnd= atof(numstr); Push(&OpndStack,opnd); else if(isoptr(ch) optr=MathOptr(ch); while(Peek1(&OptrStack).stackprecedence>=optr.inputprecedence) Evaluate(&OpndStack,Pop1(&OptrStack); Push1(&OptrStack,optr); k+; ch=strk; else if(ch=')') optr=MathOptr(ch); while(Peek1(&OptrStack).stackprecedence>=optr.inputprecedence) Evaluate(&OpndStack,Pop1(&OptrStack); Pop1(&OptrStack); k+; ch=strk; while(!StackEmpty1(&OptrStack) Evaluate(&OpndStack,Pop1(&OptrStack); opnd=Pop(&OpndStack); cout<<"你输入表达式的计算结果为"<<endl; printf("%-6.2fn",opnd); FreeStack(&OpndStack); FreeStack1(&OptrStack); void main cout<<"请输入你要计算的表达式,并以“=”号结束。"<<endl; char str50; gets(str); Infix(str);