#include "common.h"
#include "LCD1602DRV.H"
#include "matrixkeypaddrv.h"
sbit clear=P3^2;
sbit BK=P3^3;
uchar buf[17]={0};
uchar bufIdx=0;
void main()
{
uchar scancode,keyValue;
uchar x,sign;
float a=0,b=0,c;
uchar isPointStatus=0;
uchar Point=0;
uchar flag=0;
Lcd1602Init();
SetRowCol(0,0);
printf(" Welcome ");
SetRowCol(1,0);
printf("calculate:");
while(1)
{
if(clear==0)
{
ClearLine(0);
ClearLine(1);
flag=0;
bufIdx=0;
Point=0;
isPointStatus=0;
}
if(BK==0)
{
printf("\b",GetKeyValue(scancode));
flag=1;
bufIdx=0;
Point=0;
isPointStatus=0;
}
scancode=GetScanCode();
if(scancode!=0)
{
keyValue=GetKeyValue(scancode);
if(x==0||x==2)
{
ClearLine(0);
ClearLine(1);
}
if(keyValue>='0'&&keyValue<='9')//显示0-9的数值
{
buf[bufIdx++]=keyValue;
printf("%c",(char)keyValue);
Point=1;
x=1;
}
else if((keyValue=='.')&&(isPointStatus==0)&&(Point!=0))//显示.
{
buf[bufIdx++]=keyValue;
printf("%c",(char)keyValue);
isPointStatus=1;
}
else if(((keyValue=='+')||(keyValue=='-')||(keyValue=='*')||(keyValue=='/'))&&(flag==0))//显示+-*/键值
{
sign=keyValue;
buf[bufIdx]=0;
sscanf(buf,"%f",&a);
ClearLine(0);
if(isPointStatus==1)
{
printf("%f",a);
}
else
{
printf("%.0f",a);
}
SetRowCol(0,6);
printf("%c",sign);
flag=1;
bufIdx=0;
Point=0;
isPointStatus=0;
ClearLine(1);
}
else if(keyValue=='=')//显示等号
{
buf[bufIdx]=0;
sscanf(buf,"%f",&b);
if(sign=='/'&&b==0)
{
ClearLine(0);
printf("input error");
ClearLine(1);
flag=0;
bufIdx=0;
Point=0;
isPointStatus=0;
}
else//算数运算部分
{
switch(sign)
{
case '+':c=a+b;break;
case '-':c=a-b;break;
case '*':c=a*b;break;
case '/':c=a/b;break;
default :break;
}
if(isPointStatus==1)
{
ClearLine(0);
printf("%f%c%f",a,sign,b);
ClearLine(1);
printf("=%f",c);
}
else
{
ClearLine(0);
printf("%.0f%c%.0f",a,sign,b);
ClearLine(1);
if(sign=='/')
printf("=%f",c);
else
printf("=%.0f",c);
}
bufIdx=0;
isPointStatus=0;
flag=0;
x=2;
}
}
}
}
}
简易计算器_lcd1602计算器_基于AT89C51的简易计算器_用LCD写计算器_
5星 · 超过95%的资源 187 浏览量
2021-10-01
14:52:35
上传
评论 2
收藏 73KB ZIP 举报
呼啸庄主
- 粉丝: 74
- 资源: 4702
最新资源
- 基于matlab实现车牌识别程序,和论文,自己做的,做毕业设计的可以看看 .rar
- Windows系统下安装与配置Neo4j的步骤
- 基于matlab实现潮流计算和最优潮流计算的程序1,对毕业设计有一定用处.rar
- 基于大数据学习资源推荐系统的设计与实现(部署视频)-kaic.mp4
- 哈工大形式语言和自动机2022期末含答案
- Windows系统下安装与配置Neo4j的步骤
- 哈希算法(Hash Algorithm)是一种将任意长度的二进制数据映射为较短的、固定长度的二进制值的函数.txt
- Windows系统下安装与配置Neo4j的步骤
- 在二叉树或更复杂的树形结构中,先序输出叶结点.txt
- 列出所有祖先结点的概念通常与树形结构或图论中的节点相关.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论11