No Description

mainwindow.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. //ui->x1Label->setText(fr);
  30. // ui->x1Label->move(frogger->xnenu2, 300);
  31. globalTimer = new QTimer;
  32. scene->addWidget(frogger);
  33. ui->RunPushButton->setEnabled(false);
  34. }
  35. MainWindow::~MainWindow()
  36. {
  37. delete ui;
  38. delete scene;
  39. }
  40. void MainWindow::quadraticFormulaCheck()
  41. {
  42. // float firstX;
  43. float resultFromFunction;
  44. bool pass = true;
  45. float values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
  46. 689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
  47. 465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
  48. -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
  49. 852 , 907 , 241 , -0.510947439149};
  50. float valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
  51. 270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
  52. -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
  53. 225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
  54. float a, b, c, expected;
  55. //Verifying if the equation written by the student is correct
  56. for (int i = 0; i < 40 && pass; i+=4){
  57. a = values[i];
  58. b = values[i+1];
  59. c = values[i+2];
  60. expected = values[i+3];
  61. // firstX = (((-b) + sqrt((pow(b,2)) - 4*a*c))/(2*a));
  62. resultFromFunction = frogger->QuadraticPlus(a,b,c);
  63. if( abs(resultFromFunction - expected) > 0.00001) {
  64. pass = false;
  65. }
  66. }
  67. if(!pass){
  68. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  69. exit(1);
  70. }
  71. //Verifying if the equation written by the student is correct
  72. for (int i = 0; i < 40 && pass; i+=4){
  73. a = valuesForMinus[i];
  74. b = valuesForMinus[i+1];
  75. c = valuesForMinus[i+2];
  76. expected = valuesForMinus[i+3];
  77. // firstX = (((-b) + sqrt((pow(b,2)) - 4*a*c))/(2*a));
  78. resultFromFunction = frogger->QuadraticMinus(a,b,c);
  79. if( abs(resultFromFunction - expected) > 0.00001) {
  80. pass = false;
  81. }
  82. }
  83. if(!pass){
  84. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  85. exit(1);
  86. }
  87. }
  88. void MainWindow::on_RunPushButton_clicked()
  89. {
  90. int a = 0;
  91. int b = 0;
  92. int c = 0;
  93. int det = 0;
  94. a = (ui->alineEdit->text().toInt());
  95. b = (ui->blineEdit->text().toInt());
  96. c = (ui->clineEdit->text().toInt());
  97. det = (pow(b,2) - (4*a*c));
  98. if(a > 0){
  99. QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
  100. ui->alineEdit->clear();
  101. ui->blineEdit->clear();
  102. ui->clineEdit->clear();
  103. }
  104. else if(a == 0){
  105. QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
  106. ui->alineEdit->clear();
  107. ui->blineEdit->clear();
  108. ui->clineEdit->clear();
  109. }
  110. else{
  111. if(det < 0){
  112. QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
  113. ui->alineEdit->clear();
  114. ui->blineEdit->clear();
  115. ui->clineEdit->clear();
  116. }
  117. else if(det == 0){
  118. QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
  119. ui->alineEdit->clear();
  120. ui->blineEdit->clear();
  121. ui->clineEdit->clear();
  122. }
  123. else{
  124. frogger->run(a, b, c);
  125. ui->RunPushButton->setEnabled(false);
  126. ui->alineEdit->clear();
  127. ui->blineEdit->clear();
  128. ui->clineEdit->clear();
  129. }
  130. }
  131. }
  132. void MainWindow::on_retryButton_clicked()
  133. {
  134. frogger->reset();
  135. }
  136. void MainWindow::on_alineEdit_textChanged(const QString &arg1)
  137. {
  138. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  139. ui->RunPushButton->setEnabled(true);
  140. }
  141. else{
  142. ui->RunPushButton->setEnabled(false);
  143. }
  144. }
  145. void MainWindow::on_blineEdit_textChanged(const QString &arg1)
  146. {
  147. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  148. ui->RunPushButton->setEnabled(true);
  149. }
  150. else{
  151. ui->RunPushButton->setEnabled(false);
  152. }
  153. }
  154. void MainWindow::on_clineEdit_textChanged(const QString &arg1)
  155. {
  156. if(ui->alineEdit->text() != "" && ui->blineEdit->text() != "" && ui->clineEdit->text() != "" ){
  157. ui->RunPushButton->setEnabled(true);
  158. }
  159. else{
  160. ui->RunPushButton->setEnabled(false);
  161. }
  162. }