#include "widget.h"
#include "ui_widget.h"
#include "string"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connect(ui->zero,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->one,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->two,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->three,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->four,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->five,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->six,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->seven,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->eight,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->nine,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->add,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->subs,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->multiply,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->divide,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->equals,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
connect(ui->clear,SIGNAL(clicked()),this,SLOT(on_all_clicked()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_all_clicked()
{
QString str = ui->oper1->text();
QString str2 = ui->oper2->text();
QPushButton *btn=qobject_cast<QPushButton*>(sender());
static bool op1Check= false;
static int oper;
if(op1Check==false){//第一个数没有输入完成
if(btn->objectName()=="zero"){
str=str.append("0");
}else if(btn->objectName()=="one"){
str=str.append("1");
}else if(btn->objectName()=="two"){
str=str.append("2");
}else if(btn->objectName()=="three"){
str=str.append("3");
}else if(btn->objectName()=="four"){
str=str.append("4");
}else if(btn->objectName()=="five"){
str=str.append("5");
}else if(btn->objectName()=="six"){
str=str.append("6");
}else if(btn->objectName()=="seven"){
str=str.append("7");
}else if(btn->objectName()=="eight"){
str=str.append("8");
}else if(btn->objectName()=="nine"){
str=str.append("9");
}
ui->oper1->setText(str);//追加到显示框
if(btn->objectName()=="add"){//输入 *** 作符,意味着第一个数输入完成
ui->operation->setText("+");
op1Check=true;
oper=0;
}else if(btn->objectName()=="subs"){
ui->operation->setText("-");
op1Check=true;
oper=1;
}else if(btn->objectName()=="multiply"){
ui->operation->setText("*");
op1Check=true;
oper=2;
}else if(btn->objectName()=="divide"){
ui->operation->setText("/");
op1Check=true;
oper=3;
}
}
if(op1Check==true){//如果第一个数输入完成才输入第二个数
if(btn->objectName()=="zero"){
str2=str2.append("0");
}else if(btn->objectName()=="one"){
str2=str2.append("1");
}else if(btn->objectName()=="two"){
str2=str2.append("2");
}else if(btn->objectName()=="three"){
str2=str2.append("3");
}else if(btn->objectName()=="four"){
str2=str2.append("4");
}else if(btn->objectName()=="five"){
str2=str2.append("5");
}else if(btn->objectName()=="six"){
str2=str2.append("6");
}else if(btn->objectName()=="seven"){
str2=str2.append("7");
}else if(btn->objectName()=="eight"){
str2=str2.append("8");
}else if(btn->objectName()=="nine"){
str2=str2.append("9");
}
ui->oper2->setText(str2);
}
if(btn->objectName()=="equals"){//输入等于号之后的 *** 作
QString strRes;
int num1 = str.toInt() ;
int num2 = str2.toInt();
int res;
QString op = ui->operation->text();
if(oper==0){
res = num1+num2;
}else if(oper==1){
res = num1-num2;
}else if(oper==2){
res = num1*num2;
}else if(oper==3){
res = num1/num2;
}
strRes=strRes.setNum(res);
strRes=strRes.prepend("=");
strRes=strRes.prepend(str2);//追加 *** 作数和 *** 作符成为表达式显示
if(oper==0){
strRes=strRes.prepend("+");
}else if(oper==1){
strRes=strRes.prepend("-");
}else if(oper==2){
strRes=strRes.prepend("*");
}else if(oper==3){
strRes=strRes.prepend("/");
}
strRes=strRes.prepend(str);
ui->result->setText(strRes);
op1Check=false;
ui->oper1->setText("");
ui->oper2->setText("");
ui->operation->setText("");
}
if(btn->objectName()=="clear"){//清除 *** 作
op1Check=false;
ui->oper1->setText("");
ui->oper2->setText("");
ui->operation->setText("");
ui->result->setText("");
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)