麻烦到不能再麻烦的实现,简单到不能再简单的思路。
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"); } }