📄 calculator.c
字号:
#include<reg52.h>
#include<math.h>
#include"data.h"
#include"delay12.h"
#include"lcd1602.h"
uchar temp,key,i,flag=0,z=4,flag1=0;
signed long a=0,b=0;
uchar code table2[]="0123456789+-*/= ";
uchar code table1[]={0xee,0xde,0xbe,0x7e,
0xed,0xdd,0xbd,0x7d,
0xeb,0xdb,0xbb,0x7b,
0xe7,0xd7,0xb7,0x77};
uchar code table3[]="0123456789 ";
uchar code table4[]="error";
uchar code table5[]="Please input";
uchar code table6[]=".";
void qingling()//清零函数
{
uchar x;
flag=0;
a=0,b=0;
init();
writecom(0x80+0x40);
for(x=0;x<12;x++)
{
writedata(table5[x]);
}
}
void display(a0,a1,a2,a3,a4,a5,a6,a7,a8)//1602显示函数
{
writecom(0x80+1);
writedata(table3[a0]);
writecom(0x80+2);
writedata(table3[a1]);
writecom(0x80+3);
writedata(table3[a2]);
writecom(0x80+4);
writedata(table3[a3]);
writecom(0x80+5);
writedata(table3[a4]);
writecom(0x80+6);
writedata(table3[a5]);
writecom(0x80+7);
writedata(table3[a6]);
writecom(0x80+8);//小数点
writedata(table6[0]);
writecom(0x80+9);//小数点后一位
writedata(table3[a7]);
writecom(0x80+10);//小数点后两位
writedata(table3[a8]);
}
void error()//提示错误
{
uchar x=0;
for(x=0;x<5;x++)
{
writecom(0x80+0x40+x);
writedata(table4[x]);
}
}
void turn(signed long c)//转换数字到1602上显示
{
uchar A0=0,A1=0,A2=0,A3=0,A4=0,A5=0,A6=0,A7=0,A8=0;
A0=c/100000000;if(A0==0)A0=10;
A1=c%100000000/10000000;if(A0==10){if(A1==0)A1=10;}
A2=c%100000000%10000000/1000000;if(A1==10){if(A2==0)A2=10;}
A3=c%100000000%10000000%1000000/100000;if(A2==10){if(A3==0)A3=10;}
A4=c%100000000%10000000%1000000%100000/10000;if(A3==10){if(A4==0)A4=10;}
A5=c%100000000%10000000%1000000%100000%10000/1000;if(A4==10){if(A5==0)A5=10;}
A6=c%100000000%10000000%1000000%100000%10000%1000/100;
A7=c%100000000%10000000%1000000%100000%10000%1000%100/10;
A8=c%100000000%10000000%1000000%100000%10000%1000%100%10/1;
display(A0,A1,A2,A3,A4,A5,A6,A7,A8);
}
int select(uchar num)
{
uchar y;
if(num==11)y=1;
if(num==12)y=2;
if(num==13)y=3;
if(num==14)y=4;
return y;
}
signed long jisuan(signed long m,signed long n,uchar v)//计算器计算部分
{
signed long t;
switch(v)
{
case 0: t=m+n;flag1=0;break;
case 1: t=m-n;flag1=0;break;
case 2: t=m*n;flag1=0;break;
case 3: if(n==0){error();flag1=1;}else t=(m*100)/n;flag1=2;break;
}return t;
}
void detection()//检测数字和符号a,b<65536.并显示
{
uchar A0=0,A1=0,A2=0,A3=0,A4=0,chu=0,num;
signed long s;
P1=0xf0;
if(P1!=0xf0)//检测按键
{
delays();
if(P1!=0xf0)
{
temp=P1;
P1=0x0f;
key=temp|P1;
for(i=0;i<16;i++)
if(key==table1[i])
{num=i;break;}
}
delay(30);
while(num==15)
{
num=16;
qingling();
}
if(flag==0) //输入并显示第一个数字
{
if(num<10)
{ init();
a=(num+a*10);
if(a>10000000)error();
else turn(a*100);
}
else
{
if(num<14)
{
init();
flag=1;
z=select(num);
writecom(0x80+15);
writedata(table2[num]);
}
}
}
if(flag==1)//输入并显示第二个数字
{
if(num<10)
{
b=num+b*10;
if(b>10000000)error();
else turn(b*100);
}
if(num==14)flag=2;
}
if(flag==2||num==14)//输出结果
{
init();
s=jisuan(a,b,z);
if(s<0)
{
writecom(0x80);
writedata(table2[11]);
s=fabs(s);
}
if(flag1==2)turn(s);
if(flag1==0)turn(s*100);
if(num==15)
{
while(num==15)
{
num=17;
init();
qingling();
}
}
flag=0;
a=s;
b=0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -