mcu_hi3321_watch/tjd/ui/app/calculator/TjdUiAppCalculatorModel.cpp
2025-05-26 20:15:20 +08:00

288 lines
6.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "TjdUiAppCalculatorModel.h"
#include "TjdUiAppCalculatorPresenter.h"
#include "sql_setting.h"
#include "sys_config.h"
#include <cmath>
#include <stdio.h>
#include <string.h>
namespace TJD {
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印一般常开
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
#define FONT_SIZE 54
extern struct UiCalculator *__calculator ;
extern uint8_t last_mode;
extern int count;
static TjdUiAppCalculatorModel *g_pv_CalculatorModel = nullptr;
//重置参数
void TjdUiAppCalculatorModel::CalculatorReset(void)
{
__this->num_1=0.0;
__this->num_2=0.0;
__this->point=0;
__this->cal_mode=NONE;
__this->error = 0;
memset(__this->str,0,SHOW_NUM_MAX);
__this->str[0] = '0';
}
//字符转浮点
double TjdUiAppCalculatorModel::__atof(char* str)
{
double s=0.0;
double d=10.0;
int jishu=0;
bool falg=false;
while(*str==' ')
{
str++;
}
if(*str=='-')//记录数字正负
{
falg=true;
str++;
}
if(!(*str>='0'&&*str<='9'))//如果一开始非数字则退出返回0.0
return s;
while(*str>='0'&&*str<='9'&&*str!='.')//计算小数点前整数部分
{
s=s*10.0+*str-'0';
str++;
}
if(*str=='.')//以后为小数部分
str++;
while(*str>='0'&&*str<='9')//计算小数部分
{
s=s+(*str-'0')/d;
d*=10.0;
str++;
}
s=s*(falg?-1.0:1.0);
return s;
}
//运算函数
double TjdUiAppCalculatorModel::__calculator_add(double A,double B){
printf("%s A=%f B=%f A+B=%f\n",__func__,A,B,(A+B));
return (A+B);
}
double TjdUiAppCalculatorModel::__calculator_sub(double A,double B){
printf("%s A=%f B=%f A-B=%f\n",__func__,A,B,(A-B));
return (A-B);
}
double TjdUiAppCalculatorModel::__calculator_mul(double A,double B){
printf("%s A=%f B=%f A*B=%f\n",__func__,A,B,(A*B));
return (A*B);
}
double TjdUiAppCalculatorModel::__calculator_div(double A,double B){
if(std::fabs(B)< 1e-6){//除0判断
return 0;
}
printf("%s A=%f B=%f A/B=%f\n",__func__,A,B,(A/B));
return (A/B);
}
//清除字符
void TjdUiAppCalculatorModel::StringClear(void)
{
memset(__this->str,0,SHOW_NUM_MAX);
__this->str[0] = '0';
}
//输入函数
int TjdUiAppCalculatorModel::StringInput(char str)
{
int len = 0;
if(__this->error == 1){
printf("error");
return -1;
}
// if(__this->cal_mode==DIV && str=='0'){
// printf("除数为0");
// __this->error = 1;
// }
if(str=='0' && (strcmp(__this->str,"0")==0)){
printf("0输入重复");
return -1;
}
else if(((strcmp(__this->str,"0")==0) && __this->cal_mode==NONE) || __this->cal_mode != NONE){
__this->point = 0;
if(last_mode == EQUAL){
last_mode = 0;
__this->num_1 = 0;
if(str=='.'){
StringClear();
}else{
memset(__this->str, 0, SHOW_NUM_MAX);
}
}else{
if(str=='.'){
StringClear();
}else{
memset(__this->str, 0, SHOW_NUM_MAX);
}
}
}
len = strlen(__this->str);
if(str == '.'){
if(__this->point==1){
return -1;
}else if(len < SHOW_NUM_MAX){
__this->point = 1;
}
}
if(len >= SHOW_NUM_MAX){
return -1;
}
std::string strr(__this->str);
strr+=str;
sprintf(__this->str,"%s%s",__this->str,strr.c_str());
__this->num_2 = __atof(__this->str);
__this->cal_mode = NONE;
return 0;
}
//清除所有
int TjdUiAppCalculatorModel::CalculationClear(void)
{
// CalculatorView::GetInstance()->GetResultLabel()->SetPosition(RESULT_LABEL_X, 100);
CalculatorView::GetInstance()->GetResultLabel()->SetFont(TJD_VECTOR_FONT_FILENAME,FONT_SIZE );
CalculatorView::GetInstance()->GetResultLabel1()->SetFont(TJD_VECTOR_FONT_FILENAME, FONT_SIZE);
CalculatorView::GetInstance()->GetResultLabel1()->SetText("");
CalculatorReset();
last_mode = 0;
return 0;
}
//回退单字符
int TjdUiAppCalculatorModel::CalculationBack(void)
{
if(strlen(__this->str)>0){
if(__this->str[strlen(__this->str)-1] == '.'){
__this->point = 0;
}
__this->str[strlen(__this->str)-1]='\0';
__this->num_2 = __atof(__this->str);
}else{
printf("%s del null\n",__func__);
return 0;
}
if(strlen(__this->str)==0){
__this->str[0] = '0';
/*CalculatorReset();
count=0;
last_mode = 0;*/
}
return strlen(__this->str);
}
//+ - * / =
double TjdUiAppCalculatorModel::CalculationResult(uint8_t mode)
{
double cal_res=0.0;
switch(mode){
case ADD: cal_res=__calculator_add(__this->num_1,__this->num_2); break;
case SUB: cal_res=__calculator_sub(__this->num_1,__this->num_2); break;
case MUL: cal_res=__calculator_mul(__this->num_1,__this->num_2); break;
case DIV: cal_res=__calculator_div(__this->num_1,__this->num_2); break;
default: break;
}
__this->point = 0;
return cal_res;
}
int TjdUiAppCalculatorModel::CalculationModeSet(uint8_t mode)
{
if(__this->error == 1){
printf("error");
return 0;
}
if((__this->cal_mode==mode && __this->cal_mode!=EQUAL) || __this->cal_mode!=NONE){
__this->cal_mode = mode;
last_mode = mode;
return 0;
}
if(__this->cal_mode==NONE){
if(last_mode == 0){
if(__this->num_1 == 0){
__this->num_1 = __this->num_2;
}
}else{
__this->num_1 = CalculationResult(last_mode);
}
__this->cal_mode = mode;
last_mode = mode;
}
memset(__this->str, 0, SHOW_NUM_MAX);
return __this->cal_mode;
}
int TjdUiAppCalculatorModel::DoubleToString(char* str,double num)
{
int len = 0;
int count = 0;
sprintf(str, "%f", num);
len = strlen(str);
if(len>SHOW_NUM_MAX){
str[SHOW_NUM_MAX + 1] = '\0';
if(str[SHOW_NUM_MAX] == '.') str[SHOW_NUM_MAX]='\0';
len = SHOW_NUM_MAX;
}
if(NULL == strchr(str, '.')){
return 0;
}
for(int i=len-1;i>0;i--){
if(str[i]=='.'){
str[i] = '\0';
return 0;
}
if(str[i]=='0'){
str[i] = '\0';
continue;
}else{
return 0;
}
}
return 0;
}
TjdUiAppCalculatorModel::TjdUiAppCalculatorModel(){g_pv_CalculatorModel = this;};
TjdUiAppCalculatorModel::~TjdUiAppCalculatorModel() {g_pv_CalculatorModel = nullptr;};
TjdUiAppCalculatorModel* TjdUiAppCalculatorModel::GetInstance(void) {return g_pv_CalculatorModel;};
} // namespace TJD