Qt实现简易计算器
麻烦到不能再麻烦的实现,简单到不能再简单的思路。
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实现简易计算器的更多相关文章
- Qt、C++ 简易计算器
Qt.C++实现简易计算器: 以下内容是我实现这个简易计算器整个过程,其中包括我对如何实现这个功能的思考.中途遇到的问题.走过的弯路 整个实现从易到难,计算器功能从简单到复杂,最开始设计的整个实现步骤 ...
- python + PyQt5 实现 简易计算器
忽然想起之前一直想写个简单的计算器,今天就写了一下,界面有些简陋,但是基本功能实现没有问题 以下是源码: # --*-- coding:utf-8 --*-- import sys from PyQt ...
- 【PyQt5-Qt Designer】简易的数字键盘输入+简易计算器
参考如下键盘格式写了一个键盘输入,目前还不能进行运算,后期完善... 效果如下: 完整代码: from PyQt5.QtWidgets import (QApplication,QWidget,QPu ...
- PyQt5 简易计算器
剩下计算函数(self.calculator)未实现,有兴趣的朋友可以实现它 [知识点] 1.利用循环添加按钮部件,及给每个按钮设置信号/槽 2.给按钮设置固定大小:button.setFixedSi ...
- 自制c#简易计算器
这是一个课堂作业,我觉得作为一个简易的计算器不需要态度复杂的东西,可能还有一些bug,有空再慢慢加强. using System;using System.Collections.Generic;us ...
- 剖析简易计算器带你入门微信小程序开发
写在前面,但是重点在后面 这是教程,也不是教程. 可以先看Demo的操作动图,看看是个什么玩意儿,GitHub地址(https://github.com/dunizb/wxapp-sCalc) 自从微 ...
- PHP学习笔记02——简易计算器
<!DOCTYPE html> <html> <head> <title>PHP简易计算器</title> </head> &l ...
- JavaScript之简易计算器
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...
- 菜鸟学习Struts——简易计算器
这是学习Struts的一个简单的例子文件结构如下: 1.配置Struts环境 2.新建input.jsp,success.jsp,error.jsp input.jsp代码如下: <%@ pag ...
随机推荐
- 「Luogu P4987」回文项链 解题报告
题面 求环中的长度为k(k为奇数)且回文中心不同的回文串个数 思路: 刚学manacher算法,就送上一道模板题,此题注重对manacher算法的理解 Manacher,但是不用插入其他符号,因为k是 ...
- 在Winform界面中使用DevExpress的TreeList实现节点过滤查询的两种方式
在我较早的一篇随笔<在DevExpress程序中使用TeeList控件以及节点查询的处理>中,介绍了在树形列表TreeList控件上面,利用SearchControl实现节点的模糊查询过滤 ...
- socket、http、udp、tcp的整理
1.socket简介 游戏开发中最常用的便是socket,socket本质是api,是对tcp/ip的封装.tcp/ip协议族是一个网络通信模型以及一系列网络传输协议,为互联网的基础通信架构. tcp ...
- Spring--2.Spring之IOC--IOC容器的23个实验(1)
实验1.IOC容器创建对象,并为属性赋值 Hello World:(通过各种方式给容器中注册对象(注册会员)) 以前是自己new对象,现在所有对象交给容器创建:给容器中注册组件 以后框架编写流程: ...
- (1)解锁 MongoDB replica set核心姿势
副本集Replica Set是一个术语,定义具有多节点的数据库集群,这些节点具有主从复制(master-slave replication) 且节点之间实现了自动故障转移. 这样的结构通常需要具有奇数 ...
- 关于java php go 中AES加解密秘钥长度问题
今天心血来朝,想用go把php中的一个小功能重写一下,但在解密aes加密的数据时碰到了个坑! php的mcrypt拓展(貌似php7.1版本以上不支持了)提供了aes的加解密: 而且php aes 的 ...
- Java8 新特性(二)- Stream
Stream 用来处理集合数据的,通过 stream 操作可以实现 SQL 的拥有的大部分查询功能 Java8 API 官方文档 下面借助例子,演示 stream 操作 Java userList 列 ...
- Ambari下安装oozieUI界面无法访问问题
前言: 其他集群管理工具安装oozie和手动编译安装oozie,关于oozie的UI界面无法访问的问题,大致和该方法类似. 找到下面4步骤里的视图显示的目录,有一个ext-2.2软链指向的路径 lrw ...
- 读取Core下的appsettings.json的值的时候中文乱码
这个百度一下一大堆,我就用的这个:然后重新生成一次就好了. 2.有的是更改VS的什么高级保存之类的,我记得之气设置过, 然后就是:这篇文章
- Python思维导图(一)—— 基础
前言 思维导图并不能涵盖所有知识点,只是梳理某个知识点下我们需要重点关注的分支:根据自己的情况可以进行拓展学习 计算机基础 博主认为需要重点掌握的有 编译型语言和解释型语言的区别?分别有什么编程语言? ...