#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
char a[16]={0};
char b[16]={0};
uchar dm[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x79,0x40};
uchar wx[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar m,n,l,smz1,smz,zhi,flag,cp;
char i,j,k;
bit flag1,flag2,flag3;
sbit key1=P1^3;
sbit key2=P1^2;
void delay()
{
for(i=0;i<100;i++);
}
void jia()
{
for(i=0;i<16;i++)
{
a[i]=a[i]+b[i]+cp;
if(a[i]>=10)
{
a[i]=a[i]-10;
cp=1;
}
else
cp=0;
}
}
void jian()
{
flag3=0;
for(i=15;i>=0;i--)
{
if(a[i]==b[i])
continue;
else if(a[i]>b[i])
{
flag3=1;
break;
}
else
break;
}
if(flag3==1)
{
for(i=0;i<16;i++)
{
a[i]=a[i]-cp-b[i];
if(a[i]<0)
{
a[i]=a[i]+10;
cp=1;
}
else
cp=0;
}
for(i=15;i>=0;i--)
if(a[i]!=0)
{
a[i+1]=11;
break;
}
}
else
{
for(i=0;i<16;i++)
{
a[i]=b[i]-cp-a[i];
if(a[i]<0)
{
a[i]=10+a[i];
cp=1;
}
else
cp=0;
}
}
}
void chen()
{
}
void chu()
{
}
uchar key()
{
P2=0x0f;
smz=P2;
P2=0xf0;
smz1=P2;
smz=smz+smz1;
return smz;
}
void js()
{
cp=0;
switch(flag)
{
case 0xfe: jia();break;
case 0xfd: jian();break;
case 0xfc: chen();break;
case 0xfb: chu();break;
}
}
void ys()
{
flag1=1;
if(zhi==0xee)
{
js();
flag2=1;
}
else
{
js();
flag=zhi;
flag2=0;
}
for(j=0;j<16;j++)
{
b[j]=a[j];
a[j]=0;
}
}
void yw()
{
flag1=0;
m=0;
if(flag2==1)
{
for(i=0;i<16;i++)
b[i]=0;
flag2=0;
}
if(a[7]==0)
{
for(i=6;i>0;i--)
a[i+1]=a[i];
a[1]=a[0];
a[0]=zhi;
}
}
void pd()
{
switch(key())
{
case 0xff:zhi=10;break;
case 0xed:zhi=1;while(key()==0xed);break;
case 0xdd:zhi=2;while(key()==0xdd);break;
case 0xbd:zhi=3;while(key()==0xbd);break;
case 0xeb:zhi=4;while(key()==0xeb);break;
case 0xdb:zhi=5;while(key()==0xdb);break;
case 0xbb:zhi=6;while(key()==0xbb);break;
case 0xe7:zhi=7;while(key()==0xe7);break;
case 0xd7:zhi=8;while(key()==0xd7);break;
case 0xb7:zhi=9;while(key()==0xb7);break;
case 0xde:zhi=0;while(key()==0xde);break;
case 0xee:zhi=0xff;while(key()==0xee);break;
case 0x7e:zhi=0xfe;while(key()==0x7e);break;
case 0x7d:zhi=0xfd;while(key()==0x7d);break;
case 0x7b:zhi=0xfc;while(key()==0x7b);break;
case 0x77:zhi=0xfb;while(key()==0x77);break;
case 0xbe:zhi=0xee;while(key()==0xbe);break;
}
if(zhi==0xff)
{
m=0;
for(j=0;j<16;j++)
{
a[j]=0;
b[j]=0;
}
}
else if(zhi==0xfe||zhi==0xfd||zhi==0xfc||zhi==0xfb||zhi==0xee)
{
ys();
}
else if(zhi==10);
else
{
yw();
}
}
void display()
{
if(flag1==0)
{
for(j=15;j>0;j--)
{
if(a[j]!=0)
{
if(j>=8)
{
a[0]=10;
for(i=1;i<8;i++)
a[i]=0;
m=0;
break;
}
m=j;
break;
}
}
for(j=0;j<m+1;j++)
{
n=a[j];
P0=~dm[n];
key1=1;
key1=0;
P0=wx[j];
key2=1;
key2=0;
delay();
}
}
else
{
for(j=15;j>0;j--)
{
if(b[j]!=0)
{
if(j>=8)
{
b[0]=10;
for(i=1;i<8;i++)
b[i]=0;
m=0;
break;
}
m=j;
break;
}
}
for(j=0;j<m+1;j++)
{
n=b[j];
P0=~dm[n];
key1=1;
key1=0;
P0=wx[j];
key2=1;
key2=0;
delay();
}
}
}
void main()
{
flag1=0;
cp=0;
m=0;
flag=0xfe;
while(1)
{
pd();
display();
}
}