使用c++语言基于QT框架设计的计算器小程序

发布时间 2023-10-14 18:24:35作者: blackcathead

(注:由于从未接触软件设计,后端代码也是一塌糊涂,对于一些先进的设计软件也未曾接触,如qt,vs创建MFC文件,故本次作业最大难点在于

如何将已经学习的知识和未接触过的领域结合起来。秉承程序员基本素养,利用一切可以利用的资源(感谢所有开源大佬所做的贡献),如bilibili,

csdn,博客园,github,stack overflow,途中遇到了很多难题,如软件的安装,软件内部的插件和分类,又如程序流程图,visio安装很麻烦,故我用平替网站

https://app.diagrams.net/完成流程图。写代码很麻烦,使用gpt镜像网站是一条捷径。跟纯代码比起来,使用先进专业的软件毫无疑问能大大减少

工作量(虽然都不会使用)。铺天盖地的报错使我眼花缭乱(那些打不败我的,还不如直接打败我)。

1.UI

 

2.流程图

3.加减乘除牛顿迭代法开平方根代码

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <math.h>
 4 
 5 // 牛顿迭代法计算平方根
 6 double squareRoot(double x) {
 7     if (x < 0) {
 8         printf("Invalid input: The square root of a negative number is undefined.\n");
 9         return -1.0;
10     }
11 
12     double guess = x;
13     double epsilon = 1e-6; // 设置一个较小的精度
14 
15     while (fabs(guess * guess - x) > epsilon) {
16         guess = 0.5 * (guess + x / guess);
17     }
18 
19     return guess;
20 }
21 
22 int main() {
23     char operation;
24     double num1, num2, result;
25 
26     printf("Enter an operation (+, -, *, /, or sqrt): ");
27     scanf(" %c", &operation);
28 
29     switch (operation) {
30     case '+':
31         printf("Enter two numbers: ");
32         scanf("%lf %lf", &num1, &num2);
33         result = num1 + num2;
34         break;
35     case '-':
36         printf("Enter two numbers: ");
37         scanf("%lf %lf", &num1, &num2);
38         result = num1 - num2;
39         break;
40     case '*':
41         printf("Enter two numbers: ");
42         scanf("%lf %lf", &num1, &num2);
43         result = num1 * num2;
44         break;
45     case '/':
46         printf("Enter two numbers: ");
47         scanf("%lf %lf", &num1, &num2);
48         if (num2 == 0) {
49             printf("Division by zero is not allowed.\n");
50             return 1; // 返回非零值表示错误
51         }
52         result = num1 / num2;
53         break;
54     case 's':
55         printf("Enter a number for square root: ");
56         scanf("%lf", &num1);
57         result = squareRoot(num1);
58         break;
59     default:
60         printf("Invalid operation.\n");
61         return 1; // 返回非零值表示错误
62     }
63 
64     printf("Result: %.6lf\n", result);
65 
66     return 0; // 返回零表示成功
67 }

4.使用c++语言基于qt实现软件

项目文件:projectname.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++17
 6 
 7 # You can make your code fail to compile if it uses deprecated APIs.
 8 # In order to do so, uncomment the following line.
 9 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
10 
11 SOURCES += \
12     main.cpp \
13     widget.cpp
14 
15 HEADERS += \
16     widget.h
17 
18 FORMS += \
19     widget.ui
20 
21 # Default rules for deployment.
22 qnx: target.path = /tmp/$${TARGET}/bin
23 else: unix:!android: target.path = /opt/$${TARGET}/bin
24 !isEmpty(target.path): INSTALLS += target

头文件:filename.h

 1 #ifndef WIDGET_H
 2 #define WIDGET_H
 3 
 4 #include <QWidget>
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class Widget; }
 8 QT_END_NAMESPACE
 9 
10 class Widget : public QWidget
11 {
12     Q_OBJECT
13 
14 public:
15     Widget(QWidget *parent = nullptr);
16     ~Widget();
17 
18 private slots:
19     void on_suma_clicked();
20 
21     void on_resta_clicked();
22 
23     void on_sqrt_clicked();
24 
25     void on_multiplica_clicked();
26 
27     void on_divide_clicked();
28 
29 private:
30     Ui::Widget *ui;
31 };
32 #endif // WIDGET_H

源文件:main.cpp

 1 #include "widget.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     a.setStyle("default");
 9     Widget w;
10     w.show();
11     return a.exec();
12 }

y源文件:filename.cpp

 1 //created by Yisus7u7 in termux
 2 
 3 #include "widget.h"
 4 #include "ui_widget.h"
 5 
 6 Widget::Widget(QWidget *parent)
 7     : QWidget(parent)
 8     , ui(new Ui::Widget)
 9 {
10     ui->setupUi(this);
11     setWindowTitle("Simple Calculator");
12     ui->result->setText("0.0");
13 }
14 
15 Widget::~Widget()
16 {
17     delete ui;
18 }
19 
20 
21 void Widget::on_suma_clicked()
22 {
23     ui->result->setText(QString::number(ui->n1->value() + ui->n2->value()));
24 }
25 void Widget::on_resta_clicked()
26 {
27     ui->result->setText(QString::number(ui->n1->value() - ui->n2->value()));
28 }
29 
30 void Widget::on_sqrt_clicked()
31 {
32     // 在你的槽函数或事件处理函数中:
33     double numberToCalculateSqrt = ui->n1->value(); // 获取要计算平方根的数值
34     double epsilon = 1e-6; // 设置一个较小的精度
35 
36     if (numberToCalculateSqrt < 0) {
37         ui->result->setText("Invalid input: Square root of a negative number is undefined.");
38     } else {
39         double guess = numberToCalculateSqrt; // 初始猜测值
40 
41         while (qAbs(guess * guess - numberToCalculateSqrt) > epsilon) {
42             guess = 0.5 * (guess + numberToCalculateSqrt / guess); // 牛顿迭代公式
43         }
44 
45         ui->result->setText(QString::number(guess));
46     }
47 
48 }
49 
50 void Widget::on_multiplica_clicked()
51 {
52     ui->result->setText(QString::number(ui->n1->value() * ui->n2->value()));
53 }
54 
55 void Widget::on_divide_clicked()
56 {
57     ui->result->setText(QString::number(ui->n1->value() / ui->n2->value()));
58 }

界面文件:filename.ui

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <ui version="4.0">
  3  <class>Widget</class>
  4  <widget class="QWidget" name="Widget">
  5   <property name="geometry">
  6    <rect>
  7     <x>0</x>
  8     <y>0</y>
  9     <width>458</width>
 10     <height>270</height>
 11    </rect>
 12   </property>
 13   <property name="windowTitle">
 14    <string>Widget</string>
 15   </property>
 16   <property name="windowIcon">
 17    <iconset theme="accessories-calculator">
 18     <normaloff>.</normaloff>.</iconset>
 19   </property>
 20   <widget class="QWidget" name="layoutWidget">
 21    <property name="geometry">
 22     <rect>
 23      <x>20</x>
 24      <y>50</y>
 25      <width>428</width>
 26      <height>154</height>
 27     </rect>
 28    </property>
 29    <layout class="QVBoxLayout" name="verticalLayout">
 30     <item>
 31      <layout class="QGridLayout" name="gridLayout">
 32       <item row="0" column="0">
 33        <widget class="QLabel" name="label">
 34         <property name="text">
 35          <string>Number 1:</string>
 36         </property>
 37        </widget>
 38       </item>
 39       <item row="0" column="1">
 40        <widget class="QDoubleSpinBox" name="n1">
 41         <property name="cursor">
 42          <cursorShape>IBeamCursor</cursorShape>
 43         </property>
 44         <property name="minimum">
 45          <double>-999999999.000000000000000</double>
 46         </property>
 47         <property name="maximum">
 48          <double>999999999.000000000000000</double>
 49         </property>
 50        </widget>
 51       </item>
 52       <item row="1" column="0">
 53        <widget class="QLabel" name="label_2">
 54         <property name="text">
 55          <string>Number 2:</string>
 56         </property>
 57        </widget>
 58       </item>
 59       <item row="1" column="1">
 60        <widget class="QDoubleSpinBox" name="n2">
 61         <property name="cursor">
 62          <cursorShape>IBeamCursor</cursorShape>
 63         </property>
 64         <property name="minimum">
 65          <double>-999999999.000000000000000</double>
 66         </property>
 67         <property name="maximum">
 68          <double>999999999.000000000000000</double>
 69         </property>
 70        </widget>
 71       </item>
 72      </layout>
 73     </item>
 74     <item>
 75      <layout class="QHBoxLayout" name="horizontalLayout">
 76       <item>
 77        <widget class="QPushButton" name="suma">
 78         <property name="cursor">
 79          <cursorShape>PointingHandCursor</cursorShape>
 80         </property>
 81         <property name="text">
 82          <string>+</string>
 83         </property>
 84        </widget>
 85       </item>
 86       <item>
 87        <widget class="QPushButton" name="resta">
 88         <property name="cursor">
 89          <cursorShape>PointingHandCursor</cursorShape>
 90         </property>
 91         <property name="text">
 92          <string>-</string>
 93         </property>
 94        </widget>
 95       </item>
 96       <item>
 97        <widget class="QPushButton" name="sqrt">
 98         <property name="text">
 99          <string>s</string>
100         </property>
101        </widget>
102       </item>
103       <item>
104        <widget class="QPushButton" name="multiplica">
105         <property name="cursor">
106          <cursorShape>PointingHandCursor</cursorShape>
107         </property>
108         <property name="text">
109          <string>x</string>
110         </property>
111        </widget>
112       </item>
113       <item>
114        <widget class="QPushButton" name="divide">
115         <property name="cursor">
116          <cursorShape>PointingHandCursor</cursorShape>
117         </property>
118         <property name="text">
119          <string>÷</string>
120         </property>
121        </widget>
122       </item>
123      </layout>
124     </item>
125     <item>
126      <widget class="QLabel" name="label_3">
127       <property name="text">
128        <string>Result:</string>
129       </property>
130      </widget>
131     </item>
132     <item>
133      <widget class="QLabel" name="result">
134       <property name="cursor">
135        <cursorShape>WhatsThisCursor</cursorShape>
136       </property>
137       <property name="text">
138        <string>0</string>
139       </property>
140      </widget>
141     </item>
142    </layout>
143   </widget>
144  </widget>
145  <resources/>
146  <connections/>
147 </ui>

5.测试截图

(1)加法

(2)减法

(3)乘法

(4)除法

(5)开根