当前位置:网站首页>C1-qt idea of realizing simple calculator 2021.10.15

C1-qt idea of realizing simple calculator 2021.10.15

2022-06-22 02:44:00 Morning and evening rain

Qt Implement a simple calculator

1. The goal is

Realization 1 2 3 4 A calculator for the sum of four numbers , Provide ideas and help for the realization of a complete calculator .

2. The code is as follows

.h Two variables are defined in the file

  	QString str;
   	QStringlist  list;

.cpp Initializes two variables in the constructor of

 	str.clear();
    list.clear();

.cpp The go to slot function of each button implemented in is as follows

void calculator::on_btn_1_clicked()// Button “1”
{
    
    str+="1";
    ui->edit_Input->setText(str);

}
void calculator::on_btn_2_clicked()// Button “2”
{
    
    str+="2";
    ui->edit_Input->setText(str);
}
void calculator::on_btn_3_clicked()// Button “3”
{
    
    str+="3";
    ui->edit_Input->setText(str);

}
void calculator::on_btn_4_clicked()// Button “4”
{
    
    str+="4";
    ui->edit_Input->setText(str);
}
void calculator::on_btn_plus_clicked()// Button “+”
{
    
    list.insert(0,str);
    str.clear();
    list.insert(1,"+");
    ui->edit_Input->setText(str);
}
void calculator::on_btn_result_clicked()// Button “=”
{
    
    if(list.count()==2){
    
        list.insert(2,str);
        if(list.at(1)=="+"){
    
            int v3 = QString(list.at(0)).toInt()+QString(list.at(2)).toInt();
            ui->edit_Input->setText(QString::number(v3));
        }
    }
    else
    {
    
        str.clear();
        list.clear();
        ui->edit_Input->setText(str);
    }
}

3. effect

 Insert picture description here

4. summary

1. Using string variables is easier than most people think of first .
2. On this basis , Encapsulate the concrete implementation of the slot function into a method ;
3. We can consider perfecting addition, subtraction, multiplication and division ;

原网站

版权声明
本文为[Morning and evening rain]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211658130333.html