#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_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    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;
    else
        total = 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;
    else
        total = 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;
    else
        total = 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;
    else
        total = 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;
    else
        total = 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);
}




 

 

 

相关推荐

  1. QT实现log存储方法

    2024-03-15 05:26:06       36 阅读
  2. QT中程序执行时间精准计算方法及对比

    2024-03-15 05:26:06       45 阅读
  3. Python技术栈 —— 超时LRU实现方式

    2024-03-15 05:26:06       65 阅读
  4. springboot请求参数校验实现方案

    2024-03-15 05:26:06       34 阅读
  5. 树形结构便捷实现方案

    2024-03-15 05:26:06       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-15 05:26:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-15 05:26:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-15 05:26:06       87 阅读
  4. Python语言-面向对象

    2024-03-15 05:26:06       96 阅读

热门阅读

  1. 计算机网络之网络层概念整理(下)

    2024-03-15 05:26:06       36 阅读
  2. 大语言模型三个应用方向

    2024-03-15 05:26:06       49 阅读
  3. 如何使用IDE端通义灵码

    2024-03-15 05:26:06       42 阅读
  4. QT--对象模型(对象树)

    2024-03-15 05:26:06       43 阅读
  5. 【运维】-gitlab docker-compose部署,并进行备份

    2024-03-15 05:26:06       40 阅读
  6. 分布式id生成方案

    2024-03-15 05:26:06       44 阅读
  7. 高德地图 鼠标移入infowindow时取消地图滚轮缩放

    2024-03-15 05:26:06       38 阅读
  8. LUA语法复习总结

    2024-03-15 05:26:06       34 阅读
  9. 用代码生成流程图Code Chart

    2024-03-15 05:26:06       41 阅读