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 ...
随机推荐
- Word List 1 part 1
inter- 在....之间 intermediate adj. 中间的 pro- 向前,在前;很多;赞同;亲... proportion n. 比例;部分 prim- 第一,主要的 prime ad ...
- 小小知识点(二十五)5G关键技术——Massive MIMO(大规模天线阵列)和beamforming(波束成形)
转自http://www.elecfans.com/d/949864.html 多输入多输出技术(Multiple-Input Multiple-Output,MIMO)是指在发射端和接收端分别使用多 ...
- 应急响应&&取证
查看日志 eventvwr.exe 中了勒索病毒 1.查看download目录有没有病毒样本,C:\Users\86132\Downloads 2.查看系统开放端口 3.导出systemin ...
- ASP.NET Core 启用跨域请求
本文翻译整理自:https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1 一 .Cross-Orig ...
- 通过httpClient设置代理Ip
背景: 我们有个车管系统,需要定期的去查询车辆的违章,之前一直是调第三方接口去查,后面发现数据不准确(和深圳交警查的对不上),问题比较多.于是想干脆直接从深圳交警上查,那不就不会出问题了吗,但是问题又 ...
- 【转】iOS 7免费设计资源汇总
原文链接:http://mobile.51cto.com/hot-406317.htm#585532-tsina-1-28470-7e393678b940a4d55500bf3feae3d2e9 以下 ...
- MySQL快速回顾:计算字段与函数
9.1 计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式.比如: 如果想要在一个字段中既显示公司名,又显示公式的地址,但这两个信息一般包含在不同的表列中. 城市.州和邮政编码存储在不同的列 ...
- 弹性碰撞 poj 3684
Simon is doing a physics experiment with N identical balls with the same radius of R centimeters. Be ...
- NetCore下的log4
https://www.cnblogs.com/zhangxiaoyong/p/9463791.html 这一篇也不错 .NET常用的日志组件有NLog.Log4net等,.NET CORE下微软也自 ...
- 《C# 爬虫 破境之道》:第一境 爬虫原理 — 第一节:整体思路
在构建本章节内容的时候,笔者也在想一个问题,究竟什么样的采集器框架,才能算得上是一个“全能”的呢?就我自己以往项目经历而言,可以归纳以下几个大的分类: 根据通讯协议:HTTP的.HTTPS的.TCP的 ...