1. 程式人生 > >實現簡單的計算器(計算功能模塊實現)

實現簡單的計算器(計算功能模塊實現)

fine clas include span ret exec class clu define

 1 #ifndef EXECUTE_H
 2 #define EXECUTE_H
 3 #include <QString>
 4 
 5 class execute
 6 {
 7 public:
 8     execute();
 9     void setnum1(int num);
10     void setnum2(int num);
11     void setflag(QString flag);
12     QString doexe();
13 private:
14     int num1;
15     int num2;
16 QString flag; 17 }; 18 19 #endif // EXECUTE_H
 1 #include "execute.h"
 2 
 3 execute::execute()
 4 {
 5     num1=0;
 6     num2=0;
 7 }
 8 
 9 void execute::setnum1(int num){
10     this->num1 = num;
11 }
12 
13 void execute::setnum2(int num){
14     this->num2 = num;
15 } 16 17 void execute::setflag(QString flag){ 18 this->flag = flag; 19 } 20 21 QString execute::doexe(){ 22 int result = 0; 23 if(this->flag == "+"){ 24 result = this->num1+this->num2; 25 }else if (this->flag == "-") { 26 result = this->num1-this
->num2; 27 }else if (this->flag == "*") { 28 result = this->num1*this->num2; 29 }else if (this->flag == "/") { 30 if(this->num2 == 0){ 31 return "ERROR"; 32 }else { 33 result = this->num1*this->num2; 34 } 35 }else { 36 result = this->num1; 37 } 38 return QString::number(result); 39 }

實現簡單的計算器(計算功能模塊實現)