麻烦到不能再麻烦的实现,简单到不能再简单的思路。

calc.h

#ifndef CALC_H
#define CALC_H #include <QtWidgets/QMainWindow>
#include "ui_calc.h" class calc : public QMainWindow
{
Q_OBJECT public:
calc(QWidget *parent = 0);
~calc();
double x;
double y;
char ch;
bool flag; private:
Ui::calcClass ui;
private slots:
void on_pushButton_0_clicked();
void on_pushButton_1_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_9_clicked();
void on_pushButton_divide_clicked();
void on_pushButton_equal_clicked();
void on_pushButton_multi_clicked();
void on_pushButton_plus_clicked();
void on_pushButton_point_clicked();
void on_pushButton_sub_clicked();
}; #endif // CALC_H

calc.cpp

#include "calc.h"
#include <QString>
#include <QMessageBox> calc::calc(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
x = 0;
y = 0;
flag = false;
setWindowTitle(QStringLiteral("计算器")); } calc::~calc()
{ } void calc::on_pushButton_0_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" || temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("0");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("0"));
}
}
void calc::on_pushButton_1_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("1");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("1"));
}
}
void calc::on_pushButton_2_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("2");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("2"));
}
}
void calc::on_pushButton_3_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("3");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("3"));
}
}
void calc::on_pushButton_4_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("4");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("4"));
}
}
void calc::on_pushButton_5_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("5");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("5"));
}
}
void calc::on_pushButton_6_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("6");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("6"));
}
}
void calc::on_pushButton_7_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("7");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("7"));
}
}
void calc::on_pushButton_8_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("8");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("8"));
}
}
void calc::on_pushButton_9_clicked()
{
if (flag)
{
ui.label_2->clear();
flag = false;
}
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString("9");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText(QString("9"));
}
}
void calc::on_pushButton_divide_clicked()
{
QString temp = ui.label_2->text();
if (temp != "" || temp != NULL)
{
ui.label_2->clear();
ui.label_2->setText("/");
ch = '/';
x = temp.toDouble();
}
else
{
ui.label_2->setText("error");
}
}
void calc::on_pushButton_equal_clicked()
{
QString temp = ui.label_2->text();
if (temp != "" || temp != NULL)
{
flag = true;
y = temp.toDouble();
switch (ch)
{
case '+' :
ui.label_2->setText(QString("%1").arg(x + y));
break;
case '-' :
ui.label_2->setText(QString("%1").arg(x - y));
break;
case '*' :
ui.label_2->setText(QString("%1").arg(x * y));
break;
case '/' :
ui.label_2->setText(QString("%1").arg(x / y));
break;
default:
break;
}
}
else
{
ui.label_2->setText("error");
}
}
void calc::on_pushButton_multi_clicked()
{
QString temp = ui.label_2->text();
if (temp != "" || temp != NULL)
{
ui.label_2->clear();
ui.label_2->setText("*");
x = temp.toDouble();
ch = '*';
}
else
{
ui.label_2->setText("error");
}
}
void calc::on_pushButton_plus_clicked()
{
QString temp = ui.label_2->text();
if (temp != "" || temp != NULL)
{
ui.label_2->clear();
ui.label_2->setText("+");
x = temp.toDouble();
ch = '+';
}
else
{
ui.label_2->setText("error");
}
}
void calc::on_pushButton_point_clicked()
{
QString temp = ui.label_2->text();
if (temp != "" && temp != NULL && (temp != "+" && temp != "-" && temp != "*" && temp != "/"))
{
temp += QString(".");
ui.label_2->setText(temp);
} else
{
ui.label_2->setText("error");
}
}
void calc::on_pushButton_sub_clicked()
{
QString temp = ui.label_2->text();
if (temp != "" || temp != NULL)
{
ui.label_2->clear();
ui.label_2->setText("-");
x = temp.toDouble();
ch = '-';
}
else
{
ui.label_2->setText("error");
}
}

Qt实现简易计算器的更多相关文章

  1. Qt、C++ 简易计算器

    Qt.C++实现简易计算器: 以下内容是我实现这个简易计算器整个过程,其中包括我对如何实现这个功能的思考.中途遇到的问题.走过的弯路 整个实现从易到难,计算器功能从简单到复杂,最开始设计的整个实现步骤 ...

  2. python + PyQt5 实现 简易计算器

    忽然想起之前一直想写个简单的计算器,今天就写了一下,界面有些简陋,但是基本功能实现没有问题 以下是源码: # --*-- coding:utf-8 --*-- import sys from PyQt ...

  3. 【PyQt5-Qt Designer】简易的数字键盘输入+简易计算器

    参考如下键盘格式写了一个键盘输入,目前还不能进行运算,后期完善... 效果如下: 完整代码: from PyQt5.QtWidgets import (QApplication,QWidget,QPu ...

  4. PyQt5 简易计算器

    剩下计算函数(self.calculator)未实现,有兴趣的朋友可以实现它 [知识点] 1.利用循环添加按钮部件,及给每个按钮设置信号/槽 2.给按钮设置固定大小:button.setFixedSi ...

  5. 自制c#简易计算器

    这是一个课堂作业,我觉得作为一个简易的计算器不需要态度复杂的东西,可能还有一些bug,有空再慢慢加强. using System;using System.Collections.Generic;us ...

  6. 剖析简易计算器带你入门微信小程序开发

    写在前面,但是重点在后面 这是教程,也不是教程. 可以先看Demo的操作动图,看看是个什么玩意儿,GitHub地址(https://github.com/dunizb/wxapp-sCalc) 自从微 ...

  7. PHP学习笔记02——简易计算器

    <!DOCTYPE html> <html> <head> <title>PHP简易计算器</title> </head> &l ...

  8. JavaScript之简易计算器

    <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...

  9. 菜鸟学习Struts——简易计算器

    这是学习Struts的一个简单的例子文件结构如下: 1.配置Struts环境 2.新建input.jsp,success.jsp,error.jsp input.jsp代码如下: <%@ pag ...

随机推荐

  1. js动态改变下拉框内容

    今天为大家分享一篇js动态设置select下拉菜单的默认选中项实例,具有很好的参考价值,希望对大家有所帮助. 代码实例如下: <!DOCTYPE html> <html lang=& ...

  2. docker-bind挂载

    使用绑定挂载 自Docker早期以来,绑定挂载一直存在.与卷相比,绑定装载具有有限的功能.使用绑定装入时,主机上的文件或目录将装入容器中.文件或目录由其在主机上的完整路径或相对路径引用.相反,当您使用 ...

  3. docker故障排查

    代理服务器设置 代理服务器可以在启动并运行后阻止与Web应用程序的连接.如果您位于代理服务器后面,请使用以下ENV命令将以下行添加到Dockerfile中,以指定代理服务器的主机和端口: # Set ...

  4. 【转】【e周美文】优秀博客上榜推荐

    Everybody,本周的博客推荐开始啦,记住,有好的博客可要给小活推荐一下哦. 7.19日 博客推荐 Android权限列表作者:@大漠落日 链接:http://my.eoe.cn/1103623/ ...

  5. 搞定SpringBoot多数据源(1):多套源策略

    目录 1. 引言 2. 运行环境 3. 多套数据源 3.1 搭建 Spring Boot 工程 3.1.1 初始化 Spring Boot 工程 3.1.2 添加 MyBatis Plus 依赖 3. ...

  6. js的内存泄漏场景、监控以及分析

    内存泄漏 Q:什么是内存泄漏? 字面上的意思,申请的内存没有及时回收掉,被泄漏了 Q:为什么会发生内存泄漏? 虽然前端有垃圾回收机制,但当某块无用的内存,却无法被垃圾回收机制认为是垃圾时,也就发生内存 ...

  7. Thinkpad S440 I/O接口配置

    HDMI 视频接口 SS USB3.0接口 电源接口 音频接口 网络接口 没有com口可以用USB口,然后安装一个USB转com口的驱动.

  8. java实现FTP文件下载

    package com.vingsoft.util;/*** @author 作者:dujj* @version 创建时间:2020年1月13日 下午5:53:39*/import java.io.F ...

  9. App的基本结构

    今天主要学习安装了Android Studio,并且成功地在虚拟机上运行了HelloWord工程.下面针对HelloWord工程对app的基本框架结构进行一个总结.掌握app的基本结构对初学软件开发的 ...

  10. 一起来学习XPATH,来看看除了正则表达式我们还能怎么抓取数据

    参考学习的网站链接http://www.w3school.com.cn/xpath/xpath_intro.asp 首先理清楚一些常识 以此为例 <?xml version="1.0& ...