#include<reg51.h>
#include<math.h>
sbit col0=P1^0;
sbit col1=P1^1;
sbit col2=P1^2;
sbit col3=P1^3;
sbit row0=P1^4;
sbit row1=P1^5;
sbit row2=P1^6;
sbit row3=P1^7;
int m,n,q;
dis[2];
int seg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};//0--9;
void key1();
void key2();
void display1();
void display2();
void delay(int i)
{
int k,j;
for(k=0;k<i;k++)
for(j=0;j<100;j++);
}
void key1() //检测是否有按键按下
{
while(1){
P1=0x0f;
if(P1!=0x0f)
delay(10);
if(P1!=0x0f)
break;
}
}
void key2() //检测哪个按键按下
{
P1=0xff;col0=0;
if(row0==0) n=1;
if(row1==0) n=2;
if(row2==0) n=3;
if(row3==0) n=4;
P1=0xff;col1=0;
if(row0==0) n=5;
if(row1==0) n=6;
if(row2==0) n=7;
if(row3==0) n=8;
P1=0xff;col2=0;
if(row0==0) n=9;
if(row1==0) n=0;
if(row2==0) {m=n;q=1;}//+
if(row3==0) {m=n;q=2;} //-
P1=0XFF;col3=0;
if(row0==0) {m=n;q=3;} //*
if(row1==0) {m=n;q=4;} // /
if(row2==0)
{
switch(q)
{
case 1: if((m+n)>=10) dis[0]=(m+n)/10;else dis[0]=10 ;dis[1]=(m+n)%10;break;//加:求和并拆字求显示值给dis[0]和dis[1]
case 2: if(m>n){dis[0]=10;dis[1]=m-n;}else {dis[0]=11;dis[1]=n-m;} break;//减:求差并拆字求显示值给dis[0]和dis[1]
case 3: if((m*n)>=10) dis[0]=(m*n)/10;else dis[0]=10;dis[1]=(m*n)%10;break;//
case 4: dis[0]=m/n;dis[1]=m%n*10/n;break;
}
display2();
}
}
void display1()
{
P2=0xfe;
P0=seg[n];
}
void display2()
{
while(1)
{
P2=0xfd; //11111101
if(q==4)
P0=seg[dis[0]]&0x7f; //加小数点
else
P0=seg[dis[0]];
delay(1);
P2=0xfe; //11111110
P0=seg[dis[1]];
delay(1);
}
}
void main()
{
while(1)
{
key1();
key2();
display1();
}
}