这里我是用的qt图形界面直接创建按键转接槽函数,大家可以也可以使用代码创建界面,有时间的可以试试。
这是.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_close_clicked();
void on_Button_add_clicked();
void on_Button_sub_clicked();
void on_Button_mul_clicked();
void on_Button_dev_clicked();
void on_Button_prrcent_clicked();
void on_Button_1_clicked();
void on_Button_2_clicked();
void on_Button_3_clicked();
void on_Button_4_clicked();
void on_Button_5_clicked();
void on_Button_6_clicked();
void on_Button_7_clicked();
void on_Button_8_clicked();
void on_Button_9_clicked();
void on_Button_0_clicked();
void on_Button_d_clicked();
void on_Button_equal_clicked();
void on_Button_clear_clicked();
void on_Button_delete_clicked();
private:
Ui::Widget *ui;
QString op1;
QString op2;
int flag;
};
#endif // WIDGET_H
.cpp文件文章来源:https://www.toymoban.com/news/detail-506395.html
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
op1 = "";
op2 = "";
flag = 0;//设置标志位简化后面的操作
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_close_clicked()//退出计算机
{
this->close();
}
void Widget::on_Button_add_clicked()
{
flag = 1;
}
void Widget::on_Button_sub_clicked()
{
flag = 2;
}
void Widget::on_Button_mul_clicked()
{
flag = 3;
}
void Widget::on_Button_dev_clicked()
{
flag = 4;
}
void Widget::on_Button_prrcent_clicked()
{
flag = 5;
}
void Widget::on_Button_1_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "1";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "1";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_2_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "2";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "2";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_3_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "3";
ui->textEdit->setText(op1);
}
else
{
op1 = "";
op2 = op2 + "3";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_4_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "4";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "4";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_5_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "5";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "5";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_6_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "6";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "6";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_7_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "7";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "7";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_8_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "8";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "8";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_9_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "9";
ui->textEdit->setText(op1);
}
else
{
op2 = op2 + "9";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_0_clicked()
{
if(flag == 0)
{
op1 = "";
op1 = op1 + "0";
ui->textEdit->setText(op1);
}
else
{
op1 = "";
op2 = op2 + "0";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_d_clicked()//小数点,此处逻辑上没有完全完善,大家可以自行完善
{
if(flag == 0)
{
if(op1 == "")
return ;
op1 = op1 + ".";
ui->textEdit->setText(op1);
}
else
{
if(op2 == "")
return ;
op2 = op2 + ".";
ui->textEdit->setText(op2);
}
}
void Widget::on_Button_equal_clicked()// =
{
float result;
float number1;
float number2;
number1 = op1.toFloat();
number2 = op2.toFloat();
if(flag == 1)
{
result = number1 + number2;
}
if(flag == 2)
{
result = number1 - number2;
}
if(flag == 3)
{
result = number1 * number2;
}
if(flag == 4)
{
result = number1 / number2;
}
if(flag == 5)
{
result = number1 / number2; // % 百分之多少还没实现,大家可以根据直接逻辑去实现,我就偷懒了
}
QString str;
str = str.number(result);
ui->textEdit->setText(str);
flag = 0;
op1 = QString::number(result); //数字转换成字符串后继续运行
op2 = "";
}
void Widget::on_Button_clear_clicked()//清空
{
ui->textEdit->setText("");
flag = 0;
op1 = "";
op2 = "";
}
void Widget::on_Button_delete_clicked()//删除一位
{
if(flag == 0)
{
op1.chop(1);//剪切掉最后一位
ui->textEdit->setText(op1);
}
else
{
op2.chop(1);
ui->textEdit->setText(op2);
}
}
有些许逻辑上没有实现,大家可根据自己需要完善文章来源地址https://www.toymoban.com/news/detail-506395.html
到了这里,关于Qt实现简易计算机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!