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

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. 使用Django进行后端开发

    目录 更换pip源 搭建虚拟环境 项目目录设置 项目相关配置 更换pip源 更换pip源 搭建虚拟环境 搭建虚拟开发环境 项目目录设置 项目目录设置 项目相关配置 项目相关配置

  2. 洛谷P1462 通往奥格瑞玛的道路 题解 最短路+二分答案

    题目链接:https://www.luogu.com.cn/problem/P1462 题目大意: 有 \(n\) 个点 \(m\) 条边,每个点有一个点权,每个边有一个边权.求所有长度不超过 \(b ...

  3. 细说javascript typeof操作符

    细说javascript typeof操作符 typeof定义 typeof是一元运算符,用来返回操作数类型的字符串.下面是ECAMScript5.1关于typeof的标准定义: NOTE:上面表格标 ...

  4. 从零开始Go语言-GoLand(编译器)-Windows(平台)

    本文章适合那些想入门Go语言,却又不知道如何搭建自己的第一个HelloWorld的同学. 推荐几个Go语言相关学习网站: C语言中文网: http://c.biancheng.net/golang/ ...

  5. 配置ca服务器和http,mail加密

    一·CA介绍 certificate authority   数字证书授权中心 被通信双方信任的.独立的第三方机构 负责证书颁发.验证.撤销等管理 数字证书 经证书授权中心数字签名的包含公开密钥拥有者 ...

  6. linux下配置vnc-server 和gnome-session

    机器比较老,安装时间也十分久远,所以也不知道实验室系统当时是不是完全安装,最近需要使用vnc登录显示界面,结果问题就来了...没有安装vnc-server. (1)机器系统是rhel6.2的,所以就从 ...

  7. 开源工具abaplint的介绍

    长期以来,SAP提供的标准ABAP开发工具是我们对代码进行检查的唯一方式.这意味着我们只能对ABAP服务器上的ABAP代码做出分析,而离线代码则成为了纯粹的文本,开发者无法对其进行检查.abaplin ...

  8. tomcat 介绍及环境搭建

    一.tomcat介绍 Tomcat 服务器是一个免费的开放源代码的 Web 应用服务器,属于轻量级应用服务器,在中小型 系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试 JSP 程序的首选. ...

  9. ACWing 248. 窗内的星星|扫描线+懒惰标记

    传送门 题目描述 在一个天空中有很多星星(看作平面直角坐标系),已知每颗星星的坐标和亮度(都是整数). 求用宽为W.高为H的矩形窗户(W,H为正整数)能圈住的星星的亮度总和最大是多少.(矩形边界上的星 ...

  10. Java BIO NIO 与 AIO

    回顾 上一章我们介绍了操作系统层面的 IO 模型. 阻塞 IO 模型. 非阻塞 IO 模型. IO 复用模型. 信号驱动 IO 模型(用的不多,知道个概念就行). 异步 IO 模型. 并且介绍了 IO ...