Qt实现简易计算器

  创建基于QWidget的cacul项目,包含.ui文件,其余设置不变
  cacul.pro

#-------------------------------------------------
#
# Project created by QtCreator 2019-04-24T19:16:05
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = cacul
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

  widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>382</width>
    <height>283</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <widget class="QLineEdit" name="lineEdit">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>20</y>
     <width>351</width>
     <height>31</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_0">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>230</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>0</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_dot">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>230</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>.</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_pn">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>230</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>+/-</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_add">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>230</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>+</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_sub">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>190</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>-</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_2">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>190</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>2</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_1">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>190</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>1</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_3">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>190</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>3</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_9">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>110</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>9</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_5">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>150</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>5</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_7">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>110</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>7</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_mul">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>150</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>*</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_dev">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>110</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>/</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_8">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>110</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>8</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_4">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>150</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>4</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_num_6">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>150</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>6</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_clear">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>70</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>c</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_back">
   <property name="geometry">
    <rect>
     <x>200</x>
     <y>70</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>&lt;-</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_fun_equal">
   <property name="geometry">
    <rect>
     <x>290</x>
     <y>70</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>=</string>
   </property>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

  widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->num.clear();
    this->numList.clear();
    this->lineStr.clear();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btn_num_0_clicked(bool checked)
{
    this->num.append('0');
    this->lineStr.append('0');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_1_clicked(bool checked)
{
    this->num.append('1');
    this->lineStr.append('1');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_2_clicked(bool checked)
{
    this->num.append('2');
    this->lineStr.append('2');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_3_clicked(bool checked)
{
    this->num.append('3');
    this->lineStr.append('3');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_4_clicked(bool checked)
{
    this->num.append('4');
    this->lineStr.append('4');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_5_clicked(bool checked)
{
    this->num.append('5');
    this->lineStr.append('5');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_6_clicked(bool checked)
{
    this->num.append('6');
    this->lineStr.append('6');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_7_clicked(bool checked)
{
    this->num.append('7');
    this->lineStr.append('7');
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_num_8_clicked(bool checked)
{
    this->num.append('8');
    this->lineStr.append('8');
    ui->lineEdit->setText(this->lineStr);
}
void Widget::on_btn_num_9_clicked(bool checked)
{
    this->num.append('9');
    this->lineStr.append('9');
    ui->lineEdit->setText(this->lineStr);
}
void Widget::on_btn_num_dot_clicked(bool checked)
{
    this->num.append('.');
    this->lineStr.append('.');
    ui->lineEdit->setText(this->lineStr);
    ui->lineEdit->setText(this->num);
}

void Widget::on_btn_fun_pn_clicked(bool checked)
{
    this->lineStr.remove(this->num);
    if(this->num.at(0) == '-')
        this->num.remove(0,1);
    else
        this->num.insert(0,'-');
    this->lineStr.append(this->num);
    ui->lineEdit->setText(this->num);
}

void Widget::on_btn_fun_clear_clicked(bool checked)
{
    this->num.clear();
    this->numList.clear();
    this->lineStr.clear();
    ui->lineEdit->clear();
}

void Widget::on_btn_fun_back_clicked(bool checked)
{
    this->num.chop(1);
    this->lineStr.chop(1);
    ui->lineEdit->setText(this->lineStr);
}

void Widget::on_btn_fun_add_clicked(bool checked)
{
    this->lineStr.append("+");
    ui->lineEdit->setText(this->lineStr);
    if(this->numList.isEmpty())
    {
        this->numList.append(this->num);
        this->numList.append("add");
        this->num.clear();
        return ;
    }
    if(this->numList.length() == 2)
    {
        float temp;
        QString res;
        QString num1 = this->numList[0];
        if(this->numList.at(1) == "add")
            temp = num1.toFloat() + this->num.toFloat();
        else if(this->numList.at(1) == "sub")
            temp = num1.toFloat() - this->num.toFloat();
        else if(this->numList.at(1) == "mul")
            temp = num1.toFloat() * this->num.toFloat();
        else if(this->numList.at(1) == "dev")
            temp = num1.toFloat() / this->num.toFloat();

        res = QString("%1").arg(temp);
        this->num.clear();
        this->numList.clear();
        this->numList.append(res);
        this->numList.append("add");
        return ;
    }
    if(this->numList.length() == 4)
    {
        float temp;
        QString res;
        QString num1 = this->numList.at(this->numList.length() - 2);
        QString num2;
        if(this->numList.at(3) == "mul")
        {
            temp = num1.toFloat() * this->num.toFloat();
            this->numList.pop_back();
            this->numList.pop_back();
        }
        else if(this->numList.at(3) == "dev")
        {
            temp = num1.toFloat() / this->num.toFloat();
            this->numList.pop_back();
            this->numList.pop_back();
        }
        this->num.clear();
        num2 = this->numList.at(0);
        if(this->numList.at(1) == "add")
            temp = num2.toFloat() + temp;
        else if(this->numList.at(1) == "sub")
            temp = num2.toFloat() - temp;

        res = QString("%1").arg(temp);

        this->numList.pop_back();
        this->numList.pop_back();
        this->numList.append(res);
        this->numList.append("add");
    }

}

void Widget::on_btn_fun_sub_clicked(bool checked)
{
    this->lineStr.append("-");
    ui->lineEdit->setText(this->lineStr);
    if(this->numList.isEmpty())
    {
        this->numList.append(this->num);
        this->numList.append("sub");
        this->num.clear();
        return ;
    }
    if(this->numList.length() == 2)
    {
        float temp;
        QString res;
        QString num1 = this->numList[0];
        if(this->numList.at(1) == "add")
            temp = num1.toFloat() + this->num.toFloat();
        else if(this->numList.at(1) == "sub")
            temp = num1.toFloat() - this->num.toFloat();
        else if(this->numList.at(1) == "mul")
            temp = num1.toFloat() * this->num.toFloat();
        else if(this->numList.at(1) == "dev")
            temp = num1.toFloat() / this->num.toFloat();
        res = QString("%1").arg(temp);
        this->num.clear();
        this->numList.clear();
        this->numList.append(res);
        this->numList.append("sub");
        return ;
    }
    if(this->numList.length() == 4)
    {
        float temp;
        QString res;
        QString num1 = this->numList.at(this->numList.length() - 2);
        QString num2;
        if(this->numList.at(3) == "mul")
        {
            temp = num1.toFloat() * this->num.toFloat();
            res = QString("%1").arg(temp);
            this->numList.pop_back();
            this->numList.pop_back();
        }
        else if(this->numList.at(3) == "dev")
        {
            temp = num1.toFloat() / this->num.toFloat();
            res = QString("%1").arg(temp);
            this->numList.pop_back();
            this->numList.pop_back();
        }
        this->num.clear();
        num2 = this->numList.at(0);
        if(this->numList.at(1) == "add")
            temp = num2.toFloat() + temp;
        else if(this->numList.at(1) == "sub")
            temp = num2.toFloat() - temp;

        res = QString("%1").arg(temp);
        this->numList.pop_back();
        this->numList.pop_back();
        this->numList.append(res);
        this->numList.append("sub");
    }

}

void Widget::on_btn_fun_mul_clicked(bool checked)
{
    this->lineStr.append("*");
    ui->lineEdit->setText(this->lineStr);
    if(this->numList.isEmpty())
    {
        this->numList.append(this->num);
        this->numList.append("mul");
        this->num.clear();
    }
    else
    {
        if(this->numList.length() == 2)
        {
            float temp;
            QString res;
            QString num1 = this->numList[0];
            if(this->numList.at(1) == "add")
            {
                this->numList.append(this->num);
                this->numList.append("mul");
                this->num.clear();
                return ;
            }
            else if(this->numList.at(1) == "sub")
            {
                this->numList.append(this->num);
                this->numList.append("mul");
                this->num.clear();
                return ;
            }
            else if(this->numList.at(1) == "mul")
                temp = num1.toFloat() * this->num.toFloat();
            else if(this->numList.at(1) == "dev")
                temp = num1.toFloat() / this->num.toFloat();
            res = QString("%1").arg(temp);
            this->num.clear();
            this->numList.clear();
            this->numList.append(res);
            this->numList.append("mul");
        }
        if(this->numList.length() == 4)
        {
            float temp;
            QString res;
            QString num1 = this->numList.at(this->numList.length() - 2);
            if(this->numList.at(3) == "mul")
            {
                temp = num1.toFloat() * this->num.toFloat();
                res = QString("%1").arg(temp);
                this->numList.pop_back();
                this->numList.pop_back();
            }
            else if(this->numList.at(3) == "dev")
            {
                temp = num1.toFloat() / this->num.toFloat();
                res = QString("%1").arg(temp);
                this->numList.pop_back();
                this->numList.pop_back();
            }

                this->num.clear();
                this->numList.append(res);
                this->numList.append("mul");
            }

    }

}

void Widget::on_btn_fun_dev_clicked(bool checked)
{
    this->lineStr.append("/");
    ui->lineEdit->setText(this->lineStr);
    if(this->numList.isEmpty())
    {
        this->numList.append(this->num);
        this->numList.append("dev");
        this->num.clear();
    }
    else
    {
        if(this->numList.length() == 2)
        {
            float temp;
            QString res;
            QString num1 = this->numList[0];
            if(this->numList.at(1) == "add")
            {
                this->numList.append(this->num);
                this->numList.append("dev");
                this->num.clear();
                return ;
            }
            else if(this->numList.at(1) == "sub")
            {
                this->numList.append(this->num);
                this->numList.append("dev");
                this->num.clear();
                return ;
            }
            else if(this->numList.at(1) == "mul")
                temp = num1.toFloat() * this->num.toFloat();
            else if(this->numList.at(1) == "dev")
                temp = num1.toFloat() / this->num.toFloat();
            res = QString("%1").arg(temp);
            this->num.clear();
            this->numList.clear();
            this->numList.append(res);
            this->numList.append("dev");
        }
        if(this->numList.length() == 4)
        {
            float temp;
            QString res;
            QString num1 = this->numList.at(this->numList.length() - 2);
            if(this->numList.at(3) == "mul")
            {
                temp = num1.toFloat() * this->num.toFloat();
                res = QString("%1").arg(temp);
                this->numList.pop_back();
                this->numList.pop_back();
            }
            else if(this->numList.at(3) == "dev")
            {
                temp = num1.toFloat() / this->num.toFloat();
                res = QString("%1").arg(temp);
                this->numList.pop_back();
                this->numList.pop_back();
            }
            this->num.clear();
            this->numList.append(res);
            this->numList.append("dev");
        }

    }

}

void Widget::on_btn_fun_equal_clicked(bool checked)
{
    QString res;
    if(this->numList.isEmpty())
    {
        return ;
    }
    else
    {
        if(this->numList.length() == 2)
        {
            float temp;
            QString num1 = this->numList[0];
            if(this->numList.at(1) == "add")
                temp = num1.toFloat() + this->num.toFloat();
            else if(this->numList.at(1) == "sub")
                temp = num1.toFloat() - this->num.toFloat();
            else if(this->numList.at(1) == "mul")
                temp = num1.toFloat() * this->num.toFloat();
            else if(this->numList.at(1) == "dev")
                temp = num1.toFloat() / this->num.toFloat();
            res = QString("%1").arg(temp);
        }
        else if(this->numList.length() == 4)
        {
            float temp;
            QString num1 = this->numList[0];
            QString num2 = this->numList.at(this->numList.length() - 2);
            if(this->numList.at(3) == "mul")
                temp = num2.toFloat() * this->num.toFloat();
            else if(this->numList.at(3) == "dev")
                temp = num2.toFloat() / this->num.toFloat();
            if(this->numList.at(1) == "add")
                temp = num1.toFloat() + temp;
            else if(this->numList.at(1) == "sub")
                temp = num1.toFloat() - temp;
            res = QString("%1").arg(temp);

        }
        this->num.clear();
        this->numList.clear();
        this->lineStr.append("=");
        this->lineStr.append(res);
        ui->lineEdit->setText(this->lineStr);
        this->lineStr.clear();
    }
}

  main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

  演示:
在这里插入图片描述
  提示:本软件的bug:退格键只能删除数字,不能删除运算符号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值