关闭

Qt实现简易计算器

1058人阅读 评论(0) 收藏 举报
分类:

麻烦到不能再麻烦的实现,简单到不能再简单的思路。

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");		
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:887213次
    • 积分:16763
    • 等级:
    • 排名:第658名
    • 原创:671篇
    • 转载:422篇
    • 译文:3篇
    • 评论:135条
    总有一天你将破蛹而出,成长得比人们期待的还要美丽。
    但这个过程会很痛,会很辛苦,有时候还会觉得灰心。
    面对着汹涌而来的现实,觉得自己渺小无力。
    但这,也是生命的一部分。做好现在你能做的,然后,一切都会好的。
    我们都将孤独地长大,不要害怕。

    —— 寂地《踮脚张望的时光》
    博客专栏
    最新评论