基于Qt的科学计算器
本次科学计算的设计,我仿照win 10系统自带的科学计算器,完成相应的功能,能实现加减乘除,乘方开方,三角函数,二进制转换等功能。
在Qt4,Qt5版本下都可以运行。
效果图如下:
设计思路:
代码如下:
/* Mainwindow.h文件*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLineEdit>
#include <QSystemTrayIcon>
#include <QPushButton>
#include <QGridLayout>
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
bool waittingForOperand;
bool addOperator; //加法操作符
bool minOperator; //减法操作符
bool mulOperator; //乘法操作符
bool divOperator; //除法操作符
bool XYOperator; //X的Y次方操作符
bool modOperator; //余数操作符
bool operatorFlag; //操作符标志
bool calculate(double operand, const QString &operatorAct);
bool calculate(double operand);
void abortOperation();
double value;
double result;
private slots:
void buttonDigitalClicked(); //数字按键
void buttonNegativeClicked(); //正负号切换
void buttonSinClicked(); //三角函数sin
void buttonCosClicked(); //三角函数cos
void buttonTanClicked(); //三角函数tan
void buttonBINClicked(); //十进制转二进制
void buttonAbsClicked(); //绝对值
void buttonSqrtClicked(); //开方
void buttonPowClicked(); //平方
void buttonXClicked(); //倒数
void buttonXYClicked(); //X的Y次方
void buttonModClicked(); //取余
void buttonEqualClicked(); //等
void buttonAddClicked(); //加法
void buttonMinClicked(); //减法
void buttonMulClicked(); //乘法
void buttonDivClicked(); //除法
void buttonPointClicked(); //小数点
void buttonPIClicked(); //PI
void buttonDelClicked(); //回退
void buttonClearClicked(); //清空
};
#endif //MAINWINDOW_H
//Mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cmath"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//窗体属性设置
this->setWindowTitle("calculator"); //设置窗体标题
this->setMaximumSize(421,486); //窗体最大尺寸
this->setMinimumSize(421,486); //窗体最小尺寸
this->setWindowIcon(QIcon(":/timg.jpg")); //窗体图标
//参数的初始化
waittingForOperand = true; //等待操作数
result = 0.0;
value = 0.0;
operatorFlag = false;
modOperator = false;
XYOperator = false;
addOperator = false;
minOperator = false;
mulOperator = false;
divOperator = false;
//数字按键绑定
connect(ui->buttonDigital0, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital1, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital2, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital3, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital4, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital5, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital6, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital7, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital8, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonDigital9, SIGNAL(clicked()), this, SLOT(buttonDigitalClicked()));
connect(ui->buttonNega,SIGNAL(clicked()),this,SLOT(buttonNegativeClicked())); //正负符号切换
connect(ui->buttonSin,SIGNAL(clicked()), this, SLOT(buttonSinClicked())); //三角函数sin
connect(ui->buttonCos,SIGNAL(clicked()), this, SLOT(buttonCosClicked())); //三角函数cos
connect(ui->buttonTan,SIGNAL(clicked()), this, SLOT(buttonTanClicked())); //三角函数tan
connect(ui->buttonBIN, SIGNAL(clicked()), this, SLOT(buttonBINClicked()));//十进制转二进制
connect(ui->buttonAbs,SIGNAL(clicked()), this, SLOT(buttonAbsClicked())); //绝对值
connect(ui->buttonSqrt, SIGNAL(clicked()), this, SLOT(buttonSqrtClicked()));//开方
connect(ui->buttonPow, SIGNAL(clicked()), this, SLOT(buttonPowClicked()));//平方
connect(ui->buttonX, SIGNAL(clicked()), this, SLOT(buttonXClicked()));//倒数
connect(ui->buttonXY, SIGNAL(clicked()), this, SLOT(buttonXYClicked()));//x的y次方
connect(ui->buttonMod, SIGNAL(clicked()), this, SLOT(buttonModClicked()));//取余
connect(ui->buttonEqual, SIGNAL(clicked()), this, SLOT(buttonEqualClicked()));//等于
//加减乘除
connect(ui->buttonAdd, SIGNAL(clicked()), this, SLOT(buttonAddClicked()));
connect(ui->buttonMin, SIGNAL(clicked()), this, SLOT(buttonMinClicked()));
connect(ui->buttonMul, SIGNAL(clicked()), this, SLOT(buttonMulClicked()));
connect(ui->buttonDiv, SIGNAL(clicked()), this, SLOT(buttonDivClicked()));
connect(ui->buttonPoint, SIGNAL(clicked()), this, SLOT(buttonPointClicked()));//小数点
connect(ui->buttonPI, SIGNAL(clicked()), this, SLOT(buttonPIClicked()));//pai
connect(ui->buttonDel, SIGNAL(clicked()), this, SLOT(buttonDelClicked()));//回退
connect(ui->buttonClear, SIGNAL(clicked()), this, SLOT(buttonClearClicked()));//清空
ui->display->setAlignment(Qt::AlignRight); //靠右显示
ui->display->setReadOnly(true); //只读模式
ui-> display->setMinimumHeight(50);
ui->display->setText("0");//初始化为0
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::buttonDigitalClicked()//数字按键输入数字
{
QPushButton *buttonClicked = qobject_cast<QPushButton *>(sender()); //判断是那个键值被摁下
int buttonValue = buttonClicked->text().toInt(); //将键值转化为整型赋值给value
if(waittingForOperand)
{
ui->display->clear();
waittingForOperand = false;
}
ui->display->setText(ui->display->text()+QString::number(buttonValue)); //计算器显示之前的值加上现在的值
}
void MainWindow::buttonNegativeClicked()//负号
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
text = -text;
ui->display->setText(QString::number(text));
waittingForOperand = true;
}
}
void MainWindow::buttonSinClicked()//三角函数sin
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
text = std::sin(text*3.141592654/180);
ui->display->setText(QString::number(text));
}
waittingForOperand = true;
}
void MainWindow::buttonCosClicked()//三角函数cos
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
text = std::cos(text*3.141592654/180);
ui->display->setText(QString::number(text));
}
waittingForOperand = true;
}
void MainWindow::buttonTanClicked()//三角函数tan
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
text = std::tan(text*3.141592654/180);
ui->display->setText(QString::number(text));
}
waittingForOperand = true;
}
void MainWindow::buttonBINClicked()//十进制转二进制
{
if(operatorFlag)
return;
else
{
int text = ui->display->text().toInt();
QString str="";
while (text!=0) {
str = QString::number(text % 2) + str;
text = text/ 2;
}
ui->display->setText(str);
}
waittingForOperand = true;
}
void MainWindow::buttonAbsClicked()//绝对值
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
text = std::fabs(text);
ui->display->setText(QString::number(text));
}
waittingForOperand = true;
}
void MainWindow::buttonSqrtClicked()//开方
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
if(text < 0)
abortOperation();
else
{
text = std::sqrt(text);
ui->display->setText(QString::number(text));
}
}
waittingForOperand = true;
}
void MainWindow::buttonPowClicked()//平方
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
text = std::pow(text,2);
ui->display->setText(QString::number(text));
}
waittingForOperand = true;
}
void MainWindow::buttonXClicked()//倒数
{
if(operatorFlag)
return;
else
{
double text = ui->display->text().toDouble();
if(text == 0.0)
abortOperation();
else
{
text = 1/text;
ui->display->setText(QString::number(text));
waittingForOperand = true;
}
}
}
void MainWindow::buttonXYClicked()//X的Y次方
{
if(!operatorFlag)
{
result = ui->display->text().toDouble();
operatorFlag = true;
waittingForOperand = true;
}
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
}
XYOperator = true;
}
void MainWindow::buttonModClicked()//取余
{
if(!operatorFlag)
{
result = ui->display->text().toDouble();
operatorFlag = true;
waittingForOperand = true;
}
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
}
modOperator = true;
}
void MainWindow::buttonEqualClicked()//等于
{
if(!operatorFlag)
return;
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
waittingForOperand = true;
operatorFlag = false;
}
}
void MainWindow::buttonAddClicked()//加
{
if(!operatorFlag)
{
result = ui->display->text().toDouble();
operatorFlag = true;
waittingForOperand = true;
}
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
}
addOperator = true;
}
void MainWindow::buttonMinClicked()//减
{
if(!operatorFlag)
{
result = ui->display->text().toDouble();
operatorFlag = true;
waittingForOperand = true;
}
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
}
minOperator = true;
}
void MainWindow::buttonMulClicked()//乘
{
if(!operatorFlag)
{
result = ui->display->text().toDouble();
operatorFlag = true;
waittingForOperand = true;
}
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
}
mulOperator = true;
}
void MainWindow::buttonDivClicked()//除
{
if(!operatorFlag)
{
result = ui->display->text().toDouble();
operatorFlag = true;
waittingForOperand = true;
}
else
{
value = ui->display->text().toDouble();
if(calculate(value)==false)
{
abortOperation();
}
}
divOperator = true;
}
void MainWindow::buttonPointClicked()//小数点
{
//首先判断当前显示内容是否已经包含“.”符号
if(ui->display->text().contains('.'))
return;
else
ui->display->setText(ui->display->text()+tr("."));
}
void MainWindow::buttonPIClicked()//pai值
{
//判断是否是进行输入状态
if(waittingForOperand)
ui->display->setText(QString::number(3.141592654));
else
return;
}
void MainWindow::buttonDelClicked()//回退
{
//判断当前是否属于数值输入模式
if(waittingForOperand)
return;
QString text = ui->display->text();
text.chop(1);
//判断删除后数据是否为空
if(text.isEmpty())
{
text = "0";
waittingForOperand = true;
}
ui->display->setText(text);
}
void MainWindow::buttonClearClicked()//归零
{
waittingForOperand = true; //等待操作数
result = 0.0;
value = 0.0;
operatorFlag = false;
modOperator = false;
XYOperator = false;
addOperator = false;
minOperator = false;
mulOperator = false;
divOperator = false;
ui->display->setText("0");
}
bool MainWindow::calculate(double operand)//运算结果
{
if(addOperator)
{
result += operand;
addOperator = false;
}
else if(minOperator)
{
result -= operand;
minOperator = false;
}
else if(mulOperator)
{
result *= operand;
mulOperator = false;
}
else if(divOperator)
{
if(operand - 0 < 10e-6 )
return false;
result /= operand;
divOperator = false;
}
else if(modOperator)
{
if(operand - 0 < 10e-6 )
return false;
result = (int)result%(int)operand;
modOperator = false;
}
else if(XYOperator)
{
result = pow(result,operand);
XYOperator = false;
}
waittingForOperand = true;
ui->display->setText(QString::number(result));
return true;
}
void MainWindow::abortOperation()//出现错误
{
buttonClearClicked();
ui->display->setText(tr("ERROR!!!"));
}
本次设计的科学计算器,收获很大,通过运算符标志位选择进行哪种种运算。
源代码:https://download.csdn.net/download/qq_44145934/13851739