#include "calculator.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <math.h>
#include "infrared.h"
#define PRINTF 0 //为1则打印log
Calculator_S calculator_s;//实例化计算器需要的结构体
//计算器结构体全部清零
void CalculatorInit(Calculator_S *s)
{
memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
s->inputCharCnt = 0;
s->result = 0;
s->offset = 0;
s->errorFlag = 0;
}
//获取输入的数
//unsigned char getNumber(Calculator_S *s)
//{
// unsigned char keyValue = 0xFF;
//
// if(infraredValue != 0)//接收到新的键值
// {
// switch(infraredValue)
// {
// case 0x09://前括号
// s->inputCharBuf[s->inputCharCnt++] = ')';
// break;
// case 0x07:
// s->inputCharBuf[s->inputCharCnt++] = '(';
// break;
// case 0x43:
// if(s->inputCharCnt > 0)//删除一个字符
// {
// s->inputCharCnt--;
// s->inputCharBuf[s->inputCharCnt] = 0x00;
// }
// break;
// case 0x0d://删除所有字符
// s->inputCharCnt = 0;
// memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
// break;
// case 0x0c:
// keyValue = '1';
// break;
// case 0x18:
// keyValue = '2';
// break;
// case 0x5e:
// keyValue = '3';
// break;
// case 0x40:
// keyValue = '+';
// break;
// case 0x08:
// keyValue = '4';
// break;
// case 0x1c:
// keyValue = '5';
// break;
// case 0x5a:
// keyValue = '6';
// break;
// case 0x19:
// keyValue = '-';
// break;
// case 0x42:
// keyValue = '7';
// break;
// case 0x52:
// keyValue = '8';
// break;
// case 0x4a:
// keyValue = '9';
// break;
// case 0x45:
// keyValue = '*';
// break;
// case 0x44:
// keyValue = '.';
// break;
// case 0x16:
// keyValue = '0';
// break;
// case 0x15:
// keyValue = '=';
// break;
// case 0x47:
// keyValue = '/';
// break;
// default:
// keyValue = 0xFF;
// break;
// }
// if(keyValue != 0xFF)
// {
// if(keyValue == '=' || s->inputCharCnt >= 15)//输入的是=号
// {
// s->result = calculator(s);//进行计算
// s->inputCharCnt = sprintf(s->inputCharBuf, "%.2f", calculator_s.result);//将计算结果放入输入数组中,while循环中进行显示
// return 0;
// }
// else
// {
// /* 将输入的字符存入字符数组 */
// s->inputCharBuf[s->inputCharCnt++] = keyValue;
// return 1;
// }
// }
// return 1;
// }
// return 2;//return 2 表示没有新的按键
//}
//获取输入的数
unsigned char getNumber(Calculator_S *s)
{
unsigned char keyValue = 0xFF;
if(infraredValue != 0)//接收到新的键值
{
switch(infraredValue)
{
case 0x40://前括号
s->inputCharBuf[s->inputCharCnt++] = ')';
break;
case 0x44:
s->inputCharBuf[s->inputCharCnt++] = '(';
break;
case 0x43:
if(s->inputCharCnt > 0)//删除一个字符
{
s->inputCharCnt--;
s->inputCharBuf[s->inputCharCnt] = 0x00;
}
break;
case 0x0d://删除所有字符 200+
s->inputCharCnt = 0;
memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
break;
case 0x0c:
keyValue = '1';
break;
case 0x18:
keyValue = '2';
break;
case 0x5e:
keyValue = '3';
break;
case 0x15:
keyValue = '+';
break;
case 0x08:
keyValue = '4';
break;
case 0x1c:
keyValue = '5';
break;
case 0x5a:
keyValue = '6';
break;
case 0x07:
keyValue = '-';
break;
case 0x42:
keyValue = '7';
break;
case 0x52:
keyValue = '8';
break;
case 0x4a:
keyValue = '9';
break;
case 0x45: //CH-
keyValue = '*';
break;
case 0x19: //100+
keyValue = '.';
break;
case 0x16:
keyValue = '0';
break;
case 0x09:
keyValue = '='; //EQ
break;
case 0x47:
keyValue = '/'; //CH+
break;
default:
keyValue = 0xFF;
break;
}
if(keyValue != 0xFF)
{
if(keyValue == '=' || s->inputCharCnt >= 15)//输入的是=号
{
s->result = calculator(s);//进行计算
s->inputCharCnt = sprintf(s->inputCharBuf, "%.2f", calculator_s.result);//将计算结果放入输入数组中,while循环中进行显示
return 0;
}
else
{
/* 将输入的字符存入字符数组 */
s->inputCharBuf[s->inputCharCnt++] = keyValue;
return 1;
}
}
return 1;
}
return 2;//return 2 表示没有新的按键
}
float calculator(Calculator_S *s)
{
s->offset = 0;//计算偏移量回到输入数组开头
s->errorFlag = 0;//清除报警位
s->result = calculator_core();//调用计算函数进行计算
//清除状态标志位和输入数组
memset(s->inputCharBuf, 0x00, sizeof(s->inputCharBuf));
s->inputCharCnt = 0;
return s->result;//返回计算结果
}
//计算函数 reentrant表示可重入,(51单片机递归必须算好资源)
float calculator_core(void)reentrant
{
char sign = 0;//符号变量
float number[2] = {0, 0}; //输入的值转换成浮点数
char numberCnt = 0;//输入的数个数统计
sign = '+';//第一个数默认为+,因为-号会在转换成浮点数时转成负数
while(calculator_s.inputCharCnt > calculator_s.offset)//当输入字符个数大于偏移量时
{
/* 遇到左括号开始递归计算 */
if(calculator_s.inputCharBuf[calculator_s.offset] == '(')
{
#if PRINTF == 1
printf("递归入口\r\n");
#endif
calculator_s.offset++;
calculator_s.result = calculator_core();
}
/* i == 0时可以转换 */
if(calculator_s.offset == 0)
{
my_cal_atof(&calculator_s.result, (const char*)calculator_s.inputCharBuf, &calculator_s.offset);//字符串转float
}
/* 当本个字符是0-9数字,前一个字符不是0-9、.时,可以进行转换 */
else if(isdigit(calculator_s.inputCharBuf[calculator_s.offset])
&& (!isdigit(calculator_s.inputCharBuf[calculator_s.offset - 1]) && calculator_s.inputCharBuf[calculator_s.offset - 1] != '.'))
{
my_cal_atof(&calculator_s.result, (const char*)calculator_s.inputCharBuf, &calculator_s.offset);//字符串转float
}
/* 非数字或者到最后一个字符了 */
if(((calculator_s.inputCharBuf[calculator_s.offset] != ' ') && (!isdigit(calculator_s.inputCharBuf[calculator_s.offset])))
|| (calculator_s.offset == calculator_s.inputCharCnt - 1))
{
switch(sign)
{
case '+':
if(numberCnt == 2)
{
number[0] += number[1];
number[1] = calculator_s.result;
}
else
{
number[numberCnt++] = calculator_s.result;
}
break;
case '-':
if(numberCnt == 2)
{
number[0] += number[1];
number[1] = -calculator_s.result;
}
else
{
number[numberCnt++] = -calculator_s.result;
}
break;
case '*':
number[numberCnt - 1] *= calculator_s.result;//与前一个数相乘
break;
case '/':
if(calculator_s.result != 0)
{
number[numberCnt - 1] /= calculator_s.result;//与前一个数相除
}
else
{
calculator_s.errorFlag = 1;//标志被除数等于0
}
break;
case '%'://取余
number[numberCnt - 1] = number[numberCnt - 1] - (int)(number[numberCnt - 1] / calculator_s.result) * calculator_s.result;
break;
default:
break;
}
#if PRINTF == 1
printf("\r\nsign:%c,%.2f, %.2f\r\n", si
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![asm](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 41 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
xuanshang_yutou
- 粉丝: 81
- 资源: 19
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)