Qt实现简易计算器
2013-12-05 15:08
197 查看
麻烦到不能再麻烦的实现,简单到不能再简单的思路。
calc.h
calc.cpp
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实现简易计算器
- Qt实现QComboBox的鼠标点击信号
- 基于Opencv2.4.11+OpenGL(Qt5.6.0)实现增强现实(二)
- 黑白棋算法简单实现与基于Qt的GUI编程的综合应用
- Qt 透明窗体实现
- iOS简易计算器的实现
- Qt and C++ Reflection,利用Qt简化C++的反射实现
- Qt中QTableView中加入Check列实现
- qt button以及label实现不规则图形(五种方法:使用QSS,设置Mask图片,自己画)
- Qt一些源码实现细节及内部实现简析
- 【Qt编程】基于Qt的词典开发系列<八>--用户登录及API调用的实现
- 函数实现qt的属性的修改
- Qt数据加密类QCryptographicHash实现32位或16位加密
- Qt qtablewidget 实现筛选功能,显示符合条件的整行信息
- 基于Qt的多窗口设计B-窗体切换的实现
- QT实现菜单栏
- Qt之美(一):D指针/私有实现
- 基于qt串口实现十六进制收发
- QT从QObject实现多重继承