#include "reg52.h"#include "string.h"#include "stdio.h"#include "lcd1602.h"#include "keyboard.h"#define keydeng 14#define keyzuoyi 12code unsigned char KeyDisPlaybuf[16] = { '1', '2', '3', '+',\ '4', '5', '6', '-',\ '7', '8', '9', '*',\ '<', '0', '=', '/'};long Count(char *ch, char len)//简单计算器 仅支持加减乘除+-*/{ unsigned char i = 0; char Operation; long Num1=0,Num2=0; while(i<len && ch!='+' && ch!='-' && ch!='*' && ch!='/') //输入的数据 数值1 { Num1*=10; Num1+=ch-'0'; i++; } Operation=ch[i++]; while(i<len && ch!='=') //输入的数据 数值2 { Num2*=10; Num2+=ch-'0'; i++; } switch(Operation) //计算 { case'+':return Num1+Num2;//加法 case'-':return Num1-Num2;//减法 case'*':return Num1*Num2;//乘法 case'/':return Num1/Num2;//除法 } return 0;}void EasyCounter(void){ unsigned char buff[30]; unsigned char text[30]; unsigned char key; unsigned char i = 0; do { key = keycan();//读取键值 if(key!=0xff)//键值不等于0 { text[i++]=KeyDisPlaybuf[key];//记录键值对应的字符 text='\0'; LCD_ShowStr(0,0," "); LCD_ShowStr(16-strlen(text),0,text); //第一行显示字符 } if(key==keyzuoyi&&i>0)//按下"<"时删除一位 i-=2; }while(key!=keydeng);//按下'='时结束输入 sprintf(buff, "%ld", Count(text, strlen(text))); LCD_ShowStr(0,1," "); LCD_ShowStr(16-strlen(buff),1,buff); //第二行显示字符}void main(void) //主函数{ LCD_Init(); LCD_ShowStr(0,0, " 0"); while(1) { EasyCounter(); //按键扫描+计算+显示 }}
|