Nav apraksta

mainwindow.cpp 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ///
  2. /// Modifications:
  3. /// - 2015-01-13 [RAN] - Changed the quadraticFormulaCheck function to
  4. /// prevent giving away the expression
  5. #include "mainwindow.h"
  6. #include "ui_mainwindow.h"
  7. #include <QMessageBox>
  8. MainWindow::MainWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainWindow)
  11. {
  12. quadraticFormulaCheck();
  13. ui->setupUi(this);
  14. scene= new QGraphicsScene(this);
  15. scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
  16. ui->MainView->setScene(scene);
  17. ui->MainView->setAlignment((Qt::AlignLeft | Qt::AlignTop));
  18. ui->MainView->resize(580,345);
  19. ui->MainView->move(0,0);
  20. ui->alabel->move(10,360);
  21. ui->alineEdit->move(25,358);
  22. ui->blabel->move(85,360);
  23. ui->blineEdit->move(100,358);
  24. ui->clabel->move(160,360);
  25. ui->clineEdit->move(175,358);
  26. ui->RunPushButton->move(10,385);
  27. ui->retryButton->move(180,385);
  28. frogger = new frog;
  29. globalTimer = new QTimer;
  30. scene->addWidget(frogger);
  31. ui->RunPushButton->setEnabled(false);
  32. }
  33. MainWindow::~MainWindow()
  34. {
  35. delete ui;
  36. delete scene;
  37. }
  38. void MainWindow::quadraticFormulaCheck()
  39. {
  40. // float firstX;
  41. float resultFromFunction;
  42. bool pass = true;
  43. float values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
  44. 689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
  45. 465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
  46. -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
  47. 852 , 907 , 241 , -0.510947439149};
  48. float valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
  49. 270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
  50. -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
  51. 225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
  52. float a, b, c, expected;
  53. //Verifying if the equation written by the student is correct
  54. for (int i = 0; i < 40 && pass; i+=4){
  55. a = values[i];
  56. b = values[i+1];
  57. c = values[i+2];
  58. expected = values[i+3];
  59. resultFromFunction = frogger->QuadraticPlus(a,b,c);
  60. if( abs(resultFromFunction - expected) > 0.00001) {
  61. pass = false;
  62. }
  63. }
  64. if(!pass){
  65. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  66. exit(1);
  67. }
  68. //Verifying if the equation written by the student is correct
  69. for (int i = 0; i < 40 && pass; i+=4){
  70. a = valuesForMinus[i];
  71. b = valuesForMinus[i+1];
  72. c = valuesForMinus[i+2];
  73. expected = valuesForMinus[i+3];
  74. resultFromFunction = frogger->QuadraticMinus(a,b,c);
  75. if( abs(resultFromFunction - expected) > 0.00001) {
  76. pass = false;
  77. }
  78. }
  79. if(!pass){
  80. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  81. exit(1);
  82. }
  83. }
  84. void MainWindow::on_RunPushButton_clicked()
  85. {
  86. int a = 0;
  87. int b = 0;
  88. int c = 0;
  89. int det = 0;
  90. a = (ui->alineEdit->text().toInt());
  91. b = (ui->blineEdit->text().toInt());
  92. c = (ui->clineEdit->text().toInt());
  93. det = (pow(b,2) - (4*a*c));
  94. if(a > 0){
  95. QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
  96. ui->alineEdit->clear();
  97. ui->blineEdit->clear();
  98. ui->clineEdit->clear();
  99. }
  100. else if(a == 0){
  101. QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
  102. ui->alineEdit->clear();
  103. ui->blineEdit->clear();
  104. ui->clineEdit->clear();
  105. }
  106. else{
  107. if(det < 0){
  108. QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
  109. ui->alineEdit->clear();
  110. ui->blineEdit->clear();
  111. ui->clineEdit->clear();
  112. }
  113. else if(det == 0){
  114. QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
  115. ui->alineEdit->clear();
  116. ui->blineEdit->clear();
  117. ui->clineEdit->clear();
  118. }
  119. else{
  120. frogger->run(a, b, c);
  121. ui->RunPushButton->setEnabled(false);
  122. ui->alineEdit->clear();
  123. ui->blineEdit->clear();
  124. ui->clineEdit->clear();
  125. }
  126. }
  127. }
  128. void MainWindow::on_retryButton_clicked()
  129. {
  130. frogger->reset();
  131. }
  132. void MainWindow::on_alineEdit_textChanged(const QString &arg1)
  133. {
  134. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  135. ui->RunPushButton->setEnabled(true);
  136. }
  137. else{
  138. ui->RunPushButton->setEnabled(false);
  139. }
  140. }
  141. void MainWindow::on_blineEdit_textChanged(const QString &arg1)
  142. {
  143. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  144. ui->RunPushButton->setEnabled(true);
  145. }
  146. else{
  147. ui->RunPushButton->setEnabled(false);
  148. }
  149. }
  150. void MainWindow::on_clineEdit_textChanged(const QString &arg1)
  151. {
  152. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  153. ui->RunPushButton->setEnabled(true);
  154. }
  155. else{
  156. ui->RunPushButton->setEnabled(false);
  157. }
  158. }