我想做一个基于52单片机的简易计算器,我已经把四则运算做出来了,但是容错和除零提示我实在不懂,求大佬帮忙解疑,下面是我的代码
- #include <reg51.h>
- #include <intrins.h>
- #include <ctype.h>
- #include <stdlib.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar operand1[9], operand2[9];
- uchar operator;
- void delay(uint);
- uchar keyscan();
- void disp(void);
- void buf(uint value);
- uint compute(uint va1,uint va2,uchar optor);
- uchar code table[] = {0xc0,0xf9,0xa4,0xb0,0x99,
- 0x92,0x82,0xf8,0x80,0x90,0xff};
- uchar dbuf[8] = {10,10,10,10,10,10,10,10};
-
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- uchar keyscan()
- {
- uchar skey;
-
- P1 = 0xfe;
- while((P1 & 0xf0) != 0xf0)
- {
- delay(3);
- while((P1 & 0xf0) != 0xf0)
- {
- switch(P1)
- {
- case 0xee: skey = '0'; break;
- case 0xde: skey = '1'; break;
- case 0xbe: skey = '2'; break;
- case 0x7e: skey = '3'; break;
-
- default: skey = '#';
- }
- while((P1 & 0xf0) != 0xf0)
- ;
- }
- }
- P1 = 0xfd;
- while((P1 & 0xf0) != 0xf0)
- {
- delay(3);
- while((P1 & 0xf0) != 0xf0)
- {
- switch(P1)
- {
- case 0xed: skey = '4'; break;
- case 0xdd: skey = '5'; break;
- case 0xbd: skey = '6'; break;
- case 0x7d: skey = '7'; break;
-
- default: skey = '#';
- }
- while((P1 & 0xf0) != 0xf0)
- ;
- }
- }
- P1 = 0xfb;
- while((P1 & 0xf0) != 0xf0)
- {
- delay(3);
-
- while((P1 & 0xf0) != 0xf0)
- {
- switch(P1)
- {
- case 0xeb: skey = '8'; break;
- case 0xdb: skey = '9'; break;
- case 0xbb: skey = '+'; break;
- case 0x7b: skey = '-'; break;
-
- default: skey = '#';
- }
- while((P1 & 0xf0) != 0xf0)
- ;
- }
- }
- P1 = 0xf7;
- while((P1 & 0xf0) != 0xf0)
- {
- delay(3);
- while((P1 & 0xf0) != 0xf0)
- {
- switch(P1)
- {
- case 0xe7: skey = '*'; break;
- case 0xd7: skey = '/'; break;
- case 0xb7: skey = '='; break;
- case 0x77: skey = '; break;
-
- default: skey = '#';
- }
- while((P1 & 0xf0) != 0xf0)
- ;
- }
- }
- return skey;
- }
- void main()
- {
- uint value1, value2, value;
- uchar ckey, cut1 = 0, cut2 = 0;
- uchar operator;
- uchar i, bool = 0;
- init:
- buf(0);
- disp();
- value = 0;
- cut1 = cut2 = 0;
- bool = 0;
- for(i = 0;i < 9;i++)
- {
- operand1[i] = '\0';
- operand2[i] = '\0';
- }
- while(1)
- {
- ckey = keyscan();
- if(ckey != '#')
- {
- if(isdigit(ckey))
- {
- switch(bool)
- {
- case 0:
- operand1[cut1] = ckey;
- operand1[cut1+1] = '\0';
- value1 = atoi(operand1);
- cut1++;
- buf(value1);
- disp();
- break;
- case 1:
- operand2[cut2] = ckey;
- operand2[cut2+1] = '\0';
- value2 = atoi(operand2);
- cut2++;
- buf(value2);
- disp();
- break;
-
- default: break;
- }
- }
- else if(ckey=='+'||ckey=='-'||ckey=='*'||ckey=='/')
- {
- bool = 1;
- operator = ckey;
- buf(0);
- dbuf[7] = 10;
- disp();
- }
- else if(ckey == '=')
- {
- value = compute(value1,value2,operator);
- buf(value);
- disp();
- while(1)
- {
- ckey = keyscan();
- if(ckey == ')
- if(ckey == ')
- else
- goto init;
- else
- {
- buf(value);
- disp();
- }
- }
- }
- else if(ckey == ')
- { goto init;}
- }
- disp();
- }
- }
- uint compute(uint va1,uint va2,uchar optor)
- {
- uint value;
- switch(optor)
- {
- case '+' : value = va1+va2; break;
- case '-' : value = va1-va2; break;
- case '*' : value = va1*va2; break;
- case '/' : value = va17a2; break;
- default : break;
- }
- return value;
- }
- void buf(uint val)
- {
- uchar i;
- if(val == 0)
- {
- dbuf[7] = 0;
- i = 6;
- }
-
- else
- for(i = 7; val > 0; i--)
- {
- dbuf[i] = val % 10;
- val /= 10;
- }
-
- for( ; i > 0; i--)
- dbuf[i] = 10;
- }
- void disp(void)
- {
- uchar bsel, n;
-
- bsel=0x01;
- for(n=0;n<8;n++)
- {
- P2=bsel;
- P0=table[dbuf[n]];
- bsel=_crol_(bsel,1);
- delay(3);
- P0=0xff;
- }
- }
复制代码 |