Achieve Qt-- calculator

A computer analysis program interface
Achieve Qt-- calculator
from the results we can see that in FIG
1.QLineEdit for accepting user input
2.QLineEdit can acquire user input string
3.QLineEdit functional components, it requires the parent container assembly as
4.QLineEdit capable parent positioning component
of the coordinates and the position calculator shown in FIG
Achieve Qt-- calculator
code interface implemented

#include "Widget.h"
#include <QApplication>
#include <QPushButton>
#include <QLineEdit>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget* w = new QWidget(NULL, Qt::WindowCloseButtonHint);
    w->setWindowTitle("Calculator");
    QLineEdit* le = new QLineEdit(w);
    QPushButton* button[20] = {0};
    const char* btnText[20] =
    {//数字键与功能键
         "7", "8", "9", "+", "(",
         "4", "5", "6", "-", ")",
         "1", "2", "3", "*", "<-",
         "0", ".", "=", "/", "C",
    };
    int ret = 0;
    //设置的大小以及坐标
    le->move(10, 10);
    le->resize(240, 30);
    le->setReadOnly(true);
    //数字键与功能键的位置的实现
    for(int i=0; i<4; i++)
    {
         for(int j=0; j<5; j++)
         {
             button[i*5 + j] = new QPushButton(w);
             button[i*5 + j]->resize(40, 40);
             button[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
             button[i*5 + j]->setText(btnText[i*5 + j]);
         }
    }

    w->show();
    w->setFixedSize(w->width(), w->height());

    ret = a.exec();

    delete w;

    return ret;
}

The results shown in FIG run
Achieve Qt-- calculator
above computer-generated temporary demo of UI, there will be many problems and to achieve the following algorithm will be described according to
reconstruct the code calculator interface A.
1. Reconstruction - to improve code quality for the purpose of rewriting the code to make software design more rational design and architecture, improve software maintainability and scalability
to achieve the reconstruction of the code of different
code to achieve - to achieve in accordance with the design program, the focus is on implementing the functions
of code weight structure - in order to improve code quality software architecture optimized for the purpose of
distinction: architecture code implementation is not considered good or bad, just consider implementing functions can not be realized when the impact of the code refactoring, consider only improve the architecture of
the calculator interface code reconstruction
Achieve Qt-- calculator
to achieve reconstruction

main.cpp
#include "CalculatorUI.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QCalculatorUI* cal = QCalculatorUI::NewInstance();
    int ret = -1;
    if( cal != NULL )
    {
        cal->show();
        ret = a.exec();
        delete cal;
    }
    return ret;
}

CalculatorUI.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>

class QCalculatorUI : public QWidget
{
private:
    QLineEdit* m_edit;
    QPushButton* m_buttons[20];

    QCalculatorUI();
    bool construct();
public:
    static QCalculatorUI* NewInstance();
    void show();
    ~QCalculatorUI();
};

#endif // WIDGET_H

CalculatorUI.cpp
#include "CalculatorUI.h"

QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{

}

bool QCalculatorUI::construct()
{
    bool ret = true;
    const char* btnText[20] =
    {
        "7", "8", "9", "+", "(",
        "4", "5", "6", "-", ")",
        "1", "2", "3", "*", "<-",
        "0", ".", "=", "/", "C",
    };

    m_edit = new QLineEdit(this);

    if( m_edit != NULL )
    {
        m_edit->move(10, 10);
        m_edit->resize(240, 30);
        m_edit->setReadOnly(true);
    }
    else
    {
        ret = false;
    }

    for(int i=0; (i<4) && ret; i++)
    {
        for(int j=0; (j<5) && ret; j++)
        {
            m_buttons[i*5 + j] = new QPushButton(this);

            if( m_buttons[i*5 + j] != NULL )
            {
                m_buttons[i*5 + j]->resize(40, 40);
                m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
            }
            else
            {
                ret = false;
            }
        }
    }

    return ret;
}

QCalculatorUI* QCalculatorUI::NewInstance()
{
    QCalculatorUI* ret = new QCalculatorUI();

    if( (ret == NULL) || !ret->construct() )
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

void QCalculatorUI::show()
{
    QWidget::show();

    setFixedSize(width(), height());
}

QCalculatorUI::~QCalculatorUI()
{

}

代码实现的结果
Achieve Qt-- calculator
B.在这里要对Qt的消息处理做介绍,对之后计算器的操作会有帮助
1.Qt消息模型

Qt封装了具体操作系统的消息机制
Qt遵循经典的GUI消息驱动事件模型
Achieve Qt-- calculator
C.Qt定义了与系统消息相关的概念
a.信号--由操作系统产生的消息
b.--程序中的消息处理函数
c.连接--将系统消息绑定到消息处理函数
Qt的消息处理机制
Achieve Qt-- calculator
Achieve Qt-- calculator
上图从Qt帮助文档中对connect进行查找,connect函数包含了发送对象、消息名、接收对象、接收对象的的成员函数
D.自定义槽
1.只有QObject的子类才能自定义槽
2.定义槽的类必须在声明的最开始处使用Q_OBJECT
3.类中声明槽时需要使用slots关键字
4.槽与所处理的信号在函数签名上必须一致
5.SIGNAL和SLOT所指定的名称中可以包含参数类型,不能包含具体参数名

//在上面的Calculator.h上添加槽函数
private slots:
    void onButtonClicked();
//同时在cpp上将其实现,通过connect函数将其相连
void QCalculatorUI::onButtonClicked()
{
    QPushButton* btn = (QPushButton*)sender();

    qDebug() << "onButtonClicked()";
    qDebug() << btn->text();
}

connect(m_buttons[i*5 + j], SIGNAL(clicked(bool)), this, SLOT(onButtonClicked()));

按钮按下实现的打印结果
Achieve Qt-- calculator
E.计算器算法的实现
1.前缀表达式、中缀表达式、后缀表达式的区别
a.前缀表达式--前缀表达式是一种没有括号的算术表达式,与中缀表达式不同的是,其将运算符写在前面,操作数写在后面。为纪念其发明者波兰数学家Jan Lukasiewicz,前缀表达式也称为“波兰式”。例如,- 1 + 2 3,它等价于1-(2+3)。
b.中缀表达式--是一个通用的算术或逻辑公式表示方法, 操作符是以中缀形式处于操作数的中间(例:3 + 4),中缀表达式是人们常用的算术表示方法。与前缀表达式(例:+ 3 4)或后缀表达式(例:3 4 +)相比,中缀表达式不容易被计算机解析,但仍被许多程序语言使用,因为它符合人们的普遍用法。与前缀或后缀记法不同的是,中缀记法中括号是必需的。计算过程中必须用括号将操作符和对应的操作数括起来,用于指示运算的次序。
c.后缀表达式--后缀表达式,又称逆波兰式,指的是不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则)
计算器的算法实现
1.将中缀表达式进行数字和运算符的分离
2.将中缀表达式转换为后缀表达式
3.通过后缀表达式计算最终结果

所有要计算的中缀表达式中包含
Achieve Qt-- calculator
分离算法的实现--思想:以符号作为标志对表达式中的字符逐个访问
Achieve Qt-- calculator
Achieve Qt-- calculator
中缀转后缀
中缀表达式转后缀表达式的过程类似编译过程
1. The four operation expression parentheses must match
2. The conversion according to operator precedence for
expression after conversion 3. no parentheses
may sequentially calculate the final result of the conversion after 4
to achieve the conversion process: a current element e a digital output thereof; B is the operator of the current element e, to be compared with the priority of operator stack, when it is less than or equal to the output of the top element, a turn, if the stack is greater than the top element e; e c current element for the stack to the left bracket; pop stack elements when d e is the current element and outputs a right bracket, the left bracket until the top of the stack, and the stack is popped from the left parenthesis stack
Achieve Qt-- calculator
legitimate four arithmetic expression wherein - brace matching pairs, a left bracket and right bracket inevitably occurs first in
Achieve Qt-- calculator
the postfix expression is calculated
traversal postfix expression --a operators and digital current digital push elements; B is the operator to start the current element right operand stack pop, pop-up operation was yesterday number from the stack; C calculates according to a symbol; D calculation result onto the stack
Achieve Qt-- calculator

Finally, attach the complete code download link https://down.51cto.com/data/2464399

Guess you like

Origin blog.51cto.com/13475106/2427965