Преглед на файлове

Limpiando codigo. Todavia no entiendo todo

Rafael Arce Nazario преди 9 години
родител
ревизия
44c305727c
променени са 4 файла, в които са добавени 145 реда и са изтрити 181 реда
  1. 78
    97
      frog.cpp
  2. 9
    4
      frog.h
  3. 54
    80
      mainwindow.cpp
  4. 4
    0
      mainwindow.h

+ 78
- 97
frog.cpp Целия файл

7
 #include "mainwindow.h"
7
 #include "mainwindow.h"
8
 #include "ui_mainwindow.h"
8
 #include "ui_mainwindow.h"
9
 
9
 
10
-frog::frog(QWidget *parent) :
11
-    QWidget(parent)
12
-{
13
-    myTimer= new QTimer(this);
14
-    flag = xFlag = false;
10
+
11
+// Constructor for the frog class
12
+
13
+frog::frog(QWidget *parent) : QWidget(parent) {
14
+
15
     Ysapo = 190;
15
     Ysapo = 190;
16
     A = B = C = 0;
16
     A = B = C = 0;
17
+
18
+    myTimer= new QTimer(this);
19
+    flag = xFlag = false;
20
+
21
+    // These are the x positions of the background?
17
     temp = 580;
22
     temp = 580;
18
     temp2 = 0;
23
     temp2 = 0;
24
+
19
     backFlag = false;
25
     backFlag = false;
20
     moved = false;
26
     moved = false;
21
-    textBool = false;
22
-    buttonCounter = 0;
23
-    xnenu = -(rand() %30);
24
-    Xsapo = xnenu;
25
-    initialX = Xsapo;
26
-    xnenu2 = (rand() % 100) + 100;
27
-    xnenu3 = xnenu2 + (rand() % 100) + 200;
27
+    showText = false;
28
+
29
+    computeNenuPositions();
28
 }
30
 }
29
 
31
 
30
-void frog::updatenenu(){
32
+
33
+void frog::computeNenuPositions() {
31
     xnenu = -(rand() % 30);
34
     xnenu = -(rand() % 30);
32
     Xsapo = xnenu;
35
     Xsapo = xnenu;
33
     initialX = Xsapo;
36
     initialX = Xsapo;
43
     temp2 = 0;
46
     temp2 = 0;
44
     backFlag = false;
47
     backFlag = false;
45
     moved = false;
48
     moved = false;
46
-    buttonCounter = 0;
47
-    textBool = false;
49
+    showText = false;
48
     repaint();
50
     repaint();
49
 
51
 
50
     myTimer->stop();
52
     myTimer->stop();
51
     delete myTimer;
53
     delete myTimer;
52
     myTimer = new QTimer;
54
     myTimer = new QTimer;
53
     connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
55
     connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
54
-    xnenu = -(rand() %30);
55
-    Xsapo = xnenu;
56
-    initialX = Xsapo;
57
-    xnenu2 = (rand() % 100) + 100;
58
-    xnenu3 = xnenu2 + (rand() % 100) + 200;
59
 
56
 
57
+    computeNenuPositions();
60
 }
58
 }
61
 
59
 
62
-void frog::paintEvent(QPaintEvent *event){
60
+void frog::paintEvent(QPaintEvent *){
63
     QPainter p(this);
61
     QPainter p(this);
64
-    QString frogger = ":/images/Frogger1.png";
62
+    QString frogger  = ":/images/Frogger1.png";
65
     QString frogger2 = ":/images/Frogger2.png";
63
     QString frogger2 = ":/images/Frogger2.png";
66
     QString frogger3 = ":/images/Frogger3.png";
64
     QString frogger3 = ":/images/Frogger3.png";
67
-    QString leaf = ":/images/Nenufar1.png";
68
-    QString back = ":/images/background.jpg";
65
+    QString leaf     = ":/images/Nenufar1.png";
66
+    QString back     = ":/images/background.jpg";
69
 
67
 
70
-//    Condiciones que permitian un rango de error (no caer en la x exacta de los nenufares)
68
+    //    Condiciones que permitian un rango de error (no caer en la x exacta de los nenufares)
71
 
69
 
72
     if(backFlag == true || (Xsapo <= (xnenu3 + 55) && Xsapo >= (xnenu3 - 55) && Ysapo == 190)
70
     if(backFlag == true || (Xsapo <= (xnenu3 + 55) && Xsapo >= (xnenu3 - 55) && Ysapo == 190)
73
             || (Xsapo > xnenu && Ysapo == 190 && Xsapo < (xnenu2-55))
71
             || (Xsapo > xnenu && Ysapo == 190 && Xsapo < (xnenu2-55))
74
             || (Xsapo > xnenu2 && Ysapo == 190 && Xsapo < (xnenu3-55))
72
             || (Xsapo > xnenu2 && Ysapo == 190 && Xsapo < (xnenu3-55))
75
             || (Xsapo > xnenu3 && Ysapo == 190)){
73
             || (Xsapo > xnenu3 && Ysapo == 190)){
76
 
74
 
77
-//    if(backFlag == true || (Ysapo == 190 && (Xsapo != xnenu && Xsapo != xnenu2))){
78
-        if (inter1Label == NULL){
79
-            delete inter1Label;
80
-        }
81
-        if (inter2Label == NULL){
82
-            delete inter2Label;
83
-        }
75
+        if (inter1Label == NULL)    delete inter1Label;
76
+        if (inter2Label == NULL)    delete inter2Label;
77
+
84
         backFlag = true;
78
         backFlag = true;
85
         myTimer->stop();
79
         myTimer->stop();
86
         delete myTimer;
80
         delete myTimer;
88
         QThread::msleep(5);
82
         QThread::msleep(5);
89
         connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
83
         connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
90
 
84
 
91
-
92
-
93
         p.drawPixmap(temp2,-5,600,400,QPixmap(back));
85
         p.drawPixmap(temp2,-5,600,400,QPixmap(back));
94
         p.drawPixmap(temp,-5,600,400,QPixmap(back));
86
         p.drawPixmap(temp,-5,600,400,QPixmap(back));
95
 
87
 
99
         p.drawPixmap(xnenu3,250,125,60,QPixmap(leaf));
91
         p.drawPixmap(xnenu3,250,125,60,QPixmap(leaf));
100
         p.drawText(xnenu3+45,290,"x2 = " + QString::number(xnenu3));
92
         p.drawText(xnenu3+45,290,"x2 = " + QString::number(xnenu3));
101
 
93
 
102
-
103
-
104
         Xsapo=Xsapo-1;
94
         Xsapo=Xsapo-1;
105
         xnenu=xnenu-1;
95
         xnenu=xnenu-1;
106
         xnenu2=xnenu2-1;
96
         xnenu2=xnenu2-1;
107
         xnenu3=xnenu3-1;
97
         xnenu3=xnenu3-1;
98
+
99
+
100
+        // para que???
108
         temp= temp - 1;
101
         temp= temp - 1;
109
         temp2 = temp2 -1;
102
         temp2 = temp2 -1;
110
 
103
 
111
-
112
         repaint();
104
         repaint();
113
 
105
 
114
-
115
-
116
         if(temp == 0 || xnenu3 == -10 || Xsapo == 0){
106
         if(temp == 0 || xnenu3 == -10 || Xsapo == 0){
117
             backFlag = false;
107
             backFlag = false;
118
             temp = 580;
108
             temp = 580;
119
             temp2 = 0;
109
             temp2 = 0;
120
             moved = true;
110
             moved = true;
121
-            textBool = false;
122
-            updatenenu();
111
+            showText = false;
112
+            computeNenuPositions();
123
         }
113
         }
124
 
114
 
125
     }
115
     }
126
     else{
116
     else{
127
-          QFont font = p.font();
128
-          font.setBold(true);
129
-          p.setFont(font);
130
-
131
-          p.drawPixmap(temp2,-5,600,400,QPixmap(back));
132
-          p.drawPixmap(temp,-5,600,400,QPixmap(back));
133
-          p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
134
-          p.drawPixmap(xnenu2,250,125,60,QPixmap(leaf));
135
-          p.drawPixmap(xnenu3,250,125,60,QPixmap(leaf));
136
-
137
-//          Cambiada la condicion para que sea exacta la caida en el nenufar
138
-//          if(Xsapo < (xnenu2 + 55) && Ysapo == 190 && Xsapo > (xnenu2-55)){
139
-//               textBool = true;
140
-//          }
141
-
142
-          if(Ysapo == 190 && Xsapo == xnenu2){
143
-            textBool = true;
144
-          }
145
-
146
-          if(textBool){
147
-              p.drawText(xnenu2+35,290,"x1 = " + QString::number(xnenu2));
148
-              p.drawText(xnenu3+35,290,"x2 = " + QString::number(xnenu3));
149
-              p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
150
-          }
151
-          else{
152
-              p.drawText(xnenu2+35,290,"x2 = " + QString::number(xnenu2));
153
-          }
117
+        QFont font = p.font();
118
+        font.setBold(true);
119
+        p.setFont(font);
120
+
121
+        p.drawPixmap(temp2,-5,600,400,QPixmap(back));
122
+        p.drawPixmap(temp,-5,600,400,QPixmap(back));
123
+        p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
124
+        p.drawPixmap(xnenu2,250,125,60,QPixmap(leaf));
125
+        p.drawPixmap(xnenu3,250,125,60,QPixmap(leaf));
126
+
127
+        showText = (Ysapo == 190 && Xsapo == xnenu2);
128
+
129
+        if(showText){
130
+            p.drawText(xnenu2+35,290,"x1 = " + QString::number(xnenu2));
131
+            p.drawText(xnenu3+35,290,"x2 = " + QString::number(xnenu3));
132
+            p.drawPixmap(xnenu,250,125,60,QPixmap(leaf));
133
+        }
134
+        else
135
+            p.drawText(xnenu2+35,290,"x2 = " + QString::number(xnenu2));
154
 
136
 
155
     }
137
     }
156
 
138
 
157
-//    Cambiada la condicion para que sea exacta la caida en el nenufar
139
+    //    Cambiada la condicion para que sea exacta la caida en el nenufar
158
     if(Xsapo > (xnenu + 55) && Ysapo == 190 && Xsapo < (xnenu2-55)
140
     if(Xsapo > (xnenu + 55) && Ysapo == 190 && Xsapo < (xnenu2-55)
159
-        || (Xsapo > (xnenu2 + 55) && Ysapo == 190 && Xsapo < (xnenu3-55))
160
-        || (Xsapo > (xnenu3 + 55) && Ysapo == 190)){
141
+            || (Xsapo > (xnenu2 + 55) && Ysapo == 190 && Xsapo < (xnenu3-55))
142
+            || (Xsapo > (xnenu3 + 55) && Ysapo == 190)){
161
 
143
 
162
-//    if(Ysapo == 190 && (Xsapo != xnenu && Xsapo != xnenu2 && Xsapo != xnenu3)){
144
+        //    if(Ysapo == 190 && (Xsapo != xnenu && Xsapo != xnenu2 && Xsapo != xnenu3)){
163
         p.drawPixmap(Xsapo, Ysapo + 30, 100, 80, QPixmap(frogger3));
145
         p.drawPixmap(Xsapo, Ysapo + 30, 100, 80, QPixmap(frogger3));
164
     }
146
     }
165
     else if(Ysapo < 190){
147
     else if(Ysapo < 190){
167
     }else if(Ysapo == 190){
149
     }else if(Ysapo == 190){
168
         qDebug() << "xsapo: " << Xsapo << "xnenu2: " << xnenu2;
150
         qDebug() << "xsapo: " << Xsapo << "xnenu2: " << xnenu2;
169
         p.drawPixmap(Xsapo,Ysapo,100,100,QPixmap(frogger));
151
         p.drawPixmap(Xsapo,Ysapo,100,100,QPixmap(frogger));
170
-        if(!textBool){
152
+        if(!showText){
171
             p.drawText(xnenu+45,290,"x1 = " + QString::number(xnenu+10));
153
             p.drawText(xnenu+45,290,"x1 = " + QString::number(xnenu+10));
172
         }
154
         }
173
     }
155
     }
179
 }
161
 }
180
 
162
 
181
 
163
 
182
-void frog::mySlot(){
164
+void frog::animate(){
183
     float interX1 = 0;
165
     float interX1 = 0;
184
     float interX2 = 0;
166
     float interX2 = 0;
185
-    float temp = 0;
186
     float y = 0;
167
     float y = 0;
187
     float xtemp = 0;
168
     float xtemp = 0;
188
     float xEnd = 0;
169
     float xEnd = 0;
189
 
170
 
190
 
171
 
172
+    // use the functions implemented by the programmer to
173
+    // compute the x-intercept.
191
 
174
 
192
     interX1 = QuadraticMinus(A,B,C);
175
     interX1 = QuadraticMinus(A,B,C);
193
     interX2 = QuadraticPlus(A,B,C);
176
     interX2 = QuadraticPlus(A,B,C);
194
 
177
 
195
-    if(interX1 > interX2){
196
-        temp = interX1;
197
-        interX1 = interX2;
198
-        interX2 = temp;
199
-    }
200
-    if(moved){
178
+    if (interX1 > interX2) std::swap(interX1, interX2);
179
+
180
+
181
+    if (moved) {
201
         moved = false;
182
         moved = false;
202
-        buttonCounter = 1;
203
         initialX = Xsapo;
183
         initialX = Xsapo;
204
     }
184
     }
205
 
185
 
206
     xEnd = interX2 + initialX;
186
     xEnd = interX2 + initialX;
207
-        y = 1;
208
-        xtemp = 1;
209
-        if(!xFlag){
210
-            Ysapo = Ysapo - y;
211
-            Xsapo = Xsapo + xtemp;
212
-        }
213
-        else if(xFlag){
214
-            Ysapo = Ysapo + y;
215
-            Xsapo = Xsapo + xtemp;
216
-        }
187
+    y = 1;
188
+    xtemp = 1;
217
 
189
 
218
-        if(int(Xsapo) == int(xEnd/2)){
219
-            xFlag = true;
220
-        }
190
+    if(!xFlag){
191
+        Ysapo = Ysapo - y;
192
+        Xsapo = Xsapo + xtemp;
193
+    }
194
+    else if(xFlag){
195
+        Ysapo = Ysapo + y;
196
+        Xsapo = Xsapo + xtemp;
197
+    }
198
+
199
+    if(int(Xsapo) == int(xEnd/2)){
200
+        xFlag = true;
201
+    }
221
 
202
 
222
     repaint();
203
     repaint();
223
     if((int(Xsapo) == int(xEnd) && xFlag == true) || Ysapo == 190){
204
     if((int(Xsapo) == int(xEnd) && xFlag == true) || Ysapo == 190){
227
         myTimer->stop();
208
         myTimer->stop();
228
         delete myTimer;
209
         delete myTimer;
229
         myTimer = new QTimer;
210
         myTimer = new QTimer;
230
-        connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
211
+        connect(myTimer, SIGNAL(timeout()), this, SLOT(animate()));
231
 
212
 
232
         initialX = Xsapo;
213
         initialX = Xsapo;
233
         xFlag = false;
214
         xFlag = false;
240
     A = aa;
221
     A = aa;
241
     B = bb;
222
     B = bb;
242
     C = cc;
223
     C = cc;
243
-    buttonCounter++;
224
+//    buttonCounter++;
244
 
225
 
245
     connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
226
     connect(myTimer, SIGNAL(timeout()), this, SLOT(mySlot()));
246
     myTimer->start(25);
227
     myTimer->start(25);

+ 9
- 4
frog.h Целия файл

14
 public:
14
 public:
15
     explicit frog(QWidget *parent = 0);
15
     explicit frog(QWidget *parent = 0);
16
     float Ysapo, Xsapo, initialX;
16
     float Ysapo, Xsapo, initialX;
17
-    bool flag, xFlag, backFlag, moved, textBool;
18
-    int A, B, C, buttonCounter,temp,temp2,xnenu,xnenu2,xnenu3;
17
+    bool flag, xFlag, backFlag, moved, showText;
18
+
19
+    // coefficients from the quadratic formula
20
+    int A, B, C;
21
+
22
+    int temp,temp2,xnenu,xnenu2,xnenu3;
19
 
23
 
20
     double QuadraticPlus(int a, int b, int c);
24
     double QuadraticPlus(int a, int b, int c);
21
     double QuadraticMinus(int a, int b, int c);
25
     double QuadraticMinus(int a, int b, int c);
26
+    void computeNenuPositions();
22
     ~frog();
27
     ~frog();
23
 
28
 
24
 private:
29
 private:
28
 signals:
33
 signals:
29
 
34
 
30
 public slots:
35
 public slots:
31
-    void mySlot();
36
+    void animate();
32
     void run(int, int, int);
37
     void run(int, int, int);
33
     void reset();
38
     void reset();
34
-    void updatenenu();
39
+
35
 
40
 
36
 protected:
41
 protected:
37
     void paintEvent(QPaintEvent *event);
42
     void paintEvent(QPaintEvent *event);

+ 54
- 80
mainwindow.cpp Целия файл

14
 {
14
 {
15
     quadraticFormulaCheck();
15
     quadraticFormulaCheck();
16
 
16
 
17
-
18
     ui->setupUi(this);
17
     ui->setupUi(this);
19
     scene= new QGraphicsScene(this);
18
     scene= new QGraphicsScene(this);
20
     scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
19
     scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
32
     ui->RunPushButton->move(10,385);
31
     ui->RunPushButton->move(10,385);
33
     ui->retryButton->move(180,385);
32
     ui->retryButton->move(180,385);
34
 
33
 
35
-
36
-
37
     frogger = new frog;
34
     frogger = new frog;
38
 
35
 
39
     globalTimer = new QTimer;
36
     globalTimer = new QTimer;
40
     scene->addWidget(frogger);
37
     scene->addWidget(frogger);
41
 
38
 
42
     ui->RunPushButton->setEnabled(false);
39
     ui->RunPushButton->setEnabled(false);
43
-
44
 }
40
 }
45
 
41
 
46
 
42
 
50
     delete scene;
46
     delete scene;
51
 }
47
 }
52
 
48
 
49
+// This function validates if the results from the QuadraticPlus and QuadraticMinus
50
+// functions are correct. If they are not, then the program exits.
53
 
51
 
54
 void MainWindow::quadraticFormulaCheck()
52
 void MainWindow::quadraticFormulaCheck()
55
 {
53
 {
56
-    // float firstX;
57
     double resultFromFunction;
54
     double resultFromFunction;
58
     bool pass = true;
55
     bool pass = true;
56
+
57
+
58
+    // The values and valuesForMinus arrays contain precomputed a, b, c, and y values
59
+    // for the quadratic equations. They are used for testing if the functions written by
60
+    // the student are correct.
61
+    // For example: if a = 357, b = -1000, c = 698, then y = 1.4804781099
62
+
59
     double values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
63
     double values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
60
-        689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
61
-        465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
62
-        -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
63
-        852 , 907 , 241 , -0.510947439149};
64
+                       689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
65
+                       465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
66
+                       -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
67
+                       852 , 907 , 241 , -0.510947439149};
64
 
68
 
65
     double valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
69
     double valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
66
-        270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
67
-        -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
68
-        225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
70
+                               270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
71
+                               -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
72
+                               225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
69
 
73
 
70
     double a, b, c, expected;
74
     double a, b, c, expected;
71
 
75
 
72
-    //Verifying if the equation written by the student is correct
76
+    // Validate the QuadraticPlus function....
77
+
73
     for (int i = 0; i < 40 && pass; i+=4){
78
     for (int i = 0; i < 40 && pass; i+=4){
74
         a        = values[i];
79
         a        = values[i];
75
         b        = values[i+1];
80
         b        = values[i+1];
77
         expected = values[i+3];
82
         expected = values[i+3];
78
 
83
 
79
         resultFromFunction = frogger->QuadraticPlus(a,b,c);
84
         resultFromFunction = frogger->QuadraticPlus(a,b,c);
80
-
81
-        if( fabs(resultFromFunction - expected) > 0.00001) {
82
-            pass = false;
83
-        }
85
+        pass = ( fabs(resultFromFunction - expected) < 0.00001) ;
84
     }
86
     }
87
+
85
     if(!pass){
88
     if(!pass){
86
         QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
89
         QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
87
         exit(1);
90
         exit(1);
88
     }
91
     }
89
 
92
 
90
-    //Verifying if the equation written by the student is correct
93
+    // Validate the QuadraticMinus function....
94
+
91
     for (int i = 0; i < 40 && pass; i+=4){
95
     for (int i = 0; i < 40 && pass; i+=4){
92
         a        = valuesForMinus[i];
96
         a        = valuesForMinus[i];
93
         b        = valuesForMinus[i+1];
97
         b        = valuesForMinus[i+1];
95
         expected = valuesForMinus[i+3];
99
         expected = valuesForMinus[i+3];
96
 
100
 
97
         resultFromFunction = frogger->QuadraticMinus(a,b,c);
101
         resultFromFunction = frogger->QuadraticMinus(a,b,c);
98
-
99
-        if( fabs(resultFromFunction - expected) > 0.00001) {
100
-            pass = false;
101
-        }
102
+        pass =  ( fabs(resultFromFunction - expected) < 0.00001 );
102
     }
103
     }
104
+
103
     if(!pass){
105
     if(!pass){
104
         QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
106
         QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
105
         exit(1);
107
         exit(1);
106
     }
108
     }
107
 }
109
 }
108
 
110
 
109
-void MainWindow::on_RunPushButton_clicked()
110
-{
111
-    int a = 0;
112
-    int b = 0;
113
-    int c = 0;
114
-    int det = 0;
111
+void MainWindow::clearTheTextBoxes() {
112
+    ui->alineEdit->clear();
113
+    ui->blineEdit->clear();
114
+    ui->clineEdit->clear();
115
+}
116
+
117
+bool MainWindow::aTextBoxIsEmpty() {
118
+    return (ui->alineEdit->text().length() == 0 ||
119
+            ui->blineEdit->text().length() == 0 ||
120
+            ui->clineEdit->text().length() == 0);
121
+}
122
+
123
+
124
+void MainWindow::on_RunPushButton_clicked() {
125
+    int a = 0, b = 0, c = 0;
126
+    int det;
127
+
115
     a = (ui->alineEdit->text().toInt());
128
     a = (ui->alineEdit->text().toInt());
116
     b = (ui->blineEdit->text().toInt());
129
     b = (ui->blineEdit->text().toInt());
117
     c = (ui->clineEdit->text().toInt());
130
     c = (ui->clineEdit->text().toInt());
118
 
131
 
119
     det = (pow(b,2) - (4*a*c));
132
     det = (pow(b,2) - (4*a*c));
120
 
133
 
121
-    if(a > 0){
134
+    if(a > 0)
122
         QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
135
         QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
123
-        ui->alineEdit->clear();
124
-        ui->blineEdit->clear();
125
-        ui->clineEdit->clear();
126
-    }
127
-    else if(a == 0){
136
+    else if(a == 0)
128
         QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
137
         QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
129
-        ui->alineEdit->clear();
130
-        ui->blineEdit->clear();
131
-        ui->clineEdit->clear();
132
-    }
133
-    else{
134
-        if(det < 0){
138
+    else {
139
+        if(det < 0)
135
             QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
140
             QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
136
-            ui->alineEdit->clear();
137
-            ui->blineEdit->clear();
138
-            ui->clineEdit->clear();
139
-        }
140
-        else if(det == 0){
141
+        else if(det == 0)
141
             QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
142
             QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
142
-            ui->alineEdit->clear();
143
-            ui->blineEdit->clear();
144
-            ui->clineEdit->clear();
145
-        }
146
         else{
143
         else{
147
             frogger->run(a, b, c);
144
             frogger->run(a, b, c);
148
             ui->RunPushButton->setEnabled(false);
145
             ui->RunPushButton->setEnabled(false);
149
-            ui->alineEdit->clear();
150
-            ui->blineEdit->clear();
151
-            ui->clineEdit->clear();
152
         }
146
         }
153
     }
147
     }
154
-
155
-
148
+    clearTheTextBoxes();
156
 }
149
 }
157
 
150
 
158
-void MainWindow::on_retryButton_clicked()
159
-{
151
+void MainWindow::on_retryButton_clicked() {
160
     frogger->reset();
152
     frogger->reset();
161
 }
153
 }
162
 
154
 
163
-void MainWindow::on_alineEdit_textChanged(const QString &)
164
-{
165
-    if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
166
-        ui->RunPushButton->setEnabled(true);
167
-    }
168
-    else{
169
-        ui->RunPushButton->setEnabled(false);
170
-    }
155
+void MainWindow::on_alineEdit_textChanged(const QString &) {
156
+    ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
171
 }
157
 }
172
 
158
 
173
-void MainWindow::on_blineEdit_textChanged(const QString &)
174
-{
175
-    if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
176
-        ui->RunPushButton->setEnabled(true);
177
-    }
178
-    else{
179
-        ui->RunPushButton->setEnabled(false);
180
-    }
159
+void MainWindow::on_blineEdit_textChanged(const QString &) {
160
+    ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
181
 }
161
 }
182
 
162
 
183
-void MainWindow::on_clineEdit_textChanged(const QString &)
184
-{
185
-    if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
186
-        ui->RunPushButton->setEnabled(true);
187
-    }
188
-    else{
189
-        ui->RunPushButton->setEnabled(false);
190
-    }
163
+void MainWindow::on_clineEdit_textChanged(const QString &) {
164
+    ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
191
 }
165
 }

+ 4
- 0
mainwindow.h Целия файл

30
 
30
 
31
     void on_clineEdit_textChanged(const QString &arg1);
31
     void on_clineEdit_textChanged(const QString &arg1);
32
 
32
 
33
+    void clearTheTextBoxes();
34
+
35
+    bool aTextBoxIsEmpty();
36
+
33
 private:
37
 private:
34
     Ui::MainWindow *ui;
38
     Ui::MainWindow *ui;
35
     QGraphicsScene*scene;
39
     QGraphicsScene*scene;