Ingen beskrivning

mainwindow.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <cmath>
  9. MainWindow::MainWindow(QWidget *parent) :
  10. QMainWindow(parent),
  11. ui(new Ui::MainWindow)
  12. {
  13. quadraticFormulaCheck();
  14. ui->setupUi(this);
  15. scene= new QGraphicsScene(this);
  16. scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
  17. ui->MainView->setScene(scene);
  18. ui->MainView->setAlignment((Qt::AlignLeft | Qt::AlignTop));
  19. ui->MainView->resize(580,345);
  20. ui->MainView->move(0,0);
  21. ui->alabel->move(10,360);
  22. ui->alineEdit->move(25,358);
  23. ui->blabel->move(85,360);
  24. ui->blineEdit->move(100,358);
  25. ui->clabel->move(160,360);
  26. ui->clineEdit->move(175,358);
  27. ui->RunPushButton->move(10,385);
  28. ui->retryButton->move(180,385);
  29. frogger = new frog;
  30. globalTimer = new QTimer;
  31. scene->addWidget(frogger);
  32. ui->RunPushButton->setEnabled(false);
  33. }
  34. MainWindow::~MainWindow()
  35. {
  36. delete ui;
  37. delete scene;
  38. }
  39. // This function validates if the results from the QuadraticPlus and QuadraticMinus
  40. // functions are correct. If they are not, then the program exits.
  41. void MainWindow::quadraticFormulaCheck()
  42. {
  43. double resultFromFunction;
  44. bool pass = true;
  45. // The values and valuesForMinus arrays contain precomputed a, b, c, and y values
  46. // for the quadratic equations. They are used for testing if the functions written by
  47. // the student are correct.
  48. // For example: if a = 357, b = -1000, c = 698, then y = 1.4804781099
  49. double values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
  50. 689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
  51. 465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
  52. -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
  53. 852 , 907 , 241 , -0.510947439149};
  54. double valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
  55. 270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
  56. -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
  57. 225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
  58. double a, b, c, expected;
  59. // Validate the QuadraticPlus function....
  60. for (int i = 0; i < 40 && pass; i+=4){
  61. a = values[i];
  62. b = values[i+1];
  63. c = values[i+2];
  64. expected = values[i+3];
  65. resultFromFunction = frogger->QuadraticPlus(a,b,c);
  66. pass = ( fabs(resultFromFunction - expected) < 0.00001) ;
  67. }
  68. if(!pass){
  69. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  70. exit(1);
  71. }
  72. // Validate the QuadraticMinus function....
  73. for (int i = 0; i < 40 && pass; i+=4){
  74. a = valuesForMinus[i];
  75. b = valuesForMinus[i+1];
  76. c = valuesForMinus[i+2];
  77. expected = valuesForMinus[i+3];
  78. resultFromFunction = frogger->QuadraticMinus(a,b,c);
  79. pass = ( fabs(resultFromFunction - expected) < 0.00001 );
  80. }
  81. if(!pass){
  82. QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
  83. exit(1);
  84. }
  85. }
  86. void MainWindow::clearTheTextBoxes() {
  87. ui->alineEdit->clear();
  88. ui->blineEdit->clear();
  89. ui->clineEdit->clear();
  90. }
  91. bool MainWindow::aTextBoxIsEmpty() {
  92. return (ui->alineEdit->text().length() == 0 ||
  93. ui->blineEdit->text().length() == 0 ||
  94. ui->clineEdit->text().length() == 0);
  95. }
  96. void MainWindow::on_RunPushButton_clicked() {
  97. int a = 0, b = 0, c = 0;
  98. int det;
  99. a = (ui->alineEdit->text().toInt());
  100. b = (ui->blineEdit->text().toInt());
  101. c = (ui->clineEdit->text().toInt());
  102. det = (pow(b,2) - (4*a*c));
  103. if(a > 0)
  104. QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
  105. else if(a == 0)
  106. QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
  107. else {
  108. if(det < 0)
  109. QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
  110. else if(det == 0)
  111. QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
  112. else{
  113. frogger->run(a, b, c);
  114. ui->RunPushButton->setEnabled(false);
  115. }
  116. }
  117. clearTheTextBoxes();
  118. }
  119. void MainWindow::on_retryButton_clicked() {
  120. frogger->reset();
  121. }
  122. void MainWindow::on_alineEdit_textChanged(const QString &) {
  123. ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
  124. }
  125. void MainWindow::on_blineEdit_textChanged(const QString &) {
  126. ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
  127. }
  128. void MainWindow::on_clineEdit_textChanged(const QString &) {
  129. ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
  130. }