《简单四则运算计算器.doc》由会员分享,可在线阅读,更多相关《简单四则运算计算器.doc(9页珍藏版)》请在金锄头文库上搜索。
1、#include/* 就是需要引用这个图形库*/#include#include/* 控制台输入输出头文件,getch()语句需要*/#include#include#include#define TRUE 1#define FALSE 0#define Stack_Size 50/*存储空间初始分配量*/char ops7=+,-,*,/,(,),#;int cmp77=2,2,1,1,1,2,2,2,2,1,1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,2,2,1,1,1,1,1,3,0,2,2,2,2,0,2,2,1,1,1,1,1,0,3;typedef struct
2、char elemStack_Size;int top;SeqStack;/*运算数栈的定义*/typedef structint elemStack_Size;int top;SeqStackOperand;/*初始化运算符栈*/void InitStack(SeqStack *S)S-top =-1;/*初始化运算数栈*/void InitStacknOperand(SeqStackOperand *S)S-top =-1;/*判断栈S为空栈时返回值为真,反之为假*/int IsEmpty(SeqStack *S)return(S-top=-1?TRUE:FALSE);/*判断栈S为空栈时
3、返回值为真,反之为假*/int IsEmptynOperand(SeqStackOperand *S)return(S-top=-1?TRUE:FALSE);/*判断栈S为满栈时返回值为真,反之为假*/int IsFull(SeqStack *S)return(S-top=Stack_Size-1?TRUE:FALSE);/*判断栈S为满栈时返回值为真,反之为假*/int IsFullOperand(SeqStackOperand *S)return(S-top=Stack_Size-1?TRUE:FALSE);/*运算符栈入栈函数*/int Push(SeqStack *S,char x)i
4、f (S-top=Stack_Size-1)printf(Stack is full!n);return FALSE;elseS-top+;S-elemS-top=x;return TRUE;/*运算数栈入栈函数*/int PushOperand(SeqStackOperand *S,int x)if (S-top=Stack_Size-1)printf(Stack is full!n);return FALSE;elseS-top+;S-elemS-top=x;return TRUE;/*运算符栈出栈函数*/int Pop(SeqStack *S,char *x)if (S-top=-1)p
5、rintf(运算符栈空!n);return FALSE;else*x=S-elemS-top;S-top-;return TRUE;/*运算数栈出栈函数*/int PopOperand(SeqStackOperand *S,int *x)if (S-top=-1)printf(运算符栈空!n);return FALSE;else*x=S-elemS-top;S-top-;return TRUE;/*运算符栈取栈顶元素函数*/char GetTop(SeqStack *S)if (S-top =-1)printf(运算符栈为空!n);return FALSE;elsereturn (S-elem
6、S-top);/*运算数栈取栈顶元素函数*/int GetTopOperand(SeqStackOperand *S)if (S-top =-1)printf(运算符栈为空!n);return FALSE;elsereturn (S-elemS-top);/*判断输入字符是否为运算符函数,是返回TRUE,不是返回FALSE*/int Isoperator(char ch)int i;for (i=0;i7;i+)if(ch=opsi)return TRUE;return FALSE;char Compare(char ch1,char ch2)int i,j,k;char pri;int pr
7、iority;for(i=0;i;break;case 2:pri=;break;case 3:pri=;break;case 4:pri=$;printf(错误!n);break;return pri;int Execute(int num1,char op,int num2) /*进行单个算术运算*/int Rusult;switch(op) case +: Rusult=num1+num2; break;case -: Rusult=num1-num2; break;case *: Rusult=num1*num2; break;case /: if(num2=0) printf(nEr
8、ror!n);else Rusult=num1/num2;break;return(Rusult);int ExpEvaluation(char *str)int a,b,v,temp;char ch,op; int i=0;SeqStack operatordata;SeqStackOperand operand;InitStack(&operatordata);InitStacknOperand(&operand);Push(&operatordata,#); ch=*str+;while(ch!=# | GetTop(&operatordata)!=#)if(!Isoperator(ch
9、)temp=ch-0; /*将字符转换为十进制数*/ch=*str+;i+;while(!Isoperator(ch)temp=temp*10 + ch-0; /*将逐个读入运算数的各位转化为十进制数*/ch=*str+;i+;PushOperand(&operand,temp);elseswitch(Compare(GetTop(&operatordata),ch)case :Pop(&operatordata,&op);PopOperand(&operand,&b);PopOperand(&operand,&a);v=Execute(a,op,b); /*对a和b进行op运算*/PushO
10、perand(&operand,v);break;/*end while*/v=GetTopOperand(&operand);return v;void MainFace()int i,j;initgraph(400,250);/*初始化*/bar(10,10,390,240);setcolor(WHITE); /*图形边框颜色设置*/rectangle(20,20,380,230); setcolor(LIGHTRED);bar(30,30,370,70); for(i=0;i3;i+)for(j=0;j7;j+)/setcolor(BLACK);rectangle(30+50*j,80+50*i,70+50*j,120+50*i); circle(50+50*j,100+50*i,20); setcolor(RED);outtextxy (46,90,7);outtextxy(96,90,8);outtextxy(146,90,9);outtextxy(196,90,C);outtextxy(246,90,+);outtextxy(296,90,();outtextxy(346,90,);outtextxy(46,140,4);outtextxy(96,140,5);outtextxy(146,140,6);outtextxy(196,140