当前位置: 首页 > news >正文

#QT(一种朴素的计算器实现方法)

1.IDE:QTCreator


2.实验:这是全靠自己想法写的计算器,没有参考任何教程。

(1)这个计算器只要有运算符敲入就会进行一次运算,所以他没有先后之后,无法满足运算优先级。

(2)小数点第六位有小概率出现不准的情况。

(3)实时计算的值存放在全局变量total中。

(4)是将字符串转为数字

(5)用一个temp_text来记录数字,每次运算符按下会将其转换为数字然后计算完毕之后将其清空。而最上面的text则只是一个界面,用于观察自己输入的运算式子。

(6)第三条text专门用于显示结果,只有=按下时才会显示结果。


3.记录

13c2e63d1fd64481bdfa20cc2d7e8dbc.pngaf9c5cf1117b408fa9a3552623724415.png


4.代码

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_one_pb_clicked();void on_add_pb_clicked();void on_calculator_pb_clicked();void on_multiply_pb_clicked();void on_except_pb_clicked();void on_subtract_pb_clicked();void on_two_pb_clicked();void on_three_pb_clicked();void on_four_pb_clicked();void on_five_pb_clicked();void on_six_pb_clicked();void on_seven_pb_clicked();void on_eight_pb_clicked();void on_nine_pb_clicked();void on_zero_pb_clicked();void on_dot_pb_clicked();void on_clear_pb_clicked();void on_delete_pb_clicked();void on_resi_pb_clicked();private:Ui::Widget *ui;QChar last_ysf;     //记录上一次运算符是什么uint8_t index1;     // *uint8_t index2;     // /uint8_t index3;     // %uint8_t index4;     // +uint8_t index5;     // -float number_temp;  //临时记录运算数字float total;        //结果
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMessageBox>
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);}Widget::~Widget()
{delete ui;
}void Widget::on_calculator_pb_clicked()     //calculate
{number_temp=ui->temp->text().toFloat();if(last_ysf=='*')total = total*number_temp;else if(last_ysf=='/')total = total/number_temp;else if(last_ysf=='%'){int resi_temp;int total_temp;resi_temp=ui->temp->text().toInt();  //求余必须为整数total_temp=total;total = total_temp % resi_temp;}else if(last_ysf=='+')total = total+number_temp;else if(last_ysf=='-')total = total-number_temp;last_ysf='=';                    //记录本次的运算符号ui->temp->clear();QString result_string = QString::asprintf("%.6f",total);ui->lineEdit->insert("=");               //插入一个*显示符ui->lineEdit->insert(result_string);ui->result->setText(result_string);
}void Widget::on_multiply_pb_clicked()   // *
{number_temp=ui->temp->text().toFloat();ui->lineEdit->insert("*");               //插入一个*显示符if(last_ysf=='*')total = total*number_temp;else if(last_ysf=='/')total = total/number_temp;else if(last_ysf=='%'){int resi_temp;int total_temp;resi_temp=ui->temp->text().toInt();  //求余必须为整数total_temp=total;total = total_temp % resi_temp;}else if(last_ysf=='+')total = total+number_temp;else if(last_ysf=='-')total = total-number_temp;elsetotal = number_temp;last_ysf='*';                    //记录本次的运算符号ui->temp->clear();
}void Widget::on_except_pb_clicked()   //  /
{number_temp=ui->temp->text().toFloat();ui->lineEdit->insert("/");               //插入一个*显示符if(last_ysf=='*')total = total*number_temp;else if(last_ysf=='/')total = total/number_temp;else if(last_ysf=='%'){int resi_temp;int total_temp;resi_temp=ui->temp->text().toInt();  //求余必须为整数total_temp=total;total = total_temp % resi_temp;}else if(last_ysf=='+')total = total+number_temp;else if(last_ysf=='-')total = total-number_temp;elsetotal = number_temp;last_ysf='/';                    //记录本次的运算符号ui->temp->clear();
}void Widget::on_add_pb_clicked()    // +
{number_temp=ui->temp->text().toFloat();ui->lineEdit->insert("+");               //插入一个*显示符if(last_ysf=='*')total = total*number_temp;else if(last_ysf=='/')total = total/number_temp;else if(last_ysf=='%'){int resi_temp;int total_temp;resi_temp=ui->temp->text().toInt();  //求余必须为整数total_temp=total;total = total_temp % resi_temp;}else if(last_ysf=='+')total = total+number_temp;else if(last_ysf=='-')total = total-number_temp;elsetotal = number_temp;last_ysf='+';                    //记录本次的运算符号ui->temp->clear();}void Widget::on_subtract_pb_clicked()   // -
{number_temp=ui->temp->text().toFloat();ui->lineEdit->insert("-");               //插入一个*显示符if(last_ysf=='*')total = total*number_temp;else if(last_ysf=='/')total = total/number_temp;else if(last_ysf=='%'){int resi_temp;int total_temp;resi_temp=ui->temp->text().toInt();  //求余必须为整数total_temp=total;total = total_temp % resi_temp;}else if(last_ysf=='+')total = total+number_temp;else if(last_ysf=='-')total = total-number_temp;elsetotal = number_temp;last_ysf='-';                    //记录本次的运算符号ui->temp->clear();
}void Widget::on_resi_pb_clicked()  // %
{number_temp=ui->temp->text().toFloat();ui->lineEdit->insert("%");               //插入一个*显示符if(last_ysf=='*')total = total*number_temp;else if(last_ysf=='/')total = total/number_temp;else if(last_ysf=='%'){int resi_temp;int total_temp;resi_temp=ui->temp->text().toInt();  //求余必须为整数total_temp=total;total = total_temp % resi_temp;}else if(last_ysf=='+')total = total+number_temp;else if(last_ysf=='-')total = total-number_temp;elsetotal = number_temp;last_ysf='%';                    //记录本次的运算符号ui->temp->clear();
}void Widget::on_one_pb_clicked()    //1
{ui->lineEdit->insert("1");ui->temp->insert("1");
}void Widget::on_two_pb_clicked()  // 2
{ui->lineEdit->insert("2");ui->temp->insert("2");
}void Widget::on_three_pb_clicked() //3
{ui->lineEdit->insert("3");ui->temp->insert("3");
}void Widget::on_four_pb_clicked() //4
{ui->lineEdit->insert("4");ui->temp->insert("4");
}void Widget::on_five_pb_clicked()  //5
{ui->lineEdit->insert("5");ui->temp->insert("5");
}void Widget::on_six_pb_clicked()  //6
{ui->lineEdit->insert("6");ui->temp->insert("6");
}void Widget::on_seven_pb_clicked()  //7
{ui->lineEdit->insert("7");ui->temp->insert("7");
}void Widget::on_eight_pb_clicked()  //8
{ui->lineEdit->insert("8");ui->temp->insert("8");
}void Widget::on_nine_pb_clicked()  //9
{ui->lineEdit->insert("9");ui->temp->insert("9");
}void Widget::on_zero_pb_clicked()  //0
{ui->lineEdit->insert("0");ui->temp->insert("0");
}void Widget::on_dot_pb_clicked()  // .
{ui->lineEdit->insert(".");ui->temp->insert(".");
}void Widget::on_clear_pb_clicked() //清除
{ui->lineEdit->clear();ui->temp->clear();ui->result->clear();total=0;
}void Widget::on_delete_pb_clicked() //退格
{uint8_t index;QString str;index=ui->lineEdit->text().length();str=ui->lineEdit->text();str.remove(index-1,1);        //去除末尾一个字符ui->lineEdit->setText(str);uint8_t index2;QString str2;index2=ui->temp->text().length();str2=ui->temp->text();str2.remove(index2-1,1);        //去除末尾一个字符ui->temp->setText(str2);
}

 

 

 

相关文章:

  • 腾讯云服务器CVM_云主机_云计算服务器_弹性云服务器
  • 网络基础 - 预备知识(协议、网络协议、网络传输流程、地址管理)
  • 基础---nginx 启动不了,跟 Apache2 服务冲突
  • <Senior High School Math>: inequality question
  • 【C/C++ 学习笔记】指针
  • 【OceanBase诊断调优】 —— 敏捷诊断工具obdiag一键收集诊断信息实践
  • ChatGPT的核心技术
  • GoLang:云原生时代致力于构建高性能服务器的后端语言
  • 实现两栏布局
  • 数据仓库为什么要分层建设?每一层的作用是什么?
  • 微信小程序之tabBar
  • 利用Java实现数据矩阵的可视化
  • 分布式id生成方案
  • 大模型笔记:吴恩达 ChatGPT Prompt Engineering for Developers(1) prompt的基本原则和策略
  • UE4开个头-简易小汽车
  • Date型的使用
  • docker容器内的网络抓包
  • Docker下部署自己的LNMP工作环境
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • ubuntu 下nginx安装 并支持https协议
  • v-if和v-for连用出现的问题
  • webpack入门学习手记(二)
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 免费小说阅读小程序
  • 前端
  • 如何编写一个可升级的智能合约
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 使用API自动生成工具优化前端工作流
  • 微信小程序:实现悬浮返回和分享按钮
  • 异常机制详解
  • 在Docker Swarm上部署Apache Storm:第1部分
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • 中国人寿如何基于容器搭建金融PaaS云平台
  • #LLM入门|Prompt#3.3_存储_Memory
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (27)4.8 习题课
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (pytorch进阶之路)CLIP模型 实现图像多模态检索任务
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (利用IDEA+Maven)定制属于自己的jar包
  • (一)使用Mybatis实现在student数据库中插入一个学生信息
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 药厂业务系统 CPU爆高分析
  • .net网站发布-允许更新此预编译站点
  • .Net中的集合
  • [ C++ ] STL---string类的使用指南
  • [20150321]索引空块的问题.txt