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

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

时间:2023-10-14 18:35:44浏览次数:51  
标签:Widget QT void guess c++ ui result 计算器 num1

(注:由于从未接触软件设计,后端代码也是一塌糊涂,对于一些先进的设计软件也未曾接触,如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)开根

 

标签:Widget,QT,void,guess,c++,ui,result,计算器,num1
From: https://www.cnblogs.com/244-385-205/p/17764520.html

相关文章

  • Pyinstaller打包PyQt5和PaddleOCR项目实战经验分享
    简介先前做了一个PyQt5和PaddleOCR结合的项目,但在使用Pyinstaller打包时却踩了很多坑,因此分享一下,以便后人乘凉。(Pycharm)1.项目涉及图片或者文件等依赖(1)图片依赖 第一步:创建一个resources.qrc文件;第二步:将resources.qrc文件转换为.py文件,具体转换过程不赘述;第三步:在使用到......
  • C++基本算法大致总结
    排序算法:快速排序(QuickSort):使用std::sort或自定义实现。归并排序(MergeSort):自定义实现或使用std::stable_sort。堆排序(HeapSort):自定义实现或使用std::make_heap和std::sort_heap。搜索算法:二分查找(BinarySearch):使用std::binary_search或自定义实现。线性......
  • 基于Tkinter库设计的计算器
    基于Tkinter库设计的计算器Tkinter库:Tkinter是Python的标准GUI库。使用Tkinter库可以可以快速的创建GUI应用程序。1、创建计算器的窗口#创建一个窗口root=tk.Tk()root.minsize(300,480)root.title('计算器')result1=tk.StringVar()result1.set(0)result2=tk.St......
  • 【华为OD统一考试B卷 | 100分】 报数问题 (1到3报数)(C++ Java Python javaScript)
    华为OD在线刷题平台平台涵盖了华为OD机试A卷+B卷的真题。平台的题库不断更新,确保能够涵盖华为OD机试的所有真题。点击链接注册并开始你的刷题之旅:点击立即刷题华为OD统一考试A卷+B卷新题库说明2023年5月份,华为官方已经将的2022/0223Q(1/2/3/4)统一修改为OD统一考试(A卷)和OD统......
  • 抽象工厂模式--C++实现
    具体代码实现#include<iostream>usingnamespacestd;classMan{public:virtualvoidshow()=0;};classWoman{public:virtualvoidshow()=0;};classYellowMan:publicMan{public:virtualvoidshow(){cout<<"......
  • C++四舍五入
    C++四舍五入1.利用C++输出函数cout<<setprecision(4)<<1.94999<<endl;只使用setprecision:控制浮点数的输出位数,注意控制的是总输出的数字个数,不是小数点后,并且将默认将小数后面的末尾0省略,并且将四舍五入cout<<fixed<<setprecision(2)<<1.48999<<endl;使用fixedsetprecisi......
  • QT基础教程(QMap和QHash)
    (文章目录)前言本篇文章将为大家讲解QT中两个非常重要的类:QMap和QHash。QMap和QHash都是Qt框架中用于存储键值对的数据结构,它们提供了快速的查找、插入和删除操作,但在某些方面有一些不同之处。一、QMapQMap是一个有序的键值对容器,它根据键的顺序来存储元素。当您需要按照键的......
  • ACS系列(5) ACS QT版C Demo Measurement
    1)工程文件QT=coreCONFIG+=c++17cmdline#YoucanmakeyourcodefailtocompileifitusesdeprecatedAPIs.#Inordertodoso,uncommentthefollowingline.#DEFINES+=QT_DISABLE_DEPRECATED_BEFORE=0x060000#disablesalltheAPIsdeprecatedbefor......
  • 大一上学期程序设计笔记_C++
    罕见的数据类型枚举类型   enum枚举类型名T{Sunday=1,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};            枚举类型名T  变量表枚举类型只能进行赋值和比较运算。不能把整数赋给枚举型变量。枚举内部的元素会从0开始连续编码。类......
  • QT 界面隐藏标题栏后设置可支持拖动
    QT界面隐藏标题栏后设置可支持拖动,需要重写界面的mousePressEvent,mouseMoveEvent,mouseReleaseEvent事件,代码如下。1#include<QWidget>2#include<QMouseEvent>34classCustomWidget:publicQWidget{5Q_OBJECT67public:8explicitCustomWidg......