【QT】QT+VS2019的基本功能的計算器(附項目資源)

引文

鏈接: https://pan.baidu.com/s/1lKyXTOS8UXJtFRjCASlcA 提取碼: vh24

一份在Visual Studio的QT中寫的簡單的鬧鐘,Qt GUI Application項目,經過調試可能還是會有少些bug,現在將小玩意同大家分享,很期待有朋友一起討論,文章項目資源可以自行下載,我在調試時的qDebug還留着,方便理解。

代碼中主要依靠 IsOperatorClicked 和 IsStoredNumber 來判斷是否進行運算,下面是一個簡單的運行演示。

步驟 點擊的按鈕
初始 IsOperatorClicked = false; IsStoredNumber = false;
1 num4
displayLabel.append(button->text());
2 actionMul(+)
storeNumber=4; IsStoredNumber= true
storeOperator=+; IsOperatorClicked= true
3 num2
IsOperatorClicked = false
4 actionEqual(=)
calculate_result(); IsStoredNumber = false

寫文章才發現英語單詞寫錯了尬尬,把Equal寫成Euqal,就不改了哈,看到的知到它是錯的

Calculator.ui

界面設計及控件命名

在這裏插入圖片描述
在這裏插入圖片描述

Calculator.h

#pragma once
#include <QMainWindow>
#include "ui_Calculator.h"

#include <QAbstractButton>
#include <QtWidgets/QButtonGroup>
#include <QtCore>

namespace Ui {
    class Calculator;
}

class Calculator : public QMainWindow
{
	Q_OBJECT

public:
	Calculator(QWidget *parent = 0);
    ~Calculator();

private slots:
    void numberGroup_clicked(QAbstractButton*);     //123456789
    void actionGroup_clicked(QAbstractButton*);     //+-*/

    void on_actionDel_clicked();        //Del
    void on_actionEqual_clicked();       //=
    void on_comma_clicked();            //.
    void on_actionClear_clicked();      //C
    void on_actionPercent_clicked();    //%
    void on_actionSign_clicked();       //+/-

private:
	Ui::CalculatorClass* ui;

    //限制數值長度
    const int DIGIT_LIMIT = 16;
    //用於檢查上一個點擊的按鈕是否爲運算符
    bool IsOperatorClicked;
    //用於檢查數字是否存儲在內存中
    bool IsStoredNumber;
    //存儲最後點擊的運算符
    QChar storedOperator;
    //存儲數字
    double storedNumber;
    //根據存儲的號碼和顯示的號碼計算結果
    void calculate_result();

    QButtonGroup* actionGroup;      //運算符按鈕組
    QButtonGroup* numberGroup;      //數字按鈕組
};

Calculator.cpp

#include "Calculator.h"

Calculator::Calculator(QWidget* parent)
	: QMainWindow(parent),
	ui(new Ui::CalculatorClass),
	storedNumber(0)
{
	ui->setupUi(this);

	//設置空顯示面板
	ui->displayLabel->clear();

	//將operator和store number和comma標誌設置爲false
	IsOperatorClicked = false;
	IsStoredNumber = false;

	//分配空間
	numberGroup = new QButtonGroup;
	actionGroup = new QButtonGroup;

	//將按鈕放入
	numberGroup->addButton(ui->num1, 1);
	numberGroup->addButton(ui->num2, 2);
	numberGroup->addButton(ui->num3, 2);
	numberGroup->addButton(ui->num4, 4);
	numberGroup->addButton(ui->num5, 5);
	numberGroup->addButton(ui->num6, 6);
	numberGroup->addButton(ui->num7, 7);
	numberGroup->addButton(ui->num8, 8);
	numberGroup->addButton(ui->num9, 9);
	numberGroup->addButton(ui->num0, 0);

	actionGroup->addButton(ui->actionAdd, 1);
	actionGroup->addButton(ui->actionSub, 2);
	actionGroup->addButton(ui->actionMul, 3);
	actionGroup->addButton(ui->actionDiv, 4);
	actionGroup->addButton(ui->actionEuqal, 5);

	//連接信號和槽
	numberGroup->connect(numberGroup, SIGNAL(buttonClicked(QAbstractButton*)),
		this, SLOT(numberGroup_clicked(QAbstractButton*)));

	//連接信號和槽
	actionGroup->connect(actionGroup, SIGNAL(buttonClicked(QAbstractButton*)),
		this, SLOT(actionGroup_clicked(QAbstractButton*)));

	//設置窗口固定寬度和高度
	this->setFixedSize(QSize(412, 433));
}

Calculator::~Calculator()
{
	delete ui;
	delete numberGroup;
	delete actionGroup;
}

//單擊數字按鈕時調用
void Calculator::numberGroup_clicked(QAbstractButton* button)
{
	//獲取QLabel字符串
	QString displayLabel = ui->displayLabel->text();
	qDebug() << "numberGroup_clicked: " << displayLabel;

	/*檢查上一個點擊的按鈕是否是運算符按鈕。
	*如果是,請清除顯示並將標誌設置爲false。然後繼續
	*再添加新的數字。*/
	if (IsOperatorClicked)
	{
		displayLabel.clear();	//清空之前的數字

		qDebug() << "numberGroup_clicked: " << displayLabel;

		IsOperatorClicked = false;
	}

	//超過數字限制,就不加數字
	if (displayLabel.length() >= DIGIT_LIMIT) {
		return;
	}

	//末尾添加點擊的數字
	displayLabel.append(button->text());

	qDebug() << "numberGroup_clicked: " << displayLabel;

	//將數字設回顯示
	ui->displayLabel->setText(displayLabel);

}

//+-*/
void Calculator::actionGroup_clicked(QAbstractButton* button)
{

	/*如果上一個點擊的按鈕是運算符,則只需保存該運算符,然後退出
	*如果上一個點擊的按鈕不是運算符的話,我們需要看看是否只需要保存
	*如果內存中已經存儲了一個數字,則執行計算並存儲結果*/

	if (IsOperatorClicked)		//如果上一個是運算符
	{
		storedOperator = button->text().at(0);	//保存操作符

		qDebug() << "actionGroup_clicked: " << storedOperator;
	}
	else  ////如果上一個不是運算符
	{
		if (IsStoredNumber)		//如果有數字
		{
			calculate_result();
		}
		else
		{
			//從顯示中獲取字符串
			QString displayLabel = ui->displayLabel->text();

			qDebug() << "actionGroup_clicked: " << displayLabel;

			//將字符串轉換爲雙精度並保存
			storedNumber = displayLabel.toDouble();

			qDebug() << "actionGroup_clicked: " << storedNumber;

			//設置標誌,指示我們現在在內存中存儲了一個數字
			IsStoredNumber = true;


		}

		//設置上次單擊的按鈕是運算符的標誌
		IsOperatorClicked = true;

		//將運算符存儲在內存中
		storedOperator = button->text().at(0);

		qDebug() << "actionGroup_clicked: " << storedOperator;
	}
}

//delete
void Calculator::on_actionDel_clicked()
{

	//獲取QLabel字符串
	QString displayLabel = ui->displayLabel->text();

	qDebug() << "on_actionDel_clicked: " << displayLabel;

	//檢查標籤是否爲空
	if (displayLabel.length() == 0)  return;

	//從字符串中刪除最後一個數字
	displayLabel.QString::chop(1);

	qDebug() << "on_actionDel_clicked: " << displayLabel;

	//將數字設回顯示
	ui->displayLabel->setText(displayLabel);
}

//點擊=
void Calculator::on_actionEqual_clicked()
{

	//獲取QLabel字符串
	QString displayLabel = ui->displayLabel->text();

	qDebug() << "on_actionEqual_clicked: " << displayLabel;

	/*必須在內存中保存一個數字才能計算結果。
	*此外,顯示屏上應顯示一個至少有一位數字的數字,並且
	*單擊的最後一個按鈕不應是運算符*/
	if (!IsStoredNumber || displayLabel.length() < 1 || IsOperatorClicked)  return;

	//計算結果並顯示
	calculate_result();

	IsStoredNumber = false;
}

//點擊.
void Calculator::on_comma_clicked()
{
	//獲取QLabel字符串
	QString displayLabel = ui->displayLabel->text();
	qDebug() << "on_comma_clicked: " << displayLabel;

	/*檢查是否超過數字長度
	*檢查是否已經存在另一個逗號。*/
	if (displayLabel.length() >= (DIGIT_LIMIT - 1) || displayLabel.contains('.', Qt::CaseSensitive)) return;

	//如果label爲空,則添加零,然後附加逗號
	if (displayLabel.length() == 0) 
	{
		displayLabel = "0";
	}

	//附加逗號
	displayLabel.append('.');

	qDebug() << "on_comma_clicked: " << displayLabel;

	//將數字設回顯示
	ui->displayLabel->setText(displayLabel);
}

//點擊C
void Calculator::on_actionClear_clicked()
{
	//清空
	ui->displayLabel->clear();

	qDebug() << "on_actionClear_clicked: " << ui->displayLabel->text();

	IsOperatorClicked = false;
	IsStoredNumber = false;
}

//點擊%
void Calculator::on_actionPercent_clicked()
{
	//獲取QLabel字符串
	QString displayLabel = ui->displayLabel->text();

	qDebug() << "on_actionPercent_clicked: " << displayLabel;

	double percentage = displayLabel.toDouble();

	//乘以0.01就可以得到一個百分比
	percentage *= 0.01;

	//因爲可能有溢出,所以精確的轉換數字
	displayLabel = QString::number(percentage, 'g', DIGIT_LIMIT);

	ui->displayLabel->setText(displayLabel);
}

//點擊+/-
void Calculator::on_actionSign_clicked()
{
	QString displayLabel = ui->displayLabel->text();

	qDebug() << "on_actionSign_clicked: " << displayLabel;

	double percentage = displayLabel.toDouble();

	//乘以-1就改變符號
	percentage *= -1;

	displayLabel = QString::number(percentage, 'g', DIGIT_LIMIT);

	ui->displayLabel->setText(displayLabel);
}


//計算
void Calculator::calculate_result()
{
	//獲取QLabel字符串
	QString displayLabel = ui->displayLabel->text();

	qDebug() << "calculate_result: " << displayLabel;

	//如果顯示的數字以逗號結尾,就刪除逗號。
	if (displayLabel.endsWith('.', Qt::CaseSensitive)) {
		displayLabel.QString::chop(1);
	}

	//根據運算符決定
	if (storedOperator == '+') {
		storedNumber += displayLabel.toDouble();

		qDebug() << "calculate_result: " << "+";
	}
	else if (storedOperator == '-') {
		storedNumber -= displayLabel.toDouble();

		qDebug() << "calculate_result: " << "-";
	}
	else if (storedOperator == '*') {
		storedNumber *= displayLabel.toDouble();

		qDebug() << "calculate_result: " << "*";
	}
	else if (storedOperator == '/') {
		storedNumber /= displayLabel.toDouble();

		qDebug() << "calculate_result: " << "/";
	}

	//因爲可能有溢出,所以精確的轉換數字

	//返回與數字n相等的字符串,該字符串根據指定的格式和精度進行格式化。有關詳細信息,請參見參數格式。
	displayLabel = QString::number(storedNumber, 'g', DIGIT_LIMIT);

	qDebug() << "calculate_result: " << displayLabel;

	//將計算好的數值呈現
	ui->displayLabel->setText(displayLabel);
}

main.cpp

#include "Calculator.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	Calculator w;
	w.show();
	return a.exec();
}

如有不足之處,還望指正 1


  1. 如果對您有幫助可以點贊、收藏、關注,將會是我最大的動力 ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章