简易计算器


内容不做详细解释,本人比较搓,没有用什么高深的算法,完全按照自己的理解写了一个自己能够读懂的算法,打算按照这个算法再写一个PHP版及JS版的简易计算器。
如果想下载源码及DSN文件请到http://u.xunzai.com/fileview_251752.html
如若需要PROTEUS安装文件请登录

fu************@16*.com











密码:19881219
请不要随意修改密码。
转载请注明:本文转自https://www.fujiangyun.com/

/**************************************
工程名:简易计算器
作 者:付江云
***************************************/
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int

//定义位选编码
#define WEI1 0x02
#define WEI2 0x04
#define WEI3 0x08
#define WEI4 0x10

//定义运算操作符代号
#define add 0x0c //加
#define dec 0x0d //减
#define mul 0x0e //乘
#define div 0x0f //除
#define den 0x0b //等于
#define sur 0x0a //清零

uchar step; //记录操作步骤的变量
uchar sybol; //记录运算符号的变量
uint fir; //记录第一个操作数的变量
uint sec; //记录第二个操作数的变量
uint res; //记录结果的变量
uint tempres; //记录中间结果的变量

uchar code num[] = {
0x0c0,//0
0x0f9,//1
0x0a4,//2
0x0b0,//3
0x099,//4
0x092,//5
0x082,//6
0x0f8,//7
0x080,//8
0x090 //9
}; //数字0~9的编码

uchar code err[] = {
0x0ff,//NULL
0x086,//E
0x0af,//R
0x0af//R
}; //单词 ERR的编码

void delayms(uint z)
{
uint i,j;
for(i=z;i>0;i–)
for(j=110;j>0;j–);
}

uchar getkey() //键盘扫描函数
{
uchar result;
uchar TEMPP1;

//扫描第一列按键
P1 = 0x0fe;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
delayms(5);
P1 = 0x0fe;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
switch(TEMPP1)
{
case(0x0e0):
result = 0;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fe;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0d0):
result = 4;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fe;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0b0):
result = 8;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fe;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x070):
result = 12;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fe;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
}
}
}

//扫描第二列按键
P1 = 0x0fd;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
delayms(5);
P1 = 0x0fd;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
switch(TEMPP1)
{
case(0x0e0):
result = 1;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fd;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0d0):
result = 5;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fd;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0b0):
result = 9;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fd;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x070):
result = 13;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fd;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
}
}
}

//扫描第三列按键
P1 = 0x0fb;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
delayms(5);
P1 = 0x0fb;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
switch(TEMPP1)
{
case(0x0e0):
result = 2;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fb;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0d0):
result = 6;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fb;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0b0):
result = 10;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fb;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x070):
result = 14;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0fb;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
}
}
}

//扫描第四列按键
P1 = 0x0f7;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
delayms(5);
P1 = 0x0f7;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
if(TEMPP1 != 0x0f0)
{
switch(TEMPP1)
{
case(0x0e0):
result = 3;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0f7;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0d0):
result = 7;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0f7;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x0b0):
result = 11;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0f7;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
case(0x070):
result = 15;
while(TEMPP1 != 0x0f0)
{
P1 = 0x0f7;
TEMPP1 = P1;
TEMPP1 &= 0x0f0;
}
return result;
}
}
}
P1 = 0x0ff;
TEMPP1 = 0x0ff;
result = 0x0ff;
return result;
}

uint jisuan()
{
uint result;
switch(sybol)
{
case(add):
result = fir + sec;break;
case(dec):
result = fir – sec;break;
case(mul):
result = fir * sec;break;
case(div):
result = fir / sec;break;
}
return result;
}

void init() //变量初始化
{
step = 0;
fir = 0;
sec = 0;
res = 0;
tempres = 0;
}

void display(uint dat) //数码管显示数字
{
uchar q,b,s,g;
if(dat<0||dat>9999) //显示错误
{
P2 = WEI1;
P0 = err[0];
delayms(5);

P2 = WEI2;
P0 = err[1];
delayms(5);

P2 = WEI3;
P0 = err[2];
delayms(5);

P2 = WEI4;
P0 = err[3];
delayms(5);
}
else
{
q = dat/1000;
b = (dat/100)%10;
s = (dat/10)%10;
g = dat%10;
if(q==0)
{
if(b==0)
{
if(s==0)
{
P2 = WEI4;
P0 = num[g];
delayms(5);
}
else
{
P2 = WEI3;
P0 = num[s];
delayms(5);
P2 = WEI4;
P0 = num[g];
delayms(5);
}
}
else
{
P2 = WEI2;
P0 = num[b];
delayms(5);
P2 = WEI3;
P0 = num[s];
delayms(5);
P2 = WEI4;
P0 = num[g];
delayms(5);
}
}
else
{
P2 = WEI1;
P0 = num[q];
delayms(5);
P2 = WEI2;
P0 = num[b];
delayms(5);
P2 = WEI3;
P0 = num[s];
delayms(5);
P2 = WEI4;
P0 = num[g];
delayms(5);
}
}
}

void main()
{
uchar getnum;
init();
while(1)
{
//获取按键部分
getnum = getkey();
if(step==0 && getnum<10)
fir = fir*10 +getnum;
if(step==0 && (getnum>=12&&getnum<=15))
{
sybol = getnum;
sec = 0;
step = 1;
}
if(step==1 && (getnum>12&&getnum<=15))
sybol = getnum;
if(step==1 && getnum<10)
{
step = 2;
sec = sec*10 + getnum;
getnum = 0x0ff;
}
if(step==2 && getnum<10)
sec = sec*10 +getnum;
if(step==2 && getnum==11)
{
res = jisuan();
fir = 0;
sec = 0;
step = 3;
}
if(getnum == 10)
{
step = 0;
fir = 0;
sec = 0;
res = 0;
}

//显示部分
if(step==0 || step==1)
display(fir);
if(step==2)
display(sec);
if(step==3)
display(res);
}
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注