#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Init();
}
Widget::~Widget()
{
delete ui;
}
void Widget::Init()
{
num = 0;
total = 0;
flag = 0;
}
void Widget::on_Btn0_clicked()
{
a += "0";
ui->Line1->setText(a);
}
void Widget::on_Btn1_clicked()
{
a += "1";
ui->Line1->setText(a);
}
void Widget::on_Btn2_clicked()
{
a += "2";
ui->Line1->setText(a);
}
void Widget::on_Btn3_clicked()
{
a += "3";
ui->Line1->setText(a);
}
void Widget::on_Btn4_clicked()
{
a += "4";
ui->Line1->setText(a);
}
void Widget::on_Btn5_clicked()
{
a += "5";
ui->Line1->setText(a);
}
void Widget::on_Btn6_clicked()
{
a += "6";
ui->Line1->setText(a);
}
void Widget::on_Btn7_clicked()
{
a += "7";
ui->Line1->setText(a);
}
void Widget::on_Btn8_clicked()
{
a += "8";
ui->Line1->setText(a);
}
void Widget::on_Btn9_clicked()
{
a += "9";
ui->Line1->setText(a);
}
void Widget::on_Btn11_clicked()
{
b.insert(num,a);
a.clear();
b.insert(++num,"+");
++num;
flag = 0;
}
void Widget::on_Btn_equal_clicked()
{
b.insert(num,a);
double t = 0;
int j = 0;
for(j = 0;j < b.count()-1;j++)
{
if(0 == j)
{
total = QString(b[j]).toDouble();
}
if("+" == b[j])
{
t = QString(b[j+1]).toDouble();
total += t;
}
if("-" == b[j])
{
t = QString(b[j+1]).toDouble();
total -= t;
}
if("*" == b[j])
{
t = QString(b[j+1]).toDouble();
total *= t;
}
if("/" == b[j])
{
t = QString(b[j+1]).toDouble();
total /= t;
}
}
ui->Line1->setText(QString::number(total));
// total = 0;
a = QString::number(total);
b.clear();
num = 0;
flag = 0;
}
void Widget::on_Btn_clear_clicked()
{
ui->Line1->setText(" ");
a.clear();
b.clear();
flag = 0;
}
void Widget::on_Btn22_clicked()
{
b.insert(num,a);
a.clear();
b.insert(++num,"-");
++num;
flag = 0;
}
void Widget::on_Btn33_clicked()
{
b.insert(num,a);
a.clear();
b.insert(++num,"*");
++num;
flag = 0;
}
void Widget::on_Btn44_clicked()
{
b.insert(num,a);
a.clear();
b.insert(++num,"/");
++num;
flag = 0;
}
void Widget::on_Btn_point_clicked()
{
if(0 == flag)
{
a += ".";
ui->Line1->setText(a);
}
}
Qt编写的计算器
最新推荐文章于 2024-04-07 19:18:01 发布