怎样才能使这个简易计算器显示负数 比如4-3=-1
时间:10-02
整理:3721RD
点击:
#include <AT89X51.H>
#define uchar unsigned char
uchar code dispcode[]={0xFA,0x22,0xB9,0xAB,0x63,0xCB,0xDB,0xA2,0xFB,0xEB,0x01,0};
uchar disp[6];//={1,0,2,3,4,5};
long value=0;
uchar op=10;
uchar flag=1;
void delay(void)
{
uchar i;
for(i=250;i>0;i--);
}
void display()
{
uchar i,t=0;
uchar code w[]={0x10,0x20,0x01,0x02,0x04,0x08};
for(i=0;i<6;i++)
{
P2=0;
P0=dispcode[disp[i]];
if((t==0)&&(disp[i]==0)&&(i<5))P2=0;
else
{
P2=w[i];
t=1;
}
//P2=k;
//k=k<<1;
//if(k==0)k=0x01;
delay();
P2=0;
}
}
uchar inkey()
{
uchar i,j=0x01,k;
uchar code keytab[16]={0x81,0x41,0x21,0x11,0x82,0x42,0x22,0x12,0x84,
0x44,0x24,0x14,0x88,0x48,0x28,0x18};
for(i=0;i<4;i++)
{
P3=~j;
k=(~P3)&0XF0;
if(k!=0)break;
j=j<<1;
}
k=k+j;
// disp[0]=k/16;
// disp[1]=k%16;
for(i=0;i<16;i++)
{
if(keytab[15-i]==k)break;
}
// disp[2]=i/10;disp[3]=i%10;
return i;
}
void long2disp(long n)
{
uchar i;
for(i=5;i>0;i--)
{
disp[i]=n%10;
n=n/10;
}
disp[0]=n;
}
long disp2long()
{
uchar i;
long n=0;
for(i=0;i<6;i++)
{ n=n*10+disp[i]; }
return n;
}
void key_action(uchar key)
{
uchar i;
if(key<=9)
{
if(flag==0)
{
for(i=0;i<5;i++){disp[i]=disp[i+1];}
disp[5]=key;
}
else
for(i=0;i<5;i++)disp[i]=0;
disp[5]=key;
flag=0;
}
else if(key==14)
{value=0;long2disp(value);op=10; flag=1;}
else
{
if(flag==0)
{
switch(op)
{
case 10:value=value+disp2long();break;
case 11:value=value-disp2long();break;
case 12:value=value*disp2long();break;
case 13:value=value/disp2long();break;
//case 14:value=0;break;
case 15:value=value;break;
}
if(value>199999)
{
value=0;
}
long2disp(value);
flag=1;
}
op=key;
}
}
void main()
{
uchar key;
//P0=0x40;
while(1)
{
key=inkey();
if(key<16)
{
display();
if(key==inkey())
{
key_action(key);
while(key==inkey())
{display();}
}
}
display();
}
}
#define uchar unsigned char
uchar code dispcode[]={0xFA,0x22,0xB9,0xAB,0x63,0xCB,0xDB,0xA2,0xFB,0xEB,0x01,0};
uchar disp[6];//={1,0,2,3,4,5};
long value=0;
uchar op=10;
uchar flag=1;
void delay(void)
{
uchar i;
for(i=250;i>0;i--);
}
void display()
{
uchar i,t=0;
uchar code w[]={0x10,0x20,0x01,0x02,0x04,0x08};
for(i=0;i<6;i++)
{
P2=0;
P0=dispcode[disp[i]];
if((t==0)&&(disp[i]==0)&&(i<5))P2=0;
else
{
P2=w[i];
t=1;
}
//P2=k;
//k=k<<1;
//if(k==0)k=0x01;
delay();
P2=0;
}
}
uchar inkey()
{
uchar i,j=0x01,k;
uchar code keytab[16]={0x81,0x41,0x21,0x11,0x82,0x42,0x22,0x12,0x84,
0x44,0x24,0x14,0x88,0x48,0x28,0x18};
for(i=0;i<4;i++)
{
P3=~j;
k=(~P3)&0XF0;
if(k!=0)break;
j=j<<1;
}
k=k+j;
// disp[0]=k/16;
// disp[1]=k%16;
for(i=0;i<16;i++)
{
if(keytab[15-i]==k)break;
}
// disp[2]=i/10;disp[3]=i%10;
return i;
}
void long2disp(long n)
{
uchar i;
for(i=5;i>0;i--)
{
disp[i]=n%10;
n=n/10;
}
disp[0]=n;
}
long disp2long()
{
uchar i;
long n=0;
for(i=0;i<6;i++)
{ n=n*10+disp[i]; }
return n;
}
void key_action(uchar key)
{
uchar i;
if(key<=9)
{
if(flag==0)
{
for(i=0;i<5;i++){disp[i]=disp[i+1];}
disp[5]=key;
}
else
for(i=0;i<5;i++)disp[i]=0;
disp[5]=key;
flag=0;
}
else if(key==14)
{value=0;long2disp(value);op=10; flag=1;}
else
{
if(flag==0)
{
switch(op)
{
case 10:value=value+disp2long();break;
case 11:value=value-disp2long();break;
case 12:value=value*disp2long();break;
case 13:value=value/disp2long();break;
//case 14:value=0;break;
case 15:value=value;break;
}
if(value>199999)
{
value=0;
}
long2disp(value);
flag=1;
}
op=key;
}
}
void main()
{
uchar key;
//P0=0x40;
while(1)
{
key=inkey();
if(key<16)
{
display();
if(key==inkey())
{
key_action(key);
while(key==inkey())
{display();}
}
}
display();
}
}
小编,4减3不等于-1
可以用这样的算法,就是先判断例如3-4,先判断3大于等于4么如果不大于就使数码管的中间一个led灯亮,再用4-3得到后面的数值。如果3不大于4则正常显示。