zoukankan      html  css  js  c++  java
  • 实现简单的计算器(控制器代码)

    设计原理:

    1、在运算操作符前的数据,作为第一操作数,存在用于计算的对象——new execute()的num1属性

    2、点击操作符时,将操作符存在用于计算的对象——new execute()的flag属性

    3、点击等号的时候,将等号前的数据作为第二操作数,存在计算的对象——new execute()的num2属性

    同时,连续点击数字键时,利用一个字符串连接连续点击读到的数字键上的数字,lable控件显式。

    细节:

    每个按钮都需要响应点击事件,需要建立信号与槽函数。

     1 Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
     2 {
     3     ui->setupUi(this);
     4 
     5     this->res = "";
     6     this->exe = new execute();
     7 
     8     connect(this->ui->btn_0,SIGNAL(clicked()),this,SLOT(my_btn_0()));
     9     connect(this->ui->btn_1,SIGNAL(clicked()),this,SLOT(my_btn_1()));
    10     connect(this->ui->btn_2,SIGNAL(clicked()),this,SLOT(my_btn_2()));
    11     connect(this->ui->btn_3,SIGNAL(clicked()),this,SLOT(my_btn_3()));
    12     connect(this->ui->btn_4,SIGNAL(clicked()),this,SLOT(my_btn_4()));
    13     connect(this->ui->btn_5,SIGNAL(clicked()),this,SLOT(my_btn_5()));
    14     connect(this->ui->btn_6,SIGNAL(clicked()),this,SLOT(my_btn_6()));
    15     connect(this->ui->btn_7,SIGNAL(clicked()),this,SLOT(my_btn_7()));
    16     connect(this->ui->btn_8,SIGNAL(clicked()),this,SLOT(my_btn_8()));
    17     connect(this->ui->btn_9,SIGNAL(clicked()),this,SLOT(my_btn_9()));
    18     connect(this->ui->btn_c,SIGNAL(clicked()),this,SLOT(my_btn_c()));
    19     connect(this->ui->btn_eq,SIGNAL(clicked()),this,SLOT(my_btn_eq()));
    20     connect(this->ui->btn_mult,SIGNAL(clicked()),this,SLOT(my_btn_mult()));
    21     connect(this->ui->btn_dec,SIGNAL(clicked()),this,SLOT(my_btn_dec()));
    22     connect(this->ui->btn_div,SIGNAL(clicked()),this,SLOT(my_btn_div()));
    23     connect(this->ui->btn_add,SIGNAL(clicked()),this,SLOT(my_btn_add()));
    24 }

    数字按钮的槽函数

    功能:读取按钮数字,拼接成字符串,显式在label控件

     1 void Dialog::my_btn_0(){
     2     //获取到按钮上的值,存储在text属性中
     3     res += this->ui->btn_0->text();
     4     //显式到控件中
     5     if(res != "")  //避免显式:000000的情况
     6         this->ui->lab_display->setText(res);
     7 }
     8 void Dialog::my_btn_1(){
     9     res += this->ui->btn_1->text();
    10     this->ui->lab_display->setText(res);
    11 }
    12 void Dialog::my_btn_2(){
    13     res += this->ui->btn_2->text();
    14     this->ui->lab_display->setText(res);
    15 }
    16 void Dialog::my_btn_3(){
    17     res += this->ui->btn_3->text();
    18     this->ui->lab_display->setText(res);
    19 }
    20 void Dialog::my_btn_4(){
    21     res += this->ui->btn_4->text();
    22     this->ui->lab_display->setText(res);
    23 }
    24 void Dialog::my_btn_5(){
    25     res += this->ui->btn_5->text();
    26     this->ui->lab_display->setText(res);
    27 }
    28 void Dialog::my_btn_6(){
    29     res += this->ui->btn_6->text();
    30     this->ui->lab_display->setText(res);
    31 }
    32 void Dialog::my_btn_7(){
    33     res += this->ui->btn_7->text();
    34     this->ui->lab_display->setText(res);
    35 }
    36 void Dialog::my_btn_8(){
    37     res += this->ui->btn_8->text();
    38     this->ui->lab_display->setText(res);
    39 }
    40 void Dialog::my_btn_9(){
    41     res += this->ui->btn_9->text();
    42     this->ui->lab_display->setText(res);
    43 }

    运算操作符:

    功能:记录之前所有的数字拼接成的字符串,转为int类型的数据,存储。记录操作符

     1 void Dialog::my_btn_mult(){
     2     int num1 = this->res.toInt();
     3     this->exe->setnum1(num1);
     4     this->res = "";  //清除缓存器
     5     this->ui->lab_display->setText("0");  //实现更新
     6     this->exe->setflag(this->ui->btn_mult->text());  //记录操作符
     7 }
     8 void Dialog::my_btn_dec(){
     9     int num1 = this->res.toInt();
    10     this->exe->setnum1(num1);
    11     this->res = "";
    12     this->ui->lab_display->setText("0");
    13     this->exe->setflag(this->ui->btn_dec->text());
    14 }
    15 void Dialog::my_btn_add(){
    16     //获取操作符之前的数据,作为第一个数据
    17     int num1 = this->res.toInt();
    18     this->exe->setnum1(num1);
    19     this->res = "";
    20     this->ui->lab_display->setText("0");
    21     this->exe->setflag(this->ui->btn_add->text());
    22 }
    23 void Dialog::my_btn_div(){
    24     int num1 = this->res.toInt();
    25     this->exe->setnum1(num1);
    26     this->res = "";
    27     this->ui->lab_display->setText("0");
    28     this->exe->setflag(this->ui->btn_div->text());
    29 }

    等号操作符槽函数:

    功能:获取第二操作数,存储。然后开始计算

    1 void Dialog::my_btn_eq(){
    2     //获取等号操作符之前的数据,作为数据2
    3     int num2 = this->res.toInt();
    4     this->exe->setnum2(num2);
    5     this->res = "";
    6     QString rst = this->exe->doexe();  //计算
    7     this->ui->lab_display->setText(rst); //显式计算值
    8 }

    -----------------------------------------------------------------------------------------------------------------------------------------------------

    设计模式采用MVC模式,怎么关联M与V的呢?

     1 #ifndef DIALOG_H
     2 #define DIALOG_H
     3 #include "execute.h"
     4 #include <QDialog>
     5 #include <QString>
     6 
     7 namespace Ui {
     8 class Dialog;
     9 }
    10 
    11 class Dialog : public QDialog
    12 {
    13     Q_OBJECT
    14 
    15 public:
    16     explicit Dialog(QWidget *parent = nullptr);
    17     ~Dialog();
    18 
    19 private slots:
    20     void my_btn_0();
    21     void my_btn_1();
    22     void my_btn_2();
    23     void my_btn_3();
    24     void my_btn_4();
    25     void my_btn_5();
    26     void my_btn_6();
    27     void my_btn_7();
    28     void my_btn_8();
    29     void my_btn_9();
    30     void my_btn_c();
    31     void my_btn_eq();
    32     void my_btn_add();
    33     void my_btn_dec();
    34     void my_btn_mult();
    35     void my_btn_div();
    36 private:
    37     QString res; //用来存放运算操作数
    38     Ui::Dialog *ui;  //UI
    39     execute* exe;    //计算功能,在这里让MCV的 M和V关联起来
    40 };
    41 
    42 #endif // DIALOG_H

    这样在Dialog窗体(对象中),既可以从ui中获取数据,然后用于计算execute,然后再传入UI中进行显式,所以在Dialog对象中,可以访问execute对象,还可以访问Ui::Dialog对象,即在这里定义为其数据成员。

      1 #include "dialog.h"
      2 #include "ui_dialog.h"
      3 
      4 Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
      5 {
      6     ui->setupUi(this);
      7 
      8     this->res = "";
      9     this->exe = new execute();
     10 
     11     connect(this->ui->btn_0,SIGNAL(clicked()),this,SLOT(my_btn_0()));
     12     connect(this->ui->btn_1,SIGNAL(clicked()),this,SLOT(my_btn_1()));
     13     connect(this->ui->btn_2,SIGNAL(clicked()),this,SLOT(my_btn_2()));
     14     connect(this->ui->btn_3,SIGNAL(clicked()),this,SLOT(my_btn_3()));
     15     connect(this->ui->btn_4,SIGNAL(clicked()),this,SLOT(my_btn_4()));
     16     connect(this->ui->btn_5,SIGNAL(clicked()),this,SLOT(my_btn_5()));
     17     connect(this->ui->btn_6,SIGNAL(clicked()),this,SLOT(my_btn_6()));
     18     connect(this->ui->btn_7,SIGNAL(clicked()),this,SLOT(my_btn_7()));
     19     connect(this->ui->btn_8,SIGNAL(clicked()),this,SLOT(my_btn_8()));
     20     connect(this->ui->btn_9,SIGNAL(clicked()),this,SLOT(my_btn_9()));
     21     connect(this->ui->btn_c,SIGNAL(clicked()),this,SLOT(my_btn_c()));
     22     connect(this->ui->btn_eq,SIGNAL(clicked()),this,SLOT(my_btn_eq()));
     23     connect(this->ui->btn_mult,SIGNAL(clicked()),this,SLOT(my_btn_mult()));
     24     connect(this->ui->btn_dec,SIGNAL(clicked()),this,SLOT(my_btn_dec()));
     25     connect(this->ui->btn_div,SIGNAL(clicked()),this,SLOT(my_btn_div()));
     26     connect(this->ui->btn_add,SIGNAL(clicked()),this,SLOT(my_btn_add()));
     27 }
     28 
     29 Dialog::~Dialog()
     30 {
     31     delete ui;
     32     delete exe;
     33 }
     34 
     35 void Dialog::my_btn_0(){
     36     //获取到按钮上的值,存储在text属性中
     37     res += this->ui->btn_0->text();
     38     //显式到控件中
     39     if(res != "")  //避免显式:000000的情况
     40         this->ui->lab_display->setText(res);
     41 }
     42 void Dialog::my_btn_1(){
     43     res += this->ui->btn_1->text();
     44     this->ui->lab_display->setText(res);
     45 }
     46 void Dialog::my_btn_2(){
     47     res += this->ui->btn_2->text();
     48     this->ui->lab_display->setText(res);
     49 }
     50 void Dialog::my_btn_3(){
     51     res += this->ui->btn_3->text();
     52     this->ui->lab_display->setText(res);
     53 }
     54 void Dialog::my_btn_4(){
     55     res += this->ui->btn_4->text();
     56     this->ui->lab_display->setText(res);
     57 }
     58 void Dialog::my_btn_5(){
     59     res += this->ui->btn_5->text();
     60     this->ui->lab_display->setText(res);
     61 }
     62 void Dialog::my_btn_6(){
     63     res += this->ui->btn_6->text();
     64     this->ui->lab_display->setText(res);
     65 }
     66 void Dialog::my_btn_7(){
     67     res += this->ui->btn_7->text();
     68     this->ui->lab_display->setText(res);
     69 }
     70 void Dialog::my_btn_8(){
     71     res += this->ui->btn_8->text();
     72     this->ui->lab_display->setText(res);
     73 }
     74 void Dialog::my_btn_9(){
     75     res += this->ui->btn_9->text();
     76     this->ui->lab_display->setText(res);
     77 }
     78 void Dialog::my_btn_c(){
     79     res ="";
     80     this->ui->lab_display->setText("0");
     81 }
     82 void Dialog::my_btn_eq(){
     83     //获取等号操作符之前的数据,作为数据2
     84     int num2 = this->res.toInt();
     85     this->exe->setnum2(num2);
     86     this->res = "";
     87     QString rst = this->exe->doexe();
     88     this->ui->lab_display->setText(rst);
     89 }
     90 void Dialog::my_btn_mult(){
     91     int num1 = this->res.toInt();
     92     this->exe->setnum1(num1);
     93     this->res = "";
     94     this->ui->lab_display->setText("0");
     95     this->exe->setflag(this->ui->btn_mult->text());
     96 }
     97 void Dialog::my_btn_dec(){
     98     int num1 = this->res.toInt();
     99     this->exe->setnum1(num1);
    100     this->res = "";
    101     this->ui->lab_display->setText("0");
    102     this->exe->setflag(this->ui->btn_dec->text());
    103 }
    104 void Dialog::my_btn_add(){
    105     //获取操作符之前的数据,作为第一个数据
    106     int num1 = this->res.toInt();
    107     this->exe->setnum1(num1);
    108     this->res = "";
    109     this->ui->lab_display->setText("0");
    110     this->exe->setflag(this->ui->btn_add->text());
    111 }
    112 void Dialog::my_btn_div(){
    113     int num1 = this->res.toInt();
    114     this->exe->setnum1(num1);
    115     this->res = "";
    116     this->ui->lab_display->setText("0");
    117     this->exe->setflag(this->ui->btn_div->text());
    118 }
    内在的趣味,表面的繁琐
  • 相关阅读:
    locate命令详解
    python 爬虫获取文件式网站资源完整版(基于python 3.6)
    python 爬虫获取文件式网站资源(基于python 3.6)
    利用python 获取网址中的href(基于python 3.6)
    springmvc+font-awesome开发出的页面显示方框乱码的解决方法
    2017年6月短学期培训代码总结 -----springMvc
    2017年6月短学期培训代码总结 -----JDBC
    构建之法 第十章 典型用户和场景
    构建之法 第九章 项目经理
    构建之法 第八章 需求分析
  • 原文地址:https://www.cnblogs.com/data1213/p/10801008.html
Copyright © 2011-2022 走看看