123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
-
- #include <QDebug>
- #include <QString>
- #include <cstdlib>
-
- #include <QMessageBox>
- #include <QMap>
- #include "functions.h"
- #ifdef _WIN32
- #include <windows.h>
- #else
- #include <unistd.h>
- #endif
-
-
- // =========================================================
- // Sort Functions
- // =========================================================
-
- bool validateSorts(const QString &a, const QString &b, const QString &c) {
- return (a.length() * b.length() * c.length() > 0);
- }
-
-
- void mySortAlpha(QString &a, QString &b, QString &c) {
- if (a > b ) {
- if (c > a) swap(b,c);
- else if (b > c) swap(a,b);
- else {
- QString tmp = a;
- a = b; b = c; c = tmp;
- }
- }
- else {
- if (a > c) {
- QString tmp = c;
- c = b; b = a; a = tmp;
- }
- else if (b > c) swap(b,c);
- else {
- // they are already in order :-)
- }
- }
- }
-
- void mySortBeta(QString &a, QString &b, QString &c) {
- if (a > b ) {
- if (c > a) swap(a,b);
- else if (b > c) swap(a,c);
- else {
- QString tmp = a;
- a = b; b = c; c = tmp;
- }
- }
- else {
- if (a > c) {
- QString tmp = c;
- c = b; b = a; a = tmp;
- }
- else if (b > c) swap(b,c);
- else {
- // they are already in order :-)
- }
- }
- }
-
- void mySortGamma(QString &a, QString &b, QString &c) {
- if (a > b ) {
- if (c > a) swap(a,b);
- else if (b > c) swap(a,c);
- else {
- QString tmp = a;
- a = c; c = b; b = tmp;
- }
- }
- else {
- if (a > c) {
- QString tmp = c;
- c = b; b = a; a = tmp;
- }
- else if (b > c) swap(b,c);
- else {
- // they are already in order :-)
- }
- }
- }
-
- void mySortDelta(QString &a, QString &b, QString &c) {
- if (a < b ) {
- if (c < a) swap(a,b);
- else if (b < c) swap(a,c);
- else {
- QString tmp = a;
- a = b; b = c; c = tmp;
- }
- }
- else {
- if (a > c) {
- QString tmp = c;
- c = b; b = a; a = tmp;
- }
- else if (b > c) swap(b,c);
- else {
- // they are already in order :-)
- }
- }
- }
-
-
- //This is the correct dice roller
- void MainWindow::diceAlpha(){
- QString a, b;
-
- //This loop is used to simulate the dice rolling by
- //changing the images at a slowing down speed
- for (int i = 0; i<=150000 ; i=i+5000){
- usleep(i);
-
- //First dice image. Since we want random pictures
- //on each iteration, we get a random number, converted
- //it to string and append it to others string that
- //conform the names of the dices
- a = ":/images/resources/d";
- a.append(QString::number(rand()%6 + 1));
- a.append(".png");
-
- b = ":/images/resources/d";
- b.append(QString::number(rand()%6 + 1));
- b.append(".png");
-
- //We load the images and check if there is no problem with them
- if(!dice1.load(a) || !dice2.load(b)){
- qDebug() << "Error2 Loading image";
- }
-
- //Finally we set the labels with the random dice images
- label[3]->setPixmap(QPixmap::fromImage(dice1));
- label[4]->setPixmap(QPixmap::fromImage(dice2));
-
- // Is there an option to see the changes of the dice/label
- // in this function and no just when we get out of it?
-
- // Fail attempts
- // layout->update();
- // repaint();
- // window->repaint();
-
- // label[3]->repaint();
- // label[3]->update();
- // label[3]->hide();
- // label[3]->show();
- // label[3]->setVisible(false);
- // label[3]->setVisible(true);
-
- // layout->update();
- // update();
- // window->update();
-
- window->hide();
- window->show();
- }
-
- //Finally, we set the result of the sum of the dices.
- QString a2 = (QString)a[20];
- QString b2 = (QString)b[20];
- int total = a2.toInt() + b2.toInt();
- line[0]->setText(QString::number(total));
- }
-
- //This version sums the first dice twice
- void MainWindow::diceBeta(){
- QString a, b;
-
- for (int i = 0; i<=150000 ; i=i+5000){
- usleep(i);
-
- a = ":/images/resources/d";
- a.append(QString::number(rand()%6 + 1));
- a.append(".png");
-
- b = ":/images/resources/d";
- b.append(QString::number(rand()%6 + 1));
- b.append(".png");
-
- if(!dice1.load(a) || !dice2.load(b)){
- qDebug() << "Error2 Loading image";
- }
-
- label[3]->setPixmap(QPixmap::fromImage(dice1));
- label[4]->setPixmap(QPixmap::fromImage(dice2));
-
- // Is there an option to see the changes of the dice/label
- // in this function and no just when we get out of it?
- window->hide();
- window->show();
- }
-
- int total = a[20].digitValue() + a[20].digitValue();
- line[0]->setText(QString::number(total));
- }
-
- //This one substracts the second dice to the first one
- void MainWindow::diceGamma(){
- QString a, b;
-
- for (int i = 0; i<=150000 ; i=i+5000){
- usleep(i);
-
- a = ":/images/resources/d";
- a.append(QString::number(rand()%6 + 1));
- a.append(".png");
-
- b = ":/images/resources/d";
- b.append(QString::number(rand()%6 + 1));
- b.append(".png");
-
- if(!dice1.load(a) || !dice2.load(b)){
- qDebug() << "Error2 Loading image";
- }
-
- label[3]->setPixmap(QPixmap::fromImage(dice1));
- label[4]->setPixmap(QPixmap::fromImage(dice2));
-
- // Is there an option to see the changes of the dice/label
- // in this function and no just when we get out of it?
- window->hide();
- window->show();
- }
-
- QString a2 = (QString)a[20];
- QString b2 = (QString)b[20];
- int total = a2.toInt() - b2.toInt();
- line[0]->setText(QString::number(total));
- }
-
- //This one tooks the number 6 as a 12
- void MainWindow::diceDelta(){
- QString a, b;
-
- for (int i = 0; i<=150000 ; i=i+5000){
- usleep(i);
-
- a = ":/images/resources/d";
- a.append(QString::number(rand()%6 + 1));
- a.append(".png");
-
- b = ":/images/resources/d";
- b.append(QString::number(rand()%6 + 1));
- b.append(".png");
-
- if(!dice1.load(a) || !dice2.load(b)){
- qDebug() << "Error2 Loading image";
- }
-
- label[3]->setPixmap(QPixmap::fromImage(dice1));
- label[4]->setPixmap(QPixmap::fromImage(dice2));
-
- // Is there an option to see the changes of the dice/label
- // in this function and no just when we get out of it?
- window->hide();
- window->show();
- }
-
- int x, y;
- QString a2 = (QString)a[20];
- QString b2 = (QString)b[20];
- if (a2.toInt() == 6){
- x = 12;
- }
- else{
- x = a2.toInt();
- }
- if (b2.toInt() == 6){
- y = 12;
- }
- else{
- y = b2.toInt();
- }
-
- int total = x + y;
- line[0]->setText(QString::number(total));
- }
-
- // =========================================================
- // Rock Paper Scissor Functions
- // =========================================================
-
-
- ///
- /// \brief RPSAlpha
- /// \param p1
- /// \param p2
- /// \param score1
- /// \param score2
- /// \return
- ///
-
- int RPSAlpha(char p1, char p2, int &score1, int &score2) {
- p1 = toupper(p1);
- p2 = toupper(p2);
- if ( p1 == 'P' ) {
- if ( p2 == 'P' ) return 0;
- else if (p2 == 'R') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- else if (p1 == 'R') {
- if ( p2 == 'R' ) return 0;
- else if (p2 == 'S') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- else {
- if ( p2 == 'S' ) return 0;
- else if (p2 == 'P') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- }
-
-
- ///
- /// \brief RPSBeta
- /// \param p1
- /// \param p2
- /// \param score1
- /// \param score2
- /// \return
- ///
-
- int RPSBeta(char p1, char p2, int &score1, int &score2) {
- p1 = toupper(p1);
- p2 = toupper(p2);
- if ( p1 == 'P' ) {
- if ( p2 == 'S' ) return 0;
- else if (p2 == 'R') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- else if (p1 == 'R') {
- if ( p2 == 'S' ) return 0;
- else if (p2 == 'P') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- else {
- if ( p2 == 'S' ) return 0;
- else if (p2 == 'R') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- }
-
-
- ///
- /// \brief RPSGamma
- /// \param p1
- /// \param p2
- /// \param score1
- /// \param score2
- /// \return
- ///
-
- int RPSGamma(char p1, char p2, int &score1, int &score2) {
- p1 = toupper(p1);
- p2 = toupper(p2);
- if ( p1 == 'P' ) {
- if ( p2 == 'P' ) return 0;
- else if (p2 == 'S') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- else if (p1 == 'R') {
- if ( p2 == 'R' ) return 0;
- else if (p2 == 'P') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- else {
- if ( p2 == 'P' ) return 0;
- else if (p2 == 'S') {
- score1++; return 1;
- }
- else {
- score2++; return 2;
- }
- }
- }
-
- ///
- /// \brief RPSDelta
- /// \param p1
- /// \param p2
- /// \param score1
- /// \param score2
- /// \return
- ///
-
- int RPSDelta(char p1, char p2, int &score1, int &score2) {
- p1 = toupper(p1);
- p2 = toupper(p2);
- if ( p1 == 'P' ) {
- if ( p2 == 'P' ) return 0;
- else if (p2 == 'S') {
- score2++; return 1;
- }
- else {
- score1++; return 2;
- }
- }
- else if (p1 == 'R') {
- if ( p2 == 'R' ) return 0;
- else if (p2 == 'P') {
- score2++; return 1;
- }
- else {
- score1++; return 2;
- }
- }
- else {
- if ( p2 == 'P' ) return 0;
- else if (p2 == 'S') {
- score2++; return 1;
- }
- else {
- score1++; return 2;
- }
- }
- }
-
- // =========================================================
- // Check Words Functions
- // =========================================================
-
-
-
- QMap<int,QString> M;
-
- ///
- /// \brief initCheckWMaps: initialize the values in the dictionary that is used throughout
- /// the check functions.
- ///
-
- void initCheckWMaps() {
-
- M[1] = "one"; M[2] = "two"; M[3] = "three"; M[4] = "four";
- M[5] = "five"; M[6] = "six";
- M[7] = "seven"; M[8] = "eight"; M[9] = "nine"; M[10] = "ten";
- M[11] = "eleven"; M[12] = "twelve"; M[13] = "thirteen"; M[14] = "fourteen";
- M[15] = "fifteen"; M[16] = "sixteen"; M[17] = "seventeen"; M[18] = "eighteen";
- M[19] = "nineteen";
- M[20] = "twenty"; M[30] = "thirty"; M[40] = "fourty"; M[50] = "fifty";
- M[60] = "sixty"; M[70] = "seventy"; M[80] = "eighty"; M[90] = "ninety";
- }
-
-
- ///
- /// \brief validateCheckQty: determines is entered text is a valid number in [0,999999999]
- /// \param st text entered by the user
- /// \param qty text converted to integer
- /// \return true if the entered text is valid
- ///
-
- bool validateCheckQty(QString st, unsigned int &qty) {
- int i = 0;
- for (i = 0; i < st.length() ; i++) {
- if (!st[i].isDigit()) return false;
- }
-
- if (i > 9) return false;
-
- qty = st.toInt();
- return true;
- }
-
- ///
- /// \brief wordForNumber: Given a number n in [0,999] returns the word equivalent
- /// \param n: the number
- /// \return a Qstring containing the number in words
- ///
-
- QString wordForNumber(int n) {
- QString st;
-
- int tens = n % 100;
- if (tens == 0)
- st = "";
- else if (tens <= 20)
- st = M[n];
- else {
- st = M[10 * (tens/10)];
- if (tens % 10)
- st.append(" " + M[tens % 10]);
- }
-
- n = n / 100;
- if (n) st.prepend(M[n % 10] + " hundred" + (st.length() > 0 ? " " : ""));
-
- return st;
- }
-
-
- ///
- /// \brief checkWAlpha: One of the functions to convert a number in [0,999999999] to words
- /// \param n: the number
- /// \return a Qstring containing the number in words
- ///
-
- QString checkWAlpha(int n) {
- QString st;
-
- // the cents
- st = wordForNumber(n % 1000);
-
- // the thousands
- n = n / 1000;
- if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
-
- // the millions
- n = n / 1000;
- if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
-
- return st;
- }
-
- ///
- /// \brief checkWBeta: One of the functions to convert a number in [0,999999999] to words
- /// \param n: the number
- /// \return a Qstring containing the number in words
- ///
-
- QString checkWBeta(int n) {
- QString st;
-
- st = wordForNumber(n % 1000);
- n = n / 1000;
- if (n % 1000) st.append( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
- n = n / 1000;
- if (n % 1000) st.append( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
-
- return st;
- }
-
- ///
- /// \brief checkWGamma: One of the functions to convert a number in [0,999999999] to words
- /// \param n: the number
- /// \return a Qstring containing the number in words
- ///
-
- QString checkWGamma(int n) {
- QString st;
-
- st = wordForNumber(n % 10);
- n = n / 1000;
- if (n % 1000) st.append( wordForNumber(n % 10) + " thousand" + (st.length() > 0 ? " " : ""));
- n = n / 1000;
- if (n % 1000) st.append( wordForNumber(n % 10) + " million" + (st.length() > 0 ? " " : ""));
-
- return st;
- }
-
- ///
- /// \brief checkWDelta: One of the functions to convert a number in [0,999999999] to words
- /// \param n: the number
- /// \return a Qstring containing the number in words
- ///
-
-
- QString checkWDelta(int n) {
- QString st;
-
- n /= 10;
- st = wordForNumber(n % 1000);
- n = n / 1000;
- if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
- n = n / 1000;
- if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
-
- return st;
- }
-
-
- // =========================================================
- // Zulu Functions
- // =========================================================
-
-
- bool validZuluTime(const QString &time, const QString &zone, int &hours, int &minutes) {
- int i = 0;
- for (i = 0; i< time.size(); i++)
- if (!time[i].isDigit()) return false;
-
- if (i != 4) return false;
- hours = time.mid(0,2).toInt();
- minutes = time.mid(2,2).toInt();
-
- if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59)
- return false;
-
- if (zone.length() < 1 || !zone[0].isLetter() || toupper(zone.toStdString()[0]) == 'J') return false;
- return true;
- }
-
- //This is the correct one
-
- QString zuluAlpha(int hours, int minutes, char zone) {
- int diff = 0;
-
- if (zone <= 'I') diff = zone - 'A' + 1;
- else if (zone <= 'M') diff = 10 + (zone - 'K');
- else if (zone <= 'Y') diff = -(zone - 'N' + 1);
- else diff = 0;
-
- hours = (hours + (24 + diff) ) % 24;
-
- return QString::number(hours) + QString::number(minutes);
- }
-
- QString zuluBeta(int hours, int minutes, char zone) {
- int diff = 0;
-
- if (zone <= 'I') diff = zone - 'B' + 1;
- else if (zone <= 'M') diff = 10 + (zone - 'M');
- else if (zone <= 'Y') diff = -(zone - 'N');
- diff = 0;
-
- hours = (hours + (24 + diff) ) % 24;
-
- return QString::number(hours) + QString::number(minutes);
- }
-
- QString zuluGamma(int hours, int minutes, char zone) {
- int diff = 0;
-
- if (zone <= 'I') diff = zone - 'A' + 1;
- else if (zone <= 'M') diff = 10 + (zone - 'M');
- else if (zone <= 'Y') diff = -(zone - 'N');
- else diff = 0;
-
- hours = (hours + (24 - diff) ) % 24;
-
- return QString::number(hours) + QString::number(minutes);
- }
-
- QString zuluDelta(int hours, int minutes, char zone) {
- int diff = 0;
-
- if (zone <= 'M') diff = zone - 'B' + 1;
- else if (zone <= 'Y') diff = -(zone - 'N');
- else diff = 0;
-
- hours = (hours + (24 - diff) ) % 24;
-
- return QString::number(hours) + QString::number(minutes);
- }
-
-
-
- // =========================================================
- // APFT Functions
- // =========================================================
-
-
- //This it the correct one
-
- void MainWindow::APFTAlpha(){
- //For each one of these variables we apply the formula that they use
- //to set the points
- double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
-
- double push_ups = qFloor(line[1]->text().toInt() * 1.42);
-
- double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
- double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
-
- //Since the maximum of points that one can obtain
- //on each excercise type is 100, we restrict it
- if (sit_ups > 100){
- sit_ups = 100;
- }
- else if (sit_ups < 0){
- sit_ups = 0;
- }
- if (push_ups > 100){
- push_ups = 100;
- }
- else if (push_ups < 0){
- push_ups = 0;
- }
- if (two_mile_run > 100){
- two_mile_run = 100;
- }
- else if (two_mile_run < 0){
- two_mile_run = 0;
- }
-
- //Finally we set the outputs.
- line[3]->setText(QString::number(sit_ups));
- line[4]->setText(QString::number(push_ups));
- line[5]->setText(QString::number(two_mile_run));
- line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
- if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
- label[7]->setText("PASS");
- }
- else{
- label[7]->setText("FAIL");
- }
- }
-
- //This one do not check if the number is greater than 100
-
- void MainWindow::APFTBeta(){
- double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
-
- double push_ups = qFloor(line[1]->text().toInt() * 1.42);
-
- double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
- double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
-
- line[3]->setText(QString::number(sit_ups));
- line[4]->setText(QString::number(push_ups));
- line[5]->setText(QString::number(two_mile_run));
- line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
- if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
- line[7]->setText("PASS");
- }
- else{
- line[7]->setText("FAIL");
- }
- }
-
- //This one switch the results
-
- void MainWindow::APFTGamma(){
- double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
- double push_ups = qFloor(line[1]->text().toInt() * 1.42);
- double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
- double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
-
- if (sit_ups > 100){
- sit_ups = 100;
- }
- else if (sit_ups < 0){
- sit_ups = 0;
- }
- if (push_ups > 100){
- push_ups = 100;
- }
- else if (push_ups < 0){
- push_ups = 0;
- }
- if (two_mile_run > 100){
- two_mile_run = 100;
- }
- else if (two_mile_run < 0){
- two_mile_run = 0;
- }
-
- line[5]->setText(QString::number(sit_ups));
- line[3]->setText(QString::number(push_ups));
- line[4]->setText(QString::number(two_mile_run));
- line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
- if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
- line[7]->setText("PASS");
- }
- else{
- line[7]->setText("FAIL");
- }
- }
-
- //This one do not sums the points but the quantities of each excercise
-
- void MainWindow::APFTDelta(){
- double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
- double push_ups = qFloor(line[1]->text().toInt() * 1.42);
- double running_time = qFloor((line[2]->text().left(2).append('.'
- + line[2]->text().right(2))).toDouble());
- double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
-
- if (sit_ups > 100){
- sit_ups = 100;
- }
- else if (sit_ups < 0){
- sit_ups = 0;
- }
- if (push_ups > 100){
- push_ups = 100;
- }
- else if (push_ups < 0){
- push_ups = 0;
- }
- if (two_mile_run > 100){
- two_mile_run = 100;
- }
- else if (two_mile_run < 0){
- two_mile_run = 0;
- }
-
- int sumaTo = line[0]->text().toInt() + line[1]->text().toInt() + running_time;
- line[3]->setText(QString::number(sit_ups));
- line[4]->setText(QString::number(push_ups));
- line[5]->setText(QString::number(two_mile_run));
- line[6]->setText(QString::number(sumaTo));
- if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
- line[7]->setText("PASS");
- }
- else{
- line[7]->setText("FAIL");
- }
- }
-
-
|