首页 > 编程语言 >【Qt项目制作普通计算器】C++语言

【Qt项目制作普通计算器】C++语言

时间:2024-07-23 21:24:39浏览次数:10  
标签:num1 num2 setText C++ label ui 计算器 msg Qt

目录

一、概述

二、界面设计

三、程序代码

1、程序代码存放位置

2、widget.h文件

3、widget.cpp

4、main.cpp

5、.pro文件

6、资源文件的添加


一、概述

1. 规划与设计

  • 功能确定:决定计算器将支持哪些基本运算(加、减、乘、除、百分之、平方、开根号、变分数等)。
  • 界面设计:设计用户界面,通常包括数字键、运算符键、清除键和显示结果的显示屏。

2. 环境搭建

  • 安装Qt:确保已经安装了Qt开发环境和Qt Creator集成开发环境。
  • 创建项目:在Qt Creator中创建一个新的Qt Widgets Application项目。

3. UI布局

  • 使用Qt Designer:通过Qt Designer设计UI界面,包括按钮和显示屏。
  • 布局管理:使用布局管理器(如QGridLayout)来排列按钮,确保界面在不同尺寸的窗口中也能正确显示。

4. 逻辑实现

  • 定义槽函数:为每个按钮编写槽函数,用于处理点击事件。
  • 信号与槽连接:将按钮的点击信号连接到对应的槽函数。
  • 输入处理:在槽函数中处理用户输入,更新显示屏内容。

二、界面设计

给每个组件设置名称,在我设计的界面中组件用的是PublishButton按钮和QLable组件共同构成,其中将PublishButton都存入Widget组件中,QLable放入Widget中。(注意:组件名称不可重名)

界面效果

三、程序代码

1、程序代码存放位置

2、widget.h文件
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "QString"
#include "QIcon"
#include "math.h"
#include "QPixmap"
#include <QPainter>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    void paintEvent(QPaintEvent *event);
public slots:
    void bnt_num0();
    void bnt_num1();
    void bnt_num2();
    void bnt_num3();
    void bnt_num4();
    void bnt_num5();
    void bnt_num6();
    void bnt_num7();
    void bnt_num8();
    void bnt_num9();

    void bnt_add();
    void bnt_sub();
    void bnt_mul();
    void bnt_div();
    void bnt_d();
    void bnt_over();


private slots:

    void on_clear_clicked(bool checked);

    void on_clearall_clicked(bool checked);

    void on_delete_2_clicked(bool checked);

    void on_fenshu_clicked(bool checked);

    void on_pow_clicked(bool checked);

    void on_genhao_clicked(bool checked);

    void on_aors_clicked(bool checked);

    void on_yu_clicked(bool checked);

private:
    Ui::Widget *ui;
    QString symbol = "";
    QString num1 = "";
    QString num2 = "";
    QString over = "";
    QString msg = "";
    bool isSave01 = true;
    bool isSave02 = true;
};

#endif // WIDGET_H
3、widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "QIcon"
#include "QString"
#include "QDebug"
#include "QLabel"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(430,550);
    this->setWindowTitle("计算器");
    this->setWindowIcon(QIcon(":/image/computer.webp"));
    //使用setStyleSheet方法设置标签颜色
    ui->label_msg->setStyleSheet("QLabel { color: grey; }");



    connect(ui->add,SIGNAL(clicked(bool)),this,SLOT(bnt_add()));
    connect(ui->div,SIGNAL(clicked(bool)),this,SLOT(bnt_div()));
    connect(ui->sub,SIGNAL(clicked(bool)),this,SLOT(bnt_sub()));
    connect(ui->mul,SIGNAL(clicked(bool)),this,SLOT(bnt_mul()));
    connect(ui->over,SIGNAL(clicked(bool)),this,SLOT(bnt_over()));

    connect(ui->btn_d,SIGNAL(clicked(bool)),this,SLOT(bnt_d()));

    connect(ui->btn_0,SIGNAL(clicked(bool)),this,SLOT(bnt_num0()));
    connect(ui->btn_1,SIGNAL(clicked(bool)),this,SLOT(bnt_num1()));
    connect(ui->btn_2,SIGNAL(clicked(bool)),this,SLOT(bnt_num2()));
    connect(ui->btn_3,SIGNAL(clicked(bool)),this,SLOT(bnt_num3()));
    connect(ui->btn_4,SIGNAL(clicked(bool)),this,SLOT(bnt_num4()));
    connect(ui->btn_5,SIGNAL(clicked(bool)),this,SLOT(bnt_num5()));
    connect(ui->btn_6,SIGNAL(clicked(bool)),this,SLOT(bnt_num6()));
    connect(ui->btn_7,SIGNAL(clicked(bool)),this,SLOT(bnt_num7()));
    connect(ui->btn_8,SIGNAL(clicked(bool)),this,SLOT(bnt_num8()));
    connect(ui->btn_9,SIGNAL(clicked(bool)),this,SLOT(bnt_num9()));

}

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

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
//    painter.drawPixmap(0,0,width(),height(),QPixmap(":/image/bizhi.jpg"));
}
//数字0代码段
void Widget::bnt_num0()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("0");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("0");
        ui->label_idet->setText(num2);
    }
}
//数字1代码段
void Widget::bnt_num1()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("1");
        ui->label_idet->setText(num1);

    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("1");
        ui->label_idet->setText(num2);
    }
}
//数字2代码段
void Widget::bnt_num2()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("2");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("2");
        ui->label_idet->setText(num2);
    }
}
//数字3代码段
void Widget::bnt_num3()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("3");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("3");
        ui->label_idet->setText(num2);
    }
}
//数字4代码段
void Widget::bnt_num4()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("4");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("4");
        ui->label_idet->setText(num2);
    }
}
//数字5代码段
void Widget::bnt_num5()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("5");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("5");
        ui->label_idet->setText(num2);
    }
}
//数字6代码段
void Widget::bnt_num6()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("6");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("6");
        ui->label_idet->setText(num2);
    }
}
//数字7代码段
void Widget::bnt_num7()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("7");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("7");
        ui->label_idet->setText(num2);
    }
}
//数字8代码段
void Widget::bnt_num8()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("8");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("8");
        ui->label_idet->setText(num2);
    }
}
//数字9代码段
void Widget::bnt_num9()
{
    if(symbol == "")
    {
        if(num1 == "0")
        {
            num1.chop(1);
        }
        num1.append("9");
        ui->label_idet->setText(num1);
    }
    else
    {
        if(num2 == "0")
        {
            num2.chop(1);
        }
        num2.append("9");
        ui->label_idet->setText(num2);
    }
}
//小数点代码段
void Widget::bnt_d()
{
    if(symbol == "")
    {
        num1.append(".");
        ui->label_idet->setText(num1);
    }
    else
    {
        num2.append(".");
        ui->label_idet->setText(num2);
    }
}

//加法
void Widget::bnt_add()
{
    double n1 = num1.toDouble();
    double n2 = num2.toDouble();
    double x = 0.0;
    if(symbol == "+")
    {
        //计算三位以上的式子
        x = n1 + n2;
        num1 = QString::number(x);
        qDebug()<<"x="<<x;
    }
    else if (symbol =="-")
    {
        x = n1 - n2;
        num1 = QString::number(x);
    }
    else if (symbol =="×")
    {
        x = n1 * n2;
        num1 = QString::number(x);
    }
    else if (symbol =="÷")
    {
        x = n1 / n2;
        num1 = QString::number(x);
    }
    else if(symbol == "%")
    {
        if(num1 == "")
        {
            ui->label_msg->setText("0");
        }
        else if(num2 != "")
        {
            num1 = QString::number(x);
        }
    }

    num2 = "";
    msg = "";
    //将式子放入信息栏中
    if(isSave01)
    {
        msg.append(num1);
    }
    symbol = "+";
    msg.append(symbol);
    ui->label_msg->setText(msg);
    isSave01 = true;

}
//减法
void Widget::bnt_sub()
{
    double n1 = num1.toDouble();
    double n2 = num2.toDouble();
    double x = 0.0;
    if(symbol == "-")
    {
        //计算三位以上的式子
        x = n1 - n2;
        num1 = QString::number(x);
        qDebug()<<"x="<<x;
    }
    else if (symbol =="+")
    {
        x = n1 + n2;
        num1 = QString::number(x);
    }
    else if (symbol =="×")
    {
        x = n1 * n2;
        num1 = QString::number(x);
    }
    else if (symbol =="÷")
    {
        x = n1 / n2;
        num1 = QString::number(x);
    }
    else if(symbol == "%")
    {
        if(num1 == "")
        {
            ui->label_msg->setText("0");
        }
        else if(num2 != "")
        {
            num1 = QString::number(x);
        }
    }

    num2 = "";
    msg = "";
    //将式子放入信息栏中
    if(isSave01)
    {
        msg.append(num1);
    }
    symbol = "-";
    msg.append(symbol);
    ui->label_msg->setText(msg);
    isSave01 = true;
}
//乘法
void Widget::bnt_mul()
{
    double n1 = num1.toDouble();
    double n2 = num2.toDouble();
    double x = 0.0;
    if(symbol == "×")
    {
        //计算三位以上的式子
        x = n1 * n2;
        num1 = QString::number(x);
        qDebug()<<"x="<<x;
    }
    else if (symbol =="-")
    {
        x = n1 - n2;
        num1 = QString::number(x);
    }
    else if (symbol =="+")
    {
        x = n1 + n2;
        num1 = QString::number(x);
    }
    else if (symbol =="÷")
    {
        x = n1 / n2;
        num1 = QString::number(x);
    }
    else if(symbol == "%")
    {
        if(num1 == "")
        {
            ui->label_msg->setText("0");
        }
        else if(num2 != "")
        {
            num1 = QString::number(x);
        }
    }
    num2 = "";
    msg = "";
    //将式子放入信息栏中
    if(isSave01)
    {
        msg.append(num1);
    }
    symbol = "×";
    msg.append(symbol);
    ui->label_msg->setText(msg);
    isSave01 = true;

}
//除法
void Widget::bnt_div()
{
    double n1 = num1.toDouble();
    double n2 = num2.toDouble();
    double x = 0.0;
    if(symbol == "÷")
    {
        //计算三位以上的式子
        x = n1 / n2;
        num1 = QString::number(x);
        qDebug()<<"x="<<x;
    }
    else if (symbol =="-")
    {
        x = n1 - n2;
        num1 = QString::number(x);
    }
    else if (symbol =="×")
    {
        x = n1 * n2;
        num1 = QString::number(x);
    }
    else if (symbol =="+")
    {
        x = n1 + n2;
        num1 = QString::number(x);
    }
    else if(symbol == "%")
    {
        if(num1 == "")
        {
            ui->label_msg->setText("0");
        }
        else if(num2 != "")
        {
            x = n1 / n2;
            num1 = QString::number(x);
        }
    }

    num2 = "";
    msg = "";
    //将式子放入信息栏中
    if(isSave01)
    {
        msg.append(num1);
    }
    symbol = "÷";
    msg.append(symbol);
    ui->label_msg->setText(msg);
    isSave01 = true;
}
//等于
void Widget::bnt_over()
{
    if(isSave02)
    {
        msg.append(num2);
    }
    double n1 = num1.toDouble();
    double n2 = num2.toDouble();

    double x = 0;

    if(symbol == "+")
    {
        //计算按下等于之后的结果
        x = n1 + n2;

    }
    else if(symbol == "-")
    {
        x = n1 - n2;
    }
    else if(symbol == "×")
    {
         x = n1 * n2;
    }
    else if(symbol == "÷")
    {
        if (n2 != 0)
        {
            x = n1 / n2;
        }
        else
        {
            ui->label_idet->setText("除数不能为0");
            return;
        }
    }
    else if(symbol == "%")
    {
        //判断num1存在的同时num2也存在
        if(num1 == "")
        {
            ui->label_msg->setText("0");
        }
        else if(num2 != "")
        {
        }
    }
    else if(symbol == "")
    {
        x = n1;
    }
    msg.append("=");
    ui->label_msg->setText(msg);

    QString str = QString::number(x);
    ui->label_idet->setText(str);
    num1 = str;
    num2 = "";
    symbol = "";
    msg = "";
    isSave02 = true;

}

void Widget::on_clear_clicked(bool checked)
{
    //删除最近一项
    if(symbol == "")
    {
        num1 = "";
        ui->label_idet->setText("0");
    }
    else
    {
        num2 = "";
        ui->label_idet->setText("0");
    }

}

void Widget::on_clearall_clicked(bool checked)
{
    //删除所有项
    num1 = "";
    num2 = "";
    symbol = "";
    ui->label_idet->setText("0");
    msg = "";
    ui->label_msg->clear();
}

void Widget::on_delete_2_clicked(bool checked)
{
    //删除一个数字
    if(symbol == "")
    {
        num1.chop(1);
        ui->label_idet->setText(num1);
    }
    else
    {
        num2.chop(1);
        ui->label_idet->setText(num2);
    }
}

void Widget::on_fenshu_clicked(bool checked)
{
     //化为分数
    if(num1 == 0)
    {
        ui->label_idet->setText("分母不能为0");
    }
    else
    {
        if(symbol == "")
        {
            double n1 = num1.toDouble();
            msg.append("1/(");
            msg.append(QString::number(n1));
            msg.append(")");
            ui->label_msg->setText(msg);

            n1 = 1 / num1.toDouble();
            QString str = QString::number(n1);
            num1 = str;
            ui->label_idet->setText(str);
        }
        else
        {
            double n2 = num2.toDouble();
            isSave02=false;
            msg.append("1/(");
            msg.append(QString::number(n2));
            msg.append(")");
            ui->label_msg->setText(msg);

            n2 = sqrt(num2.toDouble());
            qDebug() << "n2:" << n2 << endl;
            QString str = QString::number(n2);
            num2 = str;
            ui->label_idet->setText(str);
        }
    }
}

void Widget::on_pow_clicked(bool checked)
{
    //平方
    if(symbol == "")
    {
        double n1 = num1.toDouble();
        msg.append("sqr(");
        msg.append(QString::number(n1));
        msg.append(")");
        ui->label_msg->setText(msg);

        n1 = num1.toDouble() * num1.toDouble();
        QString str = QString::number(n1);
        num1 = str;
        ui->label_idet->setText(str);
    }
    else
    {
        double n2 = num2.toDouble();
        isSave02=false;
        msg.append("sqr(");
        msg.append(QString::number(n2));
        msg.append(")");
        ui->label_msg->setText(msg);

        n2 = num2.toDouble() * num2.toDouble();
        qDebug() << "n2:" << n2 << endl;
        QString str = QString::number(n2);
        num2 = str;
        ui->label_idet->setText(str);
    }
}

void Widget::on_genhao_clicked(bool checked)
{
    //开根号
    if(symbol == "")
    {
        double n1 = num1.toDouble();
        msg.append("根号(");
        msg.append(QString::number(n1));
        msg.append(")");
        ui->label_msg->setText(msg);

        n1 = sqrt(num1.toDouble());
        QString str = QString::number(n1);
        num1 = str;
        ui->label_idet->setText(str);
    }
    else
    {
        double n2 = num2.toDouble();
        isSave02=false;
        msg.append("根号(");
        msg.append(QString::number(n2));
        msg.append(")");
        ui->label_msg->setText(msg);

        n2 = sqrt(num2.toDouble());
        qDebug() << "n2:" << n2 << endl;
        QString str = QString::number(n2);
        num2 = str;
        ui->label_idet->setText(str);
    }

}

void Widget::on_aors_clicked(bool checked)
{
    //转换正负数
    if(symbol == "")
    {
        double n1 = num1.toDouble();
        n1 = -n1;
        num1 = QString::number(n1);
        ui->label_idet->setText(num1);
        qDebug() << "符号1:" << num1;
    }
    else
    {
        double n2 = num2.toDouble();
        n2 = -n2;
        num2 = QString::number(n2);
        ui->label_idet->setText(num2);
        qDebug() << "符号2:" << num2;
    }
}

void Widget::on_yu_clicked(bool checked)//有问题
{
    //72 + 5% =3.6
    //72的5%是3.6,所以72+3.6就是75.6
    //将式子放入信息栏中
    double n1 = num1.toDouble();
    double n2 = num2.toDouble();
    double x;
    if(num1 != "" && num2 != "")
    {
        n2 = (n2 / 100) * n1 ;
        num2 = QString::number(n2);
        qDebug() << "n2 = "<<n2;
    }
    else if(num2 != "")
    {
        ui->label_msg->setText("0");

    }
    if(symbol == "÷")
    {
        //计算三位以上的式子
        x = n1 / n2;
        num1 = QString::number(x);
        qDebug()<<"x="<<x;
    }
    else if (symbol =="-")
    {
        x = n1 - n2;
        num1 = QString::number(x);
    }
    else if (symbol =="×")
    {
        x = n1 * n2;
        num1 = QString::number(x);
    }
    else if (symbol =="+")
    {
        x = n1 + n2;
        num1 = QString::number(x);
    }
    msg.append(num2);
    num2 = "";

}
4、main.cpp
#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{

    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}
5、.pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2024-07-15T19:27:26
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = computer
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

RESOURCES += \
    res.qrc
6、资源文件的添加

添加前缀为“ / ”

添加完成之后,点击添加文件,选择照片,保存。

标签:num1,num2,setText,C++,label,ui,计算器,msg,Qt
From: https://blog.csdn.net/weixin_68995006/article/details/140603780

相关文章

  • C++核心编程-4、类和对象4—多态
    4.7多态4.7.1多态的基本语法 示例代码如下:#include<iostream>usingnamespacestd;//多态的基本概念//满足动态多态的条件:1、有继承的关系2、子类要重写父类的虚函数//重写:函数返回值类型函数名参数列表完全相同//动态多态的使用://父类的指针或者引用执行......
  • C++3算法比较第一期
    目录1.递推(Iteration)2.递归(Recursion)3.动态规划(DynamicProgramming,DP)递推、递归与动态规划的区别在C++编程中,递推、递归和动态规划是三种重要的算法思想,它们在解决复杂问题时各有特色。下面将分别介绍这三种算法思想,并探讨它们之间的区别。1.递推(Iteration)定义......
  • C++学习笔记(01)——使用VS Code进行C++函数分文件编写
    首先需要下载安装:C/C++ProjectGenerator扩展,就是下图这玩意:下载安装完成后,按ctrl+shift+p打开命令面板,输入createC++project,按回车后可以选择保存工程的文件夹创建好会后生成几个目录:.vscode:里面放一些配置文件之类的,如launch.json、setting.json、tasks.jsoninclude:存......
  • C++题目:DNA排序 代码
    题目描述现在有一些长度相等的 ......
  • 2024年华为OD机试真题-执行时长-C++-OD统一考试(C卷D卷)
    2024年OD统一考试(D卷)完整题库:华为OD机试2024年最新题库(Python、JAVA、C++合集) 题目描述:为了充分发挥GPU算力,需要尽可能多的将任务交给GPU执行,现在有一个任务数组,数组元素表示在这1秒内新增的任务个数且每秒都有新增任务,假设GPU最多一次执行n个任务,一次执行耗时1秒,在保证GPU......
  • [转]从SQLite到Redis:探索C++与多种数据库的交互之道
    转自:【C++风云录】从SQLite到Redis:探索C++与多种数据库的交互之道开启数据库之旅:通过C++与各种数据库交互,事半功倍1.SQLite1.1简介SQLite是一个嵌入式关系型数据库管理系统,提供了一个轻量级的C++接口。它是一个开源的软件库,无需配置服务器或安装管理工具,可以直接在程序中使......
  • C++链表
    引入链表是一种用于存储数据的数据结构,通过如链条一般的指针来连接元素。它的特点是插入与删除数据十分方便,但寻找与读取数据的表现欠佳。与数组的区别链表和数组都可用于存储数据。与链表不同,数组将所有元素按次序依次存储。不同的存储结构令它们有了不同的优势:链表因其链状......
  • c/c++ jsoncpp的基本使用
    一、概述jsoncpp官网作用:在c++中可以方便的组装及解析json格式的数据。二、代码示例voidMyJsonCpp::toJsonStr(){Json::ValuejsonValue;jsonValue["username"]="luoluoyang";jsonValue["password"]="123456";jsonValue["ag......
  • 三种语言实现计算逆序对的数量(C++/Python/Java)
    题目给定一个长度为......
  • 三种语言实现归并排序(C++/Python/Java)
    题目给定你一个长度为......