为学校实验周做的简单计算器,希望对大家有用,第一次发帖,不喜勿喷。
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x40};
long n=0,g,s,b,q,w1,w2,w3,w4,fuhao,n1,n2;
void delayms(uint xms)
{
uint i,j;
for(i=xms;i--;i>0)
for(j=110;j--;j>0);
}
void display(uchar g,uchar s,uchar b,uchar q,uchar w1,uchar w2,uchar w3,uchar w4)
{
long n4;
if(n>=0)
{
if(n==0)
{
P0=0Xfe;
P1=table[0];
}
if(n>0&n<=10)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
}
if(n>=10&n<100)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
}
if(n>=100&n<1000)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
}
if(n>=1000&n<10000)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
}
if(n>=10000&n<100000)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
}
if(n>=100000&n<1000000)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
w2=n/100000%10;
P1=table[w2];
P0=0xdf;
delayms(1);
}
if(n>=1000000&n<10000000)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
w2=n/100000%10;
P1=table[w2];
P0=0xdf;
delayms(1);
w3=n/1000000%10;
P1=table[w3];
P0=0xbf;
delayms(1);
}
if(n>=10000000&n<100000000)
{
g=n%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
w2=n/100000%10;
P1=table[w2];
P0=0xdf;
delayms(1);
w3=n/1000000%10;
P1=table[w3];
P0=0xbf;
delayms(1);
w4=n/10000000%10;
P1=table[w4];
P0=0x7f;
delayms(1);
}
if(n>=100000000)
{
P0=0X00;
P1=table[0];
}
}
if(n<0)
{
uchar fu=10;
n4=0-n;
if(n4<10)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
P0=0xfd;
P1=0x40;
delayms(1);
}
if(n4>=10&n4<100)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n4/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
P1=0x40;
P0=0xfb;
delayms(1);
}
if(n4>=100&n4<1000)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n4/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n4/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
P1=0x40;
P0=0xf7;
delayms(1);
}
if(n4>=1000&n4<10000)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n4/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n4/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n4/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
P1=0x40;
P0=0xef;
delayms(1);
}
if(n4>=10000&n4<100000)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n4/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n4/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n4/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n4/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
P1=0x40;
P0=0xdf;
delayms(1);
}
if(n4>=100000&n4<1000000)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n4/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n4/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n4/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n4/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
w2=n4/100000%10;
P1=table[w2];
P0=0xdf;
delayms(1);
P1=0x40;
P0=0xbf;
delayms(1);
}
if(n4>=1000000&n4<10000000)
{
g=n4%10;
P1=table[g];
P0=0xfe;
delayms(1);
s=n4/10%10;
P1=table[s];
P0=0xfd;
delayms(1);
b=n4/100%10;
P1=table[b];
P0=0xfb;
delayms(1);
q=n4/1000%10;
P1=table[q];
P0=0xf7;
delayms(1);
w1=n4/10000%10;
P1=table[w1];
P0=0xef;
delayms(1);
w2=n4/100000%10;
P1=table[w2];
P0=0xdf;
delayms(1);
w3=n4/1000000%10;
P1=table[w3];
P0=0xbf;
delayms(1);
P1=0x40;
P0=0xef;
delayms(1);
}
if(n4>=10000000)
{
P0=0X00;
P1=table[0];
}
}
}
void matrixkeyscan()
{
uchar temp;
P2=0xfe;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xee:
n=1+10*n;
n2=n;
break;
case 0xde:
n=2+10*n;
n2=n;
break;
case 0xbe:
n=3+10*n;
n2=n;
break;
case 0x7e:
fuhao=1;
n1=n;
n=0;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
display(g,s,b,q,w1,w2,w3,w4);
}
}
P2=0xfd;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xed:
n=4+10*n;
n2=n;
break;
case 0xdd:
n=5+10*n;
n2=n;
break;
case 0xbd:
n=6+10*n;
n2=n;
break;
case 0x7d:
fuhao=2;
n1=n;
n=0;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
display(g,s,b,q,w1,w2,w3,w4);
}
}
P2=0xfb;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xeb:
n=7+10*n;
n2=n;
break;
case 0xdb:
n=8+10*n;
n2=n;
break;
case 0xbb:
n=9+10*n;
n2=n;
break;
case 0x7b:
fuhao=3;
n1=n;
n=0;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
display(g,s,b,q,w1,w2,w3,w4);
}
}
P2=0xf7;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xe7:
n=0;
break;
case 0xd7:
n=0+10*n;
n2=n;
break;
case 0xb7:
if(fuhao==1)
{
n=n1+n2;
}
if(fuhao==2)
{
n=n1-n2;
}
if(fuhao==3)
{
n=n1*n2;
}
if(fuhao==4)
{
n=n1/n2;
}
break;
case 0x77:
fuhao=4;
n1=n;
n=0;
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
display(g,s,b,q,w1,w2,w3,w4);
}
}
}
void main()
{
while(1)
{
display(g,s,b,q,w1,w2,w3,w4);
matrixkeyscan();
}
}
|