Nenhuma descrição

functions.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. // RAN - 2015-06-26 - Fixed the issue with the leading zeros in Zulu
  2. #include "mainwindow.h"
  3. #include "ui_mainwindow.h"
  4. #include <QDebug>
  5. #include <QString>
  6. #include <cstdlib>
  7. #include <QMessageBox>
  8. #include <QMap>
  9. #include "functions.h"
  10. #ifdef _WIN32
  11. #include <windows.h>
  12. #else
  13. #include <unistd.h>
  14. #endif
  15. // =========================================================
  16. // Sort Functions
  17. // =========================================================
  18. bool validateSorts(const QString &a, const QString &b, const QString &c) {
  19. return (a.length() * b.length() * c.length() > 0);
  20. }
  21. void mySortAlpha(QString &a, QString &b, QString &c) {
  22. if (a > b ) {
  23. if (c > a) swap(b,c);
  24. else if (b > c) swap(a,b);
  25. else {
  26. QString tmp = a;
  27. a = b; b = c; c = tmp;
  28. }
  29. }
  30. else {
  31. if (a > c) {
  32. QString tmp = c;
  33. c = b; b = a; a = tmp;
  34. }
  35. else if (b > c) swap(b,c);
  36. else {
  37. // they are already in order :-)
  38. }
  39. }
  40. }
  41. void mySortBeta(QString &a, QString &b, QString &c) {
  42. if (a > b ) {
  43. if (c > a) swap(a,b);
  44. else if (b > c) swap(a,c);
  45. else {
  46. QString tmp = a;
  47. a = b; b = c; c = tmp;
  48. }
  49. }
  50. else {
  51. if (a > c) {
  52. QString tmp = c;
  53. c = b; b = a; a = tmp;
  54. }
  55. else if (b > c) swap(b,c);
  56. else {
  57. // they are already in order :-)
  58. }
  59. }
  60. }
  61. void mySortGamma(QString &a, QString &b, QString &c) {
  62. if (a > b ) {
  63. if (c > a) swap(a,b);
  64. else if (b > c) swap(a,c);
  65. else {
  66. QString tmp = a;
  67. a = c; c = b; b = tmp;
  68. }
  69. }
  70. else {
  71. if (a > c) {
  72. QString tmp = c;
  73. c = b; b = a; a = tmp;
  74. }
  75. else if (b > c) swap(b,c);
  76. else {
  77. // they are already in order :-)
  78. }
  79. }
  80. }
  81. void mySortDelta(QString &a, QString &b, QString &c) {
  82. if (a < b ) {
  83. if (c < a) swap(a,b);
  84. else if (b < c) swap(a,c);
  85. else {
  86. QString tmp = a;
  87. a = b; b = c; c = tmp;
  88. }
  89. }
  90. else {
  91. if (a > c) {
  92. QString tmp = c;
  93. c = b; b = a; a = tmp;
  94. }
  95. else if (b > c) swap(b,c);
  96. else {
  97. // they are already in order :-)
  98. }
  99. }
  100. }
  101. //========================================================
  102. // Functions for the dice roller
  103. //========================================================
  104. #include <iostream>
  105. using namespace std;
  106. void MainWindow::diceAlpha(){
  107. int total = diceFinal01 + diceFinal02 + 2;
  108. line[0]->setText(QString::number(total));
  109. }
  110. void MainWindow::diceBeta(){
  111. int total = diceFinal01 + diceFinal01 + 2;
  112. line[0]->setText(QString::number(total));
  113. }
  114. void MainWindow::diceGamma(){
  115. int total = diceFinal01 - diceFinal02;
  116. line[0]->setText(QString::number(total));
  117. }
  118. void MainWindow::diceDelta(){
  119. int total = diceFinal01 + diceFinal02;
  120. line[0]->setText(QString::number(total));
  121. }
  122. // =========================================================
  123. // Rock Paper Scissor Functions
  124. // =========================================================
  125. int RPSAlpha(char p1, char p2) {
  126. p1 = toupper(p1);
  127. p2 = toupper(p2);
  128. if ( p1 == 'P' ) {
  129. if ( p2 == 'P' ) return 0;
  130. else if (p2 == 'R') return 1;
  131. else return 2;
  132. }
  133. else if (p1 == 'R') {
  134. if ( p2 == 'R' ) return 0;
  135. else if (p2 == 'S') return 1;
  136. else return 2;
  137. }
  138. else {
  139. if ( p2 == 'S' ) return 0;
  140. else if (p2 == 'P') return 1;
  141. else return 2;
  142. }
  143. }
  144. int RPSBeta(char p1, char p2) {
  145. p1 = toupper(p1);
  146. p2 = toupper(p2);
  147. if ( p1 == 'P' ) {
  148. if ( p2 == 'S' ) return 0;
  149. else if (p2 == 'R') return 1;
  150. else return 2;
  151. }
  152. else if (p1 == 'R') {
  153. if ( p2 == 'S' ) return 0;
  154. else if (p2 == 'P') return 1;
  155. else return 2;
  156. }
  157. else {
  158. if ( p2 == 'S' ) return 0;
  159. else if (p2 == 'R') return 1;
  160. else return 2;
  161. }
  162. }
  163. int RPSGamma(char p1, char p2) {
  164. p1 = toupper(p1);
  165. p2 = toupper(p2);
  166. if ( p1 == 'P' ) {
  167. if ( p2 == 'P' ) return 0;
  168. else if (p2 == 'S') return 1;
  169. else return 2;
  170. }
  171. else if (p1 == 'R') {
  172. if ( p2 == 'R' ) return 0;
  173. else if (p2 == 'P') return 1;
  174. else return 2;
  175. }
  176. else {
  177. if ( p2 == 'P' ) return 0;
  178. else if (p2 == 'S') return 1;
  179. else return 2;
  180. }
  181. }
  182. int RPSDelta(char p1, char p2) {
  183. p1 = toupper(p1);
  184. p2 = toupper(p2);
  185. if ( p1 == 'P' ) {
  186. if ( p2 == 'P' ) return 0;
  187. else if (p2 == 'S') return 1;
  188. else return 2;
  189. }
  190. else if (p1 == 'R') {
  191. if ( p2 == 'R' ) return 0;
  192. else if (p2 == 'P') return 1;
  193. else return 2;
  194. }
  195. else {
  196. if ( p2 == 'P' ) return 0;
  197. else if (p2 == 'S') return 1;
  198. else return 2;
  199. }
  200. }
  201. // =========================================================
  202. // Check Words Functions
  203. // =========================================================
  204. QMap<int,QString> M;
  205. ///
  206. /// \brief initCheckWMaps: initialize the values in the dictionary that is used throughout
  207. /// the check functions.
  208. ///
  209. void initCheckWMaps() {
  210. M[1] = "one"; M[2] = "two"; M[3] = "three"; M[4] = "four";
  211. M[5] = "five"; M[6] = "six";
  212. M[7] = "seven"; M[8] = "eight"; M[9] = "nine"; M[10] = "ten";
  213. M[11] = "eleven"; M[12] = "twelve"; M[13] = "thirteen"; M[14] = "fourteen";
  214. M[15] = "fifteen"; M[16] = "sixteen"; M[17] = "seventeen"; M[18] = "eighteen";
  215. M[19] = "nineteen";
  216. M[20] = "twenty"; M[30] = "thirty"; M[40] = "fourty"; M[50] = "fifty";
  217. M[60] = "sixty"; M[70] = "seventy"; M[80] = "eighty"; M[90] = "ninety";
  218. }
  219. ///
  220. /// \brief validateCheckQty: determines is entered text is a valid number in [0,999999999]
  221. /// \param st text entered by the user
  222. /// \param qty text converted to integer
  223. /// \return true if the entered text is valid
  224. ///
  225. bool validateCheckQty(QString st, unsigned int &qty) {
  226. int i = 0;
  227. for (i = 0; i < st.length() ; i++) {
  228. if (!st[i].isDigit()) return false;
  229. }
  230. if (i > 9) return false;
  231. qty = st.toInt();
  232. return true;
  233. }
  234. ///
  235. /// \brief wordForNumber: Given a number n in [0,999] returns the word equivalent
  236. /// \param n: the number
  237. /// \return a Qstring containing the number in words
  238. ///
  239. QString wordForNumber(int n) {
  240. QString st;
  241. int tens = n % 100;
  242. if (tens == 0)
  243. st = "";
  244. else if (tens <= 20)
  245. st = M[n];
  246. else {
  247. st = M[10 * (tens/10)];
  248. if (tens % 10)
  249. st.append(" " + M[tens % 10]);
  250. }
  251. n = n / 100;
  252. if (n) st.prepend(M[n % 10] + " hundred" + (st.length() > 0 ? " " : ""));
  253. return st;
  254. }
  255. ///
  256. /// \brief checkWAlpha: One of the functions to convert a number in [0,999999999] to words
  257. /// \param n: the number
  258. /// \return a Qstring containing the number in words
  259. ///
  260. QString checkWAlpha(int n) {
  261. QString st;
  262. // the cents
  263. st = wordForNumber(n % 1000);
  264. // the thousands
  265. n = n / 1000;
  266. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  267. // the millions
  268. n = n / 1000;
  269. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  270. return st;
  271. }
  272. ///
  273. /// \brief checkWBeta: One of the functions to convert a number in [0,999999999] to words
  274. /// \param n: the number
  275. /// \return a Qstring containing the number in words
  276. ///
  277. QString checkWBeta(int n) {
  278. QString st;
  279. st = wordForNumber(n % 1000);
  280. n = n / 1000;
  281. if (n % 1000) st.append( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  282. n = n / 1000;
  283. if (n % 1000) st.append( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  284. return st;
  285. }
  286. ///
  287. /// \brief checkWGamma: One of the functions to convert a number in [0,999999999] to words
  288. /// \param n: the number
  289. /// \return a Qstring containing the number in words
  290. ///
  291. QString checkWGamma(int n) {
  292. QString st;
  293. st = wordForNumber(n % 10);
  294. n = n / 1000;
  295. if (n % 1000) st.append( wordForNumber(n % 10) + " thousand" + (st.length() > 0 ? " " : ""));
  296. n = n / 1000;
  297. if (n % 1000) st.append( wordForNumber(n % 10) + " million" + (st.length() > 0 ? " " : ""));
  298. return st;
  299. }
  300. ///
  301. /// \brief checkWDelta: One of the functions to convert a number in [0,999999999] to words
  302. /// \param n: the number
  303. /// \return a Qstring containing the number in words
  304. ///
  305. QString checkWDelta(int n) {
  306. QString st;
  307. n /= 10;
  308. st = wordForNumber(n % 1000);
  309. n = n / 1000;
  310. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  311. n = n / 1000;
  312. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  313. return st;
  314. }
  315. // =========================================================
  316. // Zulu Functions
  317. // =========================================================
  318. bool validZuluTime(const QString &time, const QString &zone, int &hours, int &minutes) {
  319. int i = 0;
  320. for (i = 0; i< time.size(); i++)
  321. if (!time[i].isDigit()) return false;
  322. if (i != 4) return false;
  323. hours = time.mid(0,2).toInt();
  324. minutes = time.mid(2,2).toInt();
  325. if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59)
  326. return false;
  327. if (zone.length() < 1 || zone[0] < 'A' ||
  328. zone[0] > 'Z' || toupper(zone.toStdString()[0]) == 'J')
  329. return false;
  330. return true;
  331. }
  332. //This is the correct one
  333. QString zuluAlpha(int hours, int minutes, char zone) {
  334. int diff = 0;
  335. zone = toupper(zone);
  336. if (zone <= 'I') diff = zone - 'A' + 1;
  337. else if (zone <= 'M') diff = 10 + (zone - 'K');
  338. else if (zone <= 'Y') diff = -(zone - 'N' + 1);
  339. else diff = 0;
  340. hours = (hours + (24 + diff) ) % 24;
  341. QString qstHours = QString::number(hours);
  342. if (qstHours.length() == 1) qstHours.prepend("0");
  343. QString qstMin = QString::number(minutes);
  344. if (qstMin.length() == 1) qstMin.prepend("0");
  345. return qstHours + qstMin;
  346. }
  347. QString zuluBeta(int hours, int minutes, char zone) {
  348. int diff = 0;
  349. if (zone <= 'I') diff = zone - 'B' + 1;
  350. else if (zone <= 'M') diff = 10 + (zone - 'M');
  351. else if (zone <= 'Y') diff = -(zone - 'N');
  352. diff = 0;
  353. hours = (hours + (24 + diff) ) % 24;
  354. QString qstHours = QString::number(hours);
  355. if (qstHours.length() == 1) qstHours.prepend("0");
  356. QString qstMin = QString::number(minutes);
  357. if (qstMin.length() == 1) qstMin.prepend("0");
  358. return qstHours + qstMin;
  359. }
  360. QString zuluGamma(int hours, int minutes, char zone) {
  361. int diff = 0;
  362. if (zone <= 'I') diff = zone - 'A' + 1;
  363. else if (zone <= 'M') diff = 10 + (zone - 'M');
  364. else if (zone <= 'Y') diff = -(zone - 'N');
  365. else diff = 0;
  366. hours = (hours + (24 + diff) ) % 24;
  367. QString qstHours = QString::number(hours);
  368. if (qstHours.length() == 1) qstHours.prepend("0");
  369. QString qstMin = QString::number(minutes);
  370. if (qstMin.length() == 1) qstMin.prepend("0");
  371. return qstHours + qstMin;
  372. }
  373. QString zuluDelta(int hours, int minutes, char zone) {
  374. int diff = 0;
  375. if (zone <= 'M') diff = zone - 'B' + 1;
  376. else if (zone <= 'Y') diff = -(zone - 'N');
  377. else diff = 0;
  378. hours = (hours + (24 - diff) ) % 24;
  379. QString qstHours = QString::number(hours);
  380. if (qstHours.length() == 1) qstHours.prepend("0");
  381. QString qstMin = QString::number(minutes);
  382. if (qstMin.length() == 1) qstMin.prepend("0");
  383. return qstHours + qstMin;
  384. }
  385. // =========================================================
  386. // APFT Functions
  387. // =========================================================
  388. //This it the correct one
  389. void MainWindow::APFTAlpha(){
  390. //For each one of these variables we apply the formula that they use
  391. //to set the points
  392. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  393. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  394. double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
  395. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  396. //Since the maximum of points that one can obtain
  397. //on each excercise type is 100, we restrict it
  398. if (sit_ups > 100){
  399. sit_ups = 100;
  400. }
  401. else if (sit_ups < 0){
  402. sit_ups = 0;
  403. }
  404. if (push_ups > 100){
  405. push_ups = 100;
  406. }
  407. else if (push_ups < 0){
  408. push_ups = 0;
  409. }
  410. if (two_mile_run > 100){
  411. two_mile_run = 100;
  412. }
  413. else if (two_mile_run < 0){
  414. two_mile_run = 0;
  415. }
  416. //Finally we set the outputs.
  417. line[3]->setText(QString::number(sit_ups));
  418. line[4]->setText(QString::number(push_ups));
  419. line[5]->setText(QString::number(two_mile_run));
  420. line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
  421. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  422. label[7]->setText("PASS");
  423. }
  424. else{
  425. label[7]->setText("FAIL");
  426. }
  427. }
  428. //This one do not check if the number is greater than 100
  429. void MainWindow::APFTBeta(){
  430. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  431. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  432. double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
  433. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  434. line[3]->setText(QString::number(sit_ups));
  435. line[4]->setText(QString::number(push_ups));
  436. line[5]->setText(QString::number(two_mile_run));
  437. line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
  438. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  439. line[7]->setText("PASS");
  440. }
  441. else{
  442. line[7]->setText("FAIL");
  443. }
  444. }
  445. //This one switch the results
  446. void MainWindow::APFTGamma(){
  447. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  448. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  449. double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
  450. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  451. if (sit_ups > 100){
  452. sit_ups = 100;
  453. }
  454. else if (sit_ups < 0){
  455. sit_ups = 0;
  456. }
  457. if (push_ups > 100){
  458. push_ups = 100;
  459. }
  460. else if (push_ups < 0){
  461. push_ups = 0;
  462. }
  463. if (two_mile_run > 100){
  464. two_mile_run = 100;
  465. }
  466. else if (two_mile_run < 0){
  467. two_mile_run = 0;
  468. }
  469. line[5]->setText(QString::number(sit_ups));
  470. line[3]->setText(QString::number(push_ups));
  471. line[4]->setText(QString::number(two_mile_run));
  472. line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
  473. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  474. line[7]->setText("PASS");
  475. }
  476. else{
  477. line[7]->setText("FAIL");
  478. }
  479. }
  480. //This one do not sums the points but the quantities of each excercise
  481. void MainWindow::APFTDelta(){
  482. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  483. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  484. double running_time = qFloor((line[2]->text().left(2).append('.'
  485. + line[2]->text().right(2))).toDouble());
  486. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  487. if (sit_ups > 100){
  488. sit_ups = 100;
  489. }
  490. else if (sit_ups < 0){
  491. sit_ups = 0;
  492. }
  493. if (push_ups > 100){
  494. push_ups = 100;
  495. }
  496. else if (push_ups < 0){
  497. push_ups = 0;
  498. }
  499. if (two_mile_run > 100){
  500. two_mile_run = 100;
  501. }
  502. else if (two_mile_run < 0){
  503. two_mile_run = 0;
  504. }
  505. int sumaTo = line[0]->text().toInt() + line[1]->text().toInt() + running_time;
  506. line[3]->setText(QString::number(sit_ups));
  507. line[4]->setText(QString::number(push_ups));
  508. line[5]->setText(QString::number(two_mile_run));
  509. line[6]->setText(QString::number(sumaTo));
  510. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  511. line[7]->setText("PASS");
  512. }
  513. else{
  514. line[7]->setText("FAIL");
  515. }
  516. }