软件开发第一次作业

发布时间 2023-10-14 14:35:13作者: Yzxj

基于QT和C++面向对象计算器软件开发

系统简介

设计背景

身为一名计算机专业的在校大学生,深刻体会到了专业技能的重要性,这学期开设的《软件开发环境》课程,在当前的学习中我了解了软件开发的基本流程及软件设计和开发的基本工具。为此,我使用OT开发框架和c++开发一个简单的计算器,主要完成堆数据的准确运算。该系统可以实现加、减、乘、除的四则混合运算和算术开方功能。

开发工具及环境

开发工具及介绍

QT是由QT Company 开发的跨平台c++图形用户界面应用程序开发框架。它及可以开发GUI(Graphical User Interface 图形用户界面),也可用于开发非GUI程序,比如控制台和服务器。QT是面向对象的框架,很容易扩展,并且允许正真地组件编程。

QT Creator 是跨平台集成开发环境,实现了对于ios (苹果公司开发的移动操作端)的完全支持。

开发环境

Windows 11系统、QT Creator 11.0.3(community)、C++、Visio

系统分析

设计目的

使用QT Creator 跨平台开发环境设计并编写一个简易计算器程序,用户通过鼠标或键盘操作输入参加的数值和运算符,要求能进行简单的四则运算和算术开方。

  1. 功能需求

应用堆栈和类的继承原理,设计一个面向对象的能够实现基本运算功能的简易计算器。使用QT设计出能够承载该计算器的通用界面,用户可以通过鼠标和键盘输入参加计算的数值,进行加减乘除等混合运算,实现以下功能:

(1)、单击计算器上的数字按钮(0、1、2、3、4、5、6、7、8、9、.)可以设置参与计算的运算数。

(2)、单击计算器上的运算符按钮(+、-、×、÷、√ ̄)可与选择运算符。

(3)、单击“ = ”按钮可以显示计算器的运算结果。

(4)、单击“ AC ”按钮可以将文本框中的内容全部清零。

(5)、单击“ ← ”按钮可以删除文本框中的以为字符。

系统设计与实现

界面设计与实现

利用QT Creator 自带的UI设计工具设计计算器界面。在mainwindow.ui界面中,用Tool Button对按钮进行设定,用Plain Text Edit 显示输出文本框,利用槽函数对按钮绑定。

图示

描述已自动生成

  1. 计算器界面展示

电脑萤幕

描述已自动生成

  1. 代码展示

<?xml version="1.0" encoding="UTF-8"?>

<ui version="4.0">

<class>MainWindow</class>

<widget class="QMainWindow" name="MainWindow">

<property name="geometry">

<rect>

<x>0</x>

<y>0</y>

<width>350</width>

<height>470</height>

</rect>

</property>

<property name="minimumSize">

<size>

<width>350</width>

<height>470</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>350</width>

<height>470</height>

</size>

</property>

<property name="windowTitle">

<string>MainWindow</string>

</property>

<widget class="QWidget" name="centralwidget">

<layout class="QVBoxLayout" name="verticalLayout">

<item>

<widget class="QPlainTextEdit" name="plainTextEdit"/>

</item>

<item>

<layout class="QHBoxLayout" name="horizontalLayout">

<item>

<widget class="QToolButton" name="kaifang">

<property name="minimumSize">

<size>

<width>100</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>100</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>√ ̄</string>

</property>

</widget>

</item>

<item>

<widget class="QToolButton" name="Del">

<property name="minimumSize">

<size>

<width>100</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>100</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>←</string>

</property>

</widget>

</item>

<item>

<widget class="QToolButton" name="AC">

<property name="minimumSize">

<size>

<width>100</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>100</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>AC</string>

</property>

</widget>

</item>

</layout>

</item>

<item>

<layout class="QGridLayout" name="gridLayout">

<property name="spacing">

<number>9</number>

</property>

<item row="0" column="0">

<widget class="QToolButton" name="num7">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>7</string>

</property>

</widget>

</item>

<item row="0" column="1">

<widget class="QToolButton" name="num8">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>8</string>

</property>

</widget>

</item>

<item row="0" column="2">

<widget class="QToolButton" name="num9">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>9</string>

</property>

</widget>

</item>

<item row="0" column="3">

<widget class="QToolButton" name="Div">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>÷</string>

</property>

</widget>

</item>

<item row="1" column="0">

<widget class="QToolButton" name="num4">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>4</string>

</property>

</widget>

</item>

<item row="1" column="1">

<widget class="QToolButton" name="num5">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>5</string>

</property>

</widget>

</item>

<item row="1" column="2">

<widget class="QToolButton" name="num6">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>6</string>

</property>

</widget>

</item>

<item row="1" column="3">

<widget class="QToolButton" name="Mul">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>×</string>

</property>

</widget>

</item>

<item row="2" column="0">

<widget class="QToolButton" name="num1">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>1</string>

</property>

</widget>

</item>

<item row="2" column="1">

<widget class="QToolButton" name="num2">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>2</string>

</property>

</widget>

</item>

<item row="2" column="2">

<widget class="QToolButton" name="num3">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>3</string>

</property>

</widget>

</item>

<item row="2" column="3">

<widget class="QToolButton" name="Sub">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>-</string>

</property>

</widget>

</item>

<item row="3" column="0">

<widget class="QToolButton" name="num10">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>.</string>

</property>

</widget>

</item>

<item row="3" column="1">

<widget class="QToolButton" name="num0">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>0</string>

</property>

</widget>

</item>

<item row="3" column="2">

<widget class="QToolButton" name="Equal">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>=</string>

</property>

</widget>

</item>

<item row="3" column="3">

<widget class="QToolButton" name="Add">

<property name="minimumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="maximumSize">

<size>

<width>75</width>

<height>50</height>

</size>

</property>

<property name="text">

<string>+</string>

</property>

</widget>

</item>

</layout>

</item>

</layout>

</widget>

<widget class="QMenuBar" name="menubar">

<property name="geometry">

<rect>

<x>0</x>

<y>0</y>

<width>350</width>

<height>21</height>

</rect>

</property>

</widget>

<widget class="QStatusBar" name="statusbar"/>

</widget>

<resources/>

<connections/>

</ui>

//槽函数的绑定;

//利用for循环对数字按钮进行绑定;

texT = ui->plainTextEdit->toPlainText();

QToolButton *numButtons[11]; //定义空间大小为11的数组存放数字按钮对象(包含小数点)

for(int k=0;k<11;k++)

{

QString numName = "num"+QString::number(k);

numButtons[k] = MainWindow::findChild<QToolButton *>(numName);

connect(numButtons[k],SIGNAL(clicked(bool)),this,SLOT(numOnClick()));

}

//绑定计算器符号按钮;

connect(ui->Add,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Sub,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Mul,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Div,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->AC,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Del,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

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

connect(ui->Equal,SIGNAL(clicked(bool)),this,SLOT(equalNum()));

功能设计与实现

  1. 流程图

加法

减法

乘法

除法

整体流程图

  1. 代码

//对数字进行赋值(点击数字按钮进行的操作);

void MainWindow::numOnClick()

{

QToolButton *numName = (QToolButton*)sender(); //sender()返回当前点击的对象

ui->plainTextEdit->textCursor().insertText(numName->text());

texT = ui->plainTextEdit->toPlainText();

//text().toDouble()将字符串String转换成double类型,QString::number(3)将数字3转换成字符串"3"

if(Add)

{

int i = texT.indexOf("+");

texT = texT.mid(i+1);

b = texT;

}

else if(Sub)

{

int i = texT.indexOf("-");

texT = texT.mid(i+1);

b = texT;

}

else if(Mul)

{

int i = texT.indexOf("×");

texT = texT.mid(i+1);

b = texT;

}

else if(Div)

{

int i = texT.indexOf("÷");

texT = texT.mid(i+1);

b = texT;

}

else a=texT;

qDebug()<<a<<b;

}

//对符号进行设置

void MainWindow::fuHao()

{

QToolButton *fh = (QToolButton*)sender();

QString f = fh->text();

if(!(Add||Sub||Mul||Div)) //限定只输入一个运算符

{

if(f == "+")

{

Add = true;

ui->plainTextEdit->textCursor().insertText("+");

}

if(f == "-")

{

Sub = true;

ui->plainTextEdit->textCursor().insertText("-");

}

if(f == "×")

{

Mul = true;

ui->plainTextEdit->textCursor().insertText("×");

}

if(f == "÷")

{

Div = true;

ui->plainTextEdit->textCursor().insertText("÷");

}

}

if(f == "←")

{

texT = ui->plainTextEdit->toPlainText();

texT.chop(1);

Add=Sub=Mul=Div=false;

matchFh(); //识别输入文本的符号并分割

ui->plainTextEdit->setPlainText(texT);

ui->plainTextEdit->moveCursor(QTextCursor::End);

}

if(f == "AC")

{

//a=b=0;

Add=Sub=Mul=Div=false;

ui->plainTextEdit->setPlainText("");

}

}

//识别输入文本的符号并分割

void MainWindow::matchFh()

{

if(texT.contains("+",Qt::CaseSensitive)) //检测输入文本是否有“+”号

{

QStringList t = texT.split("+");

a = t[0]; //将分割的字符分别赋值给a和b

b = t[1];

Add = true;

}

else if(texT.contains("-",Qt::CaseSensitive)) //检测输入文本是否有“-”号

{

QStringList t = texT.split("-");

a = t[0];

b = t[1];

Sub = true;

}

else if(texT.contains("×",Qt::CaseSensitive)) //检测输入文本是否有“×”号

{

QStringList t = texT.split("×");

a = t[0];

b = t[1];

Mul = true;

}

else if(texT.contains("÷",Qt::CaseSensitive)) //检测输入文本是否有“÷”号

{

QStringList t = texT.split("÷");

a = t[0];

b = t[1];

Div = true;

}

else a = texT; //这是删掉了很多只剩数字的情况了

}

//等于号的实现;

void MainWindow::equalNum()

{

double x;

double y;

texT = ui->plainTextEdit->toPlainText();

matchFh();

x = a.toDouble();

y = b.toDouble();

qDebug()<<"x:"<<x<<"y:"<<y;

if(Add)

{

ui->plainTextEdit->setPlainText(a=QString::number(x+y));

Add = false;

}

if(Sub)

{

ui->plainTextEdit->setPlainText(a=QString::number(x-y));

Sub = false;

}

if(Mul)

{

ui->plainTextEdit->setPlainText(a=QString::number(x*y));

Mul = false;

}

if(Div)

{

ui->plainTextEdit->setPlainText(a=QString::number(x/y));

Div = false;

}

ui->plainTextEdit->moveCursor(QTextCursor::End);

}

系统测试

23+52=75

52-28=24

5*2=10

10/2=5

整体代码展示

mainwindow.h 文件

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE

namespace Ui { class MainWindow; }

QT_END_NAMESPACE

class MainWindow : public QMainWindow

{

Q_OBJECT

public:

MainWindow(QWidget *parent = nullptr);

void matchFh(); //识别输入文本的符号与分割

~MainWindow();

private slots:

void numOnClick();

void fuHao();

void equalNum();

// double kaifang( int x );

private:

Ui::MainWindow *ui;

};

#endif // MAINWINDOW_H

mainwindow.cpp 文件

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QDebug>

#include <QString>

#include <QToolButton>

#include <QTextCursor>

#include <QVBoxLayout>

//命名规范

//类名首字母大写,单词和单词之间首字母大写

//函数名变量名称首字母小写,单词和单词之间首字母大写

QString a; //保存用户输入的数字;

QString b; //保存符号;

QString texT; //记录UI界面输入的文本;

bool Add=false;

bool Sub=false;

bool Mul=false;

bool Div=false;

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

, ui(new Ui::MainWindow)

{

ui->setupUi(this);

this->setWindowTitle("计算器");

ui->plainTextEdit->setPlainText("");//设置文本框的初始状态(为空);

//槽函数的绑定;

//利用for循环对数字按钮进行绑定;

texT = ui->plainTextEdit->toPlainText();

QToolButton *numButtons[11]; //定义空间大小为11的数组存放数字按钮对象(包含小数点)

for(int k=0;k<11;k++)

{

QString numName = "num"+QString::number(k);

numButtons[k] = MainWindow::findChild<QToolButton *>(numName);

connect(numButtons[k],SIGNAL(clicked(bool)),this,SLOT(numOnClick()));

}

//绑定计算器符号按钮;

connect(ui->Add,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Sub,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Mul,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Div,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->AC,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

connect(ui->Del,SIGNAL(clicked(bool)),this,SLOT(fuHao()));

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

connect(ui->Equal,SIGNAL(clicked(bool)),this,SLOT(equalNum()));

}

MainWindow::~MainWindow()

{

delete ui;

}

//对数字进行赋值(点击数字按钮进行的操作);

void MainWindow::numOnClick()

{

QToolButton *numName = (QToolButton*)sender(); //sender()返回当前点击的对象

ui->plainTextEdit->textCursor().insertText(numName->text());

texT = ui->plainTextEdit->toPlainText();

//text().toDouble()将字符串String转换成double类型,QString::number(3)将数字3转换成字符串"3"

if(Add)

{

int i = texT.indexOf("+");

texT = texT.mid(i+1);

b = texT;

}

else if(Sub)

{

int i = texT.indexOf("-");

texT = texT.mid(i+1);

b = texT;

}

else if(Mul)

{

int i = texT.indexOf("×");

texT = texT.mid(i+1);

b = texT;

}

else if(Div)

{

int i = texT.indexOf("÷");

texT = texT.mid(i+1);

b = texT;

}

else a=texT;

qDebug()<<a<<b;

}

//对符号进行设置

void MainWindow::fuHao()

{

QToolButton *fh = (QToolButton*)sender();

QString f = fh->text();

if(!(Add||Sub||Mul||Div)) //限定只输入一个运算符

{

if(f == "+")

{

Add = true;

ui->plainTextEdit->textCursor().insertText("+");

}

if(f == "-")

{

Sub = true;

ui->plainTextEdit->textCursor().insertText("-");

}

if(f == "×")

{

Mul = true;

ui->plainTextEdit->textCursor().insertText("×");

}

if(f == "÷")

{

Div = true;

ui->plainTextEdit->textCursor().insertText("÷");

}

}

if(f == "←")

{

texT = ui->plainTextEdit->toPlainText();

texT.chop(1);

Add=Sub=Mul=Div=false;

matchFh(); //识别输入文本的符号并分割

ui->plainTextEdit->setPlainText(texT);

ui->plainTextEdit->moveCursor(QTextCursor::End);

}

if(f == "AC")

{

//a=b=0;

Add=Sub=Mul=Div=false;

ui->plainTextEdit->setPlainText("");

}

}

//识别输入文本的符号并分割

void MainWindow::matchFh()

{

if(texT.contains("+",Qt::CaseSensitive)) //检测输入文本是否有“+”号

{

QStringList t = texT.split("+");

a = t[0]; //将分割的字符分别赋值给a和b

b = t[1];

Add = true;

}

else if(texT.contains("-",Qt::CaseSensitive)) //检测输入文本是否有“-”号

{

QStringList t = texT.split("-");

a = t[0];

b = t[1];

Sub = true;

}

else if(texT.contains("×",Qt::CaseSensitive)) //检测输入文本是否有“×”号

{

QStringList t = texT.split("×");

a = t[0];

b = t[1];

Mul = true;

}

else if(texT.contains("÷",Qt::CaseSensitive)) //检测输入文本是否有“÷”号

{

QStringList t = texT.split("÷");

a = t[0];

b = t[1];

Div = true;

}

else a = texT; //这是删掉了很多只剩数字的情况了

}

//等于号的实现;

void MainWindow::equalNum()

{

double x;

double y;

texT = ui->plainTextEdit->toPlainText();

matchFh();

x = a.toDouble();

y = b.toDouble();

qDebug()<<"x:"<<x<<"y:"<<y;

if(Add)

{

ui->plainTextEdit->setPlainText(a=QString::number(x+y));

Add = false;

}

if(Sub)

{

ui->plainTextEdit->setPlainText(a=QString::number(x-y));

Sub = false;

}

if(Mul)

{

ui->plainTextEdit->setPlainText(a=QString::number(x*y));

Mul = false;

}

if(Div)

{

ui->plainTextEdit->setPlainText(a=QString::number(x/y));

Div = false;

}

ui->plainTextEdit->moveCursor(QTextCursor::End);

}