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 ...
随机推荐
- ObserverPattern(观察者模式)-----Java/.Net
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern).比如,当一个对象被修改时,则会自动通知它的依赖对象.观察者模式属于行为型模式
- EF 学习系列三 数据操作数据加载及EF中执行Sql
1.实体状态 我们通过EF来对数据库进行操作并持久化到数据库,那么EF必然通过EF上下文来维护实体的状态,明确知道每一个状态所对应的操作.也就是说EF通过上下文负责跟踪实体的状态.EF实体状态存在命名 ...
- 利用log4net创建日志文件时过滤日志,这是坑还是?
前言 网上貌似没有太多关于log4net过滤日志的资料,在研究过程中发现一点小问题,这里做下记录,希望对后续有用到的童鞋起到一丢丢帮助作用. log4net日志过滤 由于是在.NET Core中使用, ...
- Python 愤怒的小鸟代码实现:物理引擎pymunk使用
游戏介绍 最近比较忙,周末正好有时间写了python版本的愤怒的小鸟,使用了物理引擎pymunk,图片资源是从github上下载的,实现了一个可玩的简单版本. 功能实现如下: 支持小鸟类型:红色小鸟, ...
- iOS开发常见问题
1. 在 ViewController 中添加子视图时,导航栏遮挡添加的子视图 let bpView = BpView.init(frame: CGRect.init(x: , y: , width: ...
- 曹工说Spring Boot源码(9)-- Spring解析xml文件,到底从中得到了什么(context命名空间上)
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- MySQL快速回顾:高级查询操作
8.1 排序数据 检索出的数据并不是以纯粹的随机顺序显示的.如果不排序,数据一般将以它在底层表中出现的顺序显示.这可以是数据最初添加到表中的顺序.但是,如果数据后来进行过更新或删除,则此顺序将会受到M ...
- MySQL 物理备份工具-xtrabackup
安装 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum -y install perl ...
- 一键部署k8s 、docker 工具集——最新版本
- P2756 飞行员配对方案问题 二分图匹配 匈牙利算法
题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其中1 名是英国飞行员,另1名是外 ...