123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- ///
- /// Modifications:
- /// - 2015-01-13 [RAN] - Changed the quadraticFormulaCheck function to
- /// prevent giving away the expression
-
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QMessageBox>
- #include <cmath>
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- quadraticFormulaCheck();
-
- ui->setupUi(this);
- scene= new QGraphicsScene(this);
- scene->setSceneRect(QRectF(QPointF(0,0), QPointF(0,0)));
- ui->MainView->setScene(scene);
- ui->MainView->setAlignment((Qt::AlignLeft | Qt::AlignTop));
- ui->MainView->resize(580,345);
- ui->MainView->move(0,0);
-
- ui->alabel->move(10,360);
- ui->alineEdit->move(25,358);
- ui->blabel->move(85,360);
- ui->blineEdit->move(100,358);
- ui->clabel->move(160,360);
- ui->clineEdit->move(175,358);
- ui->RunPushButton->move(10,385);
- ui->retryButton->move(180,385);
-
- frogger = new frog;
-
- globalTimer = new QTimer;
- scene->addWidget(frogger);
-
- ui->RunPushButton->setEnabled(false);
- }
-
-
- MainWindow::~MainWindow()
- {
- delete ui;
- delete scene;
- }
-
- // This function validates if the results from the QuadraticPlus and QuadraticMinus
- // functions are correct. If they are not, then the program exits.
-
- void MainWindow::quadraticFormulaCheck()
- {
- double resultFromFunction;
- bool pass = true;
-
-
- // The values and valuesForMinus arrays contain precomputed a, b, c, and y values
- // for the quadratic equations. They are used for testing if the functions written by
- // the student are correct.
- // For example: if a = 357, b = -1000, c = 698, then y = 1.4804781099
-
- double values[] = {357 , -1000 , 698 , 1.4804781099 , 748 , -392 , 51 , 0.28391805554 ,
- 689 , -689 , 172 , 0.519048482944 , 90 , 521 , 751 , -2.71178575308 , 5 , 107 ,
- 465 , -6.0642692054 , 1609 , -983 , 150 , 0.314734649792 , 273 , 496 , 224 ,
- -0.839700867988 , 2 , 91 , 920 , -15.1630045473 , 48 , 300 , 463 , -2.77889067238 ,
- 852 , 907 , 241 , -0.510947439149};
-
- double valuesForMinus[] = {129 , -486 , 456 , 1.76744186047 , 384 , 980 , 625 , -1.30208333333 ,
- 270 , -904 , 755 , 1.59515823795 , 67 , -450 , 752 , 3.12650485528 , 98 , 506 , 651 ,
- -2.72985550047 , 38 , -373 , 901 , 4.29396930804 , 273 , -282 , 72 , 0.461538461538 ,
- 225 , -889 , 874 , 1.84 , 8 , 136 , 522 , -11.1457513111 , 5 , 77 , 214 , -11.760788100};
-
- double a, b, c, expected;
-
- // Validate the QuadraticPlus function....
-
- for (int i = 0; i < 40 && pass; i+=4){
- a = values[i];
- b = values[i+1];
- c = values[i+2];
- expected = values[i+3];
-
- resultFromFunction = frogger->QuadraticPlus(a,b,c);
- pass = ( fabs(resultFromFunction - expected) < 0.00001) ;
- }
-
- if(!pass){
- QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
- exit(1);
- }
-
- // Validate the QuadraticMinus function....
-
- for (int i = 0; i < 40 && pass; i+=4){
- a = valuesForMinus[i];
- b = valuesForMinus[i+1];
- c = valuesForMinus[i+2];
- expected = valuesForMinus[i+3];
-
- resultFromFunction = frogger->QuadraticMinus(a,b,c);
- pass = ( fabs(resultFromFunction - expected) < 0.00001 );
- }
-
- if(!pass){
- QMessageBox::information(this, "Error", "La funcion cuadratica esta mal escrita!");
- exit(1);
- }
- }
-
- void MainWindow::clearTheTextBoxes() {
- ui->alineEdit->clear();
- ui->blineEdit->clear();
- ui->clineEdit->clear();
- }
-
- bool MainWindow::aTextBoxIsEmpty() {
- return (ui->alineEdit->text().length() == 0 ||
- ui->blineEdit->text().length() == 0 ||
- ui->clineEdit->text().length() == 0);
- }
-
-
- void MainWindow::on_RunPushButton_clicked() {
- int a = 0, b = 0, c = 0;
- int det;
-
- a = (ui->alineEdit->text().toInt());
- b = (ui->blineEdit->text().toInt());
- c = (ui->clineEdit->text().toInt());
-
- det = (pow(b,2) - (4*a*c));
-
- if(a > 0)
- QMessageBox::information(this, "Error", "La parabola abre hacia arriba y por lo tanto el sapo no brincara");
- else if(a == 0)
- QMessageBox::information(this, "Error", "No hay parabola y por lo tanto el sapo no brincara");
- else {
- if(det < 0)
- QMessageBox::information(this, "Error", "No tiene intercepto y por lo tanto el sapo no brincara");
- else if(det == 0)
- QMessageBox::information(this, "Error", "El vertice esta en el intercepto y por lo tanto el sapo no brincara");
- else{
- frogger->run(a, b, c);
- ui->RunPushButton->setEnabled(false);
- }
- }
- clearTheTextBoxes();
- }
-
- void MainWindow::on_retryButton_clicked() {
- frogger->reset();
- }
-
- void MainWindow::on_alineEdit_textChanged(const QString &) {
- ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
- }
-
- void MainWindow::on_blineEdit_textChanged(const QString &) {
- ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
- }
-
- void MainWindow::on_clineEdit_textChanged(const QString &) {
- ui->RunPushButton->setEnabled(!aTextBoxIsEmpty());
- }
|