以下内容均是个人理解,不保证都是正确的,如有错欢迎指正
欢迎学习交流:815030927@qq.com
QT实现简易计算器
1、功能简介
1)实现正负整数和小数的加减乘除运算
2)能够判断输入的式子格式是否正确(以正负号开头也为正确格式)
2、原理
1)式子部分
①显示:
将按钮对应的字符追加到字符串中,并用QLineEdit对象将式子显示出来
②判断格式:
不能出现的情况(“——”后为解决方案)
a. 以*/开头或结尾——检测第一个和最后一个符号即可
b. ±/相连——找出所有±/的位置,判断是否相差1
c. 出现1234567890.±/以外的符号——每个符号与“1234567890.±/”进行比较
2)计算部分
将式子以+、-、*、/的顺序将式子分割成小式子计算后将结果返回,一层一层传递,最后返回最终结果
基本的流程
2、步骤
注:熟悉QT可直接看代码
1)界面
使用1个QLineEdit对象和16个QPushButton对象,编辑按钮上的文本。并用布局管理器进行布局。
2)代码
calculator.h中的代码:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include <QMainWindow>
#include <QPushButton>
#include <QByteArray>
namespace Ui {
class Calculator;
}
class Calculator : public QMainWindow
{
Q_OBJECT
public:
explicit Calculator(QWidget *parent = nullptr);
~Calculator();
private slots:
//按钮对应的槽函数
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_4_clicked();
void on_pushButton_5_clicked();
void on_pushButton_6_clicked();
void on_pushButton_3_clicked();
void on_pushButton_7_clicked();
void on_pushButton_8_clicked();
void on_pushButton_11_clicked();
void on_pushButton_15_clicked();
void on_pushButton_14_clicked();
void on_pushButton_16_clicked();
void on_pushButton_10_clicked();
void on_pushButton_13_clicked();
void on_pushButton_12_clicked();
void on_pushButton_9_clicked();
private:
Ui::Calculator *ui;
QString formula;//记录整个式子
//记录式子中加减号的个数
int plusSign, minusSign, multiplySign, divineSign;
double result;
//定义QStringList对象,相当于Qstring数组
QStringList plusList, minusList, multiplyList;
//将按钮对应的字符加入formula的追加函数
void addToFormula(QPushButton *ClickedBt);
//检测式子是否正确
bool detector(QString text);
//式子计算函数
void calculate();
};
#endif // CALCULATOR_H
calculator.cpp中的代码:
#include "calculator.h"
#include "ui_calculator.h"
#include <QDebug>
#include <QStringList>
Calculator::Calculator(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Calculator)
{
ui->setupUi(this);
}
Calculator::~Calculator()
{
delete ui;
}
void Calculator::addToFormula(QPushButton *ClickedBt)
{
formula.append(ClickedBt->text());
ui->lineEdit->setText(formula);
}
void Calculator::on_pushButton_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_2_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_4_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_5_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_6_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_3_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_7_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_8_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_11_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_15_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_14_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_16_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_10_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_13_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
void Calculator::on_pushButton_12_clicked()
{
QPushButton *ClickedBt = qobject_cast<QPushButton*>(sender());
addToFormula(ClickedBt);
}
//等号按钮对应的槽函数
void Calculator::on_pushButton_9_clicked()
{
//判断式子是否正确,正确则进行计算
if(detector(ui->lineEdit->text()))
calculate();
return;
}
//计算函数
void Calculator::calculate(void)
{
//声明临时变量
double tem1=0, tem2=0, tem3=0;
//qDebug()<<ui->lineEdit->text();
//计算加号个数
plusSign = ui->lineEdit->text().count("+");
//拆分式子
for(int i=0; i<plusSign+1; i++)
{
QString plusstr = ui->lineEdit->text().section('+',i,i);
//计算plusstr中减号的个数
minusSign = plusstr.count("-");
for(int j=0; j<minusSign+1; j++)
{
//拆分式子
QString minusstr = plusstr.section('-',j,j);
//计算minusstr中乘号的个数
multiplySign = minusstr.count("*");
for(int n=0; n<multiplySign+1; n++)
{
//拆分式子
QString multiplystr = minusstr.section('*',n,n);
//计算multiplystr中除号的个数
divineSign = multiplystr.count("/");
for(int m=0; m<divineSign+1; m++)
{
QString divinestr = multiplystr.section('/', m, m);
//进行相除
if(m==0)
tem1 = divinestr.toDouble();
else
tem1 = tem1/divinestr.toDouble();
}
//将result放入multiplyList
multiplyList.append(QString::number(tem1));
//进行相乘
if(n==0)
tem2 = multiplyList.at(n).toDouble();
else
tem2 *= multiplyList.at(n).toDouble();
}
//清空
multiplyList.clear();
minusList.append(QString::number(tem2));
//进行相减
if(j==0)
tem3 = minusList.at(j).toDouble();
else
tem3 -= minusList.at(j).toDouble();
}
//清空
minusList.clear();
plusList.append(QString::number(tem3));
//进行相加
if(i==0)
result = plusList.at(i).toDouble();
else
result += plusList.at(i).toDouble();
}
//清空
plusList.clear();
ui->lineEdit->setText(QString::number(result));
formula.clear();
}
bool Calculator::detector(QString text)
{
//不能出现的情况
//①以*/开头或结尾,检测第一个和最后一个符号即可
if(text.at(0)=="*" || text.at(0)=="/" || text.endsWith("*") || text.endsWith("/")){
//提示错误
ui->lineEdit->setText(QString("请重新输入"));
//清空
formula.clear();
return false;
}
//②+-*/相连,找出所有+-*/的位置,判断是否相差1
for(int i=0; i<text.count(); i++)
if(QString("+-*/").contains(text.at(i)) && QString("+-*/").contains(text.at(i+1))){
//提示错误
ui->lineEdit->setText(QString("请重新输入"));
formula.clear();
return false;
}
//③出现1234567890.+-*/以外的符号,每个符号与“1234567890.+-*/”进行比较
for(int i=0; i<text.count(); i++)
if(!QString("1234567890.+-*/").contains(text.at(i))){
//提示错误
ui->lineEdit->setText(QString("请重新输入"));
formula.clear();
return false;
}
return true;
}
3、error:
在计算的代码中,本在计算过程中想使用同一个变量result记录过程中的临时结果和最终结果,但出错。以下是错误的源代码,注释中已记录错误的原因
void Calculator::on_pushButton_9_clicked()
{
qDebug()<<ui->lineEdit->text();
//计算加号个数
plusSign = ui->lineEdit->text().count("+");
//拆分式子
for(int i=0; i<plusSign+1; i++)
{
QString plusstr = ui->lineEdit->text().section('+',i,i);
//计算plusstr中减号的个数
minusSign = plusstr.count("-");
for(int j=0; j<minusSign+1; j++)
{
//拆分式子
QString minusstr = plusstr.section('-',j,j);
//计算minusstr中乘号的个数
multiplySign = minusstr.count("*");
for(int n=0; n<multiplySign+1; n++)
{
//拆分式子
QString multiplystr = minusstr.section('*',n,n);
//计算multiplystr中除号的个数
divineSign = multiplystr.count("/");
for(int m=0; m<divineSign+1; m++)
{
QString divinestr = multiplystr.section('/', m, m);
//进行相除
if(m==0)
result = divinestr.toDouble();
else
result = result/divinestr.toDouble();
}
//将result放入multiplyList
multiplyList.append(QString::number(result));
//进行相乘
if(n==0)
result = multiplyList.at(n).toDouble();
else//在m!=0,n!=0时,以下的result不再是m=0时的result
result *= multiplyList.at(n).toDouble();
qDebug()<<result;
}
//清空
qDebug()<<"***********";
qDebug()<<multiplyList;
multiplyList.clear();
minusList.append(QString::number(result));
//进行相减
if(j==0)
result = minusList.at(j).toDouble();
else
result -= minusList.at(j).toDouble();
}
//清空
qDebug()<<"###############";
qDebug()<<minusList;
minusList.clear();
plusList.append(QString::number(result));
//进行相加
if(i==0)
result = plusList.at(i).toDouble();
else
result += plusList.at(i).toDouble();
}
//清空
qDebug()<<"&&&&&&&&&";
qDebug()<<plusList;
plusList.clear();
ui->lineEdit->setText(QString::number(result));
}