Keine Beschreibung

functions.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. //This is the correct dice roller
  105. void MainWindow::diceAlpha(){
  106. int total = diceFinal01 + diceFinal02 + 2;
  107. line[0]->setText(QString::number(total));
  108. }
  109. //This version sums the first dice twice
  110. void MainWindow::diceBeta(){
  111. int total = diceFinal01 + diceFinal01 + 2;
  112. line[0]->setText(QString::number(total));
  113. }
  114. //This one substracts the second dice to the first one
  115. void MainWindow::diceGamma(){
  116. int total = diceFinal01 - diceFinal02;
  117. line[0]->setText(QString::number(total));
  118. }
  119. //This one takes the number 6 as a 12
  120. void MainWindow::diceDelta(){
  121. int total = diceFinal01 + diceFinal02;
  122. line[0]->setText(QString::number(total));
  123. }
  124. // =========================================================
  125. // Rock Paper Scissor Functions
  126. // =========================================================
  127. int RPSAlpha(char p1, char p2) {
  128. p1 = toupper(p1);
  129. p2 = toupper(p2);
  130. if ( p1 == 'P' ) {
  131. if ( p2 == 'P' ) return 0;
  132. else if (p2 == 'R') return 1;
  133. else return 2;
  134. }
  135. else if (p1 == 'R') {
  136. if ( p2 == 'R' ) return 0;
  137. else if (p2 == 'S') return 1;
  138. else return 2;
  139. }
  140. else {
  141. if ( p2 == 'S' ) return 0;
  142. else if (p2 == 'P') return 1;
  143. else return 2;
  144. }
  145. }
  146. int RPSBeta(char p1, char p2) {
  147. p1 = toupper(p1);
  148. p2 = toupper(p2);
  149. if ( p1 == 'P' ) {
  150. if ( p2 == 'S' ) return 0;
  151. else if (p2 == 'R') return 1;
  152. else return 2;
  153. }
  154. else if (p1 == 'R') {
  155. if ( p2 == 'S' ) return 0;
  156. else if (p2 == 'P') return 1;
  157. else return 2;
  158. }
  159. else {
  160. if ( p2 == 'S' ) return 0;
  161. else if (p2 == 'R') return 1;
  162. else return 2;
  163. }
  164. }
  165. int RPSGamma(char p1, char p2) {
  166. p1 = toupper(p1);
  167. p2 = toupper(p2);
  168. if ( p1 == 'P' ) {
  169. if ( p2 == 'P' ) return 0;
  170. else if (p2 == 'S') return 1;
  171. else return 2;
  172. }
  173. else if (p1 == 'R') {
  174. if ( p2 == 'R' ) return 0;
  175. else if (p2 == 'P') return 1;
  176. else return 2;
  177. }
  178. else {
  179. if ( p2 == 'P' ) return 0;
  180. else if (p2 == 'S') return 1;
  181. else return 2;
  182. }
  183. }
  184. int RPSDelta(char p1, char p2) {
  185. p1 = toupper(p1);
  186. p2 = toupper(p2);
  187. if ( p1 == 'P' ) {
  188. if ( p2 == 'P' ) return 0;
  189. else if (p2 == 'S') return 1;
  190. else return 2;
  191. }
  192. else if (p1 == 'R') {
  193. if ( p2 == 'R' ) return 0;
  194. else if (p2 == 'P') return 1;
  195. else return 2;
  196. }
  197. else {
  198. if ( p2 == 'P' ) return 0;
  199. else if (p2 == 'S') return 1;
  200. else return 2;
  201. }
  202. }
  203. // =========================================================
  204. // Check Words Functions
  205. // =========================================================
  206. QMap<int,QString> M;
  207. ///
  208. /// \brief initCheckWMaps: initialize the values in the dictionary that is used throughout
  209. /// the check functions.
  210. ///
  211. void initCheckWMaps() {
  212. M[1] = "one"; M[2] = "two"; M[3] = "three"; M[4] = "four";
  213. M[5] = "five"; M[6] = "six";
  214. M[7] = "seven"; M[8] = "eight"; M[9] = "nine"; M[10] = "ten";
  215. M[11] = "eleven"; M[12] = "twelve"; M[13] = "thirteen"; M[14] = "fourteen";
  216. M[15] = "fifteen"; M[16] = "sixteen"; M[17] = "seventeen"; M[18] = "eighteen";
  217. M[19] = "nineteen";
  218. M[20] = "twenty"; M[30] = "thirty"; M[40] = "fourty"; M[50] = "fifty";
  219. M[60] = "sixty"; M[70] = "seventy"; M[80] = "eighty"; M[90] = "ninety";
  220. }
  221. ///
  222. /// \brief validateCheckQty: determines is entered text is a valid number in [0,999999999]
  223. /// \param st text entered by the user
  224. /// \param qty text converted to integer
  225. /// \return true if the entered text is valid
  226. ///
  227. bool validateCheckQty(QString st, unsigned int &qty) {
  228. int i = 0;
  229. for (i = 0; i < st.length() ; i++) {
  230. if (!st[i].isDigit()) return false;
  231. }
  232. if (i > 9) return false;
  233. qty = st.toInt();
  234. return true;
  235. }
  236. ///
  237. /// \brief wordForNumber: Given a number n in [0,999] returns the word equivalent
  238. /// \param n: the number
  239. /// \return a Qstring containing the number in words
  240. ///
  241. QString wordForNumber(int n) {
  242. QString st;
  243. int tens = n % 100;
  244. if (tens == 0)
  245. st = "";
  246. else if (tens <= 20)
  247. st = M[n];
  248. else {
  249. st = M[10 * (tens/10)];
  250. if (tens % 10)
  251. st.append(" " + M[tens % 10]);
  252. }
  253. n = n / 100;
  254. if (n) st.prepend(M[n % 10] + " hundred" + (st.length() > 0 ? " " : ""));
  255. return st;
  256. }
  257. ///
  258. /// \brief checkWAlpha: One of the functions to convert a number in [0,999999999] to words
  259. /// \param n: the number
  260. /// \return a Qstring containing the number in words
  261. ///
  262. QString checkWAlpha(int n) {
  263. QString st;
  264. // the cents
  265. st = wordForNumber(n % 1000);
  266. // the thousands
  267. n = n / 1000;
  268. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  269. // the millions
  270. n = n / 1000;
  271. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  272. return st;
  273. }
  274. ///
  275. /// \brief checkWBeta: One of the functions to convert a number in [0,999999999] to words
  276. /// \param n: the number
  277. /// \return a Qstring containing the number in words
  278. ///
  279. QString checkWBeta(int n) {
  280. QString st;
  281. st = wordForNumber(n % 1000);
  282. n = n / 1000;
  283. if (n % 1000) st.append( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  284. n = n / 1000;
  285. if (n % 1000) st.append( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  286. return st;
  287. }
  288. ///
  289. /// \brief checkWGamma: One of the functions to convert a number in [0,999999999] to words
  290. /// \param n: the number
  291. /// \return a Qstring containing the number in words
  292. ///
  293. QString checkWGamma(int n) {
  294. QString st;
  295. st = wordForNumber(n % 10);
  296. n = n / 1000;
  297. if (n % 1000) st.append( wordForNumber(n % 10) + " thousand" + (st.length() > 0 ? " " : ""));
  298. n = n / 1000;
  299. if (n % 1000) st.append( wordForNumber(n % 10) + " million" + (st.length() > 0 ? " " : ""));
  300. return st;
  301. }
  302. ///
  303. /// \brief checkWDelta: One of the functions to convert a number in [0,999999999] to words
  304. /// \param n: the number
  305. /// \return a Qstring containing the number in words
  306. ///
  307. QString checkWDelta(int n) {
  308. QString st;
  309. n /= 10;
  310. st = wordForNumber(n % 1000);
  311. n = n / 1000;
  312. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  313. n = n / 1000;
  314. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  315. return st;
  316. }
  317. // =========================================================
  318. // Zulu Functions
  319. // =========================================================
  320. bool validZuluTime(const QString &time, const QString &zone, int &hours, int &minutes) {
  321. int i = 0;
  322. for (i = 0; i< time.size(); i++)
  323. if (!time[i].isDigit()) return false;
  324. if (i != 4) return false;
  325. hours = time.mid(0,2).toInt();
  326. minutes = time.mid(2,2).toInt();
  327. if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59)
  328. return false;
  329. if (zone.length() < 1 || !zone[0].isLetter() || toupper(zone.toStdString()[0]) == 'J') 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. return QString::number(hours) + QString::number(minutes);
  355. }
  356. QString zuluGamma(int hours, int minutes, char zone) {
  357. int diff = 0;
  358. if (zone <= 'I') diff = zone - 'A' + 1;
  359. else if (zone <= 'M') diff = 10 + (zone - 'M');
  360. else if (zone <= 'Y') diff = -(zone - 'N');
  361. else diff = 0;
  362. hours = (hours + (24 - diff) ) % 24;
  363. return QString::number(hours) + QString::number(minutes);
  364. }
  365. QString zuluDelta(int hours, int minutes, char zone) {
  366. int diff = 0;
  367. if (zone <= 'M') diff = zone - 'B' + 1;
  368. else if (zone <= 'Y') diff = -(zone - 'N');
  369. else diff = 0;
  370. hours = (hours + (24 - diff) ) % 24;
  371. return QString::number(hours) + QString::number(minutes);
  372. }
  373. // =========================================================
  374. // APFT Functions
  375. // =========================================================
  376. //This it the correct one
  377. void MainWindow::APFTAlpha(){
  378. //For each one of these variables we apply the formula that they use
  379. //to set the points
  380. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  381. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  382. double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
  383. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  384. //Since the maximum of points that one can obtain
  385. //on each excercise type is 100, we restrict it
  386. if (sit_ups > 100){
  387. sit_ups = 100;
  388. }
  389. else if (sit_ups < 0){
  390. sit_ups = 0;
  391. }
  392. if (push_ups > 100){
  393. push_ups = 100;
  394. }
  395. else if (push_ups < 0){
  396. push_ups = 0;
  397. }
  398. if (two_mile_run > 100){
  399. two_mile_run = 100;
  400. }
  401. else if (two_mile_run < 0){
  402. two_mile_run = 0;
  403. }
  404. //Finally we set the outputs.
  405. line[3]->setText(QString::number(sit_ups));
  406. line[4]->setText(QString::number(push_ups));
  407. line[5]->setText(QString::number(two_mile_run));
  408. line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
  409. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  410. label[7]->setText("PASS");
  411. }
  412. else{
  413. label[7]->setText("FAIL");
  414. }
  415. }
  416. //This one do not check if the number is greater than 100
  417. void MainWindow::APFTBeta(){
  418. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  419. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  420. double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
  421. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  422. line[3]->setText(QString::number(sit_ups));
  423. line[4]->setText(QString::number(push_ups));
  424. line[5]->setText(QString::number(two_mile_run));
  425. line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
  426. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  427. line[7]->setText("PASS");
  428. }
  429. else{
  430. line[7]->setText("FAIL");
  431. }
  432. }
  433. //This one switch the results
  434. void MainWindow::APFTGamma(){
  435. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  436. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  437. double running_time = qFloor((line[2]->text().left(2).append('.' + line[2]->text().right(2))).toDouble());
  438. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  439. if (sit_ups > 100){
  440. sit_ups = 100;
  441. }
  442. else if (sit_ups < 0){
  443. sit_ups = 0;
  444. }
  445. if (push_ups > 100){
  446. push_ups = 100;
  447. }
  448. else if (push_ups < 0){
  449. push_ups = 0;
  450. }
  451. if (two_mile_run > 100){
  452. two_mile_run = 100;
  453. }
  454. else if (two_mile_run < 0){
  455. two_mile_run = 0;
  456. }
  457. line[5]->setText(QString::number(sit_ups));
  458. line[3]->setText(QString::number(push_ups));
  459. line[4]->setText(QString::number(two_mile_run));
  460. line[6]->setText(QString::number(push_ups + sit_ups + two_mile_run));
  461. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  462. line[7]->setText("PASS");
  463. }
  464. else{
  465. line[7]->setText("FAIL");
  466. }
  467. }
  468. //This one do not sums the points but the quantities of each excercise
  469. void MainWindow::APFTDelta(){
  470. double sit_ups = qFloor(line[0]->text().toDouble() * 1.6 - 24.8);
  471. double push_ups = qFloor(line[1]->text().toInt() * 1.42);
  472. double running_time = qFloor((line[2]->text().left(2).append('.'
  473. + line[2]->text().right(2))).toDouble());
  474. double two_mile_run = qFloor((820 / 3) - ((40 * running_time) / 3));
  475. if (sit_ups > 100){
  476. sit_ups = 100;
  477. }
  478. else if (sit_ups < 0){
  479. sit_ups = 0;
  480. }
  481. if (push_ups > 100){
  482. push_ups = 100;
  483. }
  484. else if (push_ups < 0){
  485. push_ups = 0;
  486. }
  487. if (two_mile_run > 100){
  488. two_mile_run = 100;
  489. }
  490. else if (two_mile_run < 0){
  491. two_mile_run = 0;
  492. }
  493. int sumaTo = line[0]->text().toInt() + line[1]->text().toInt() + running_time;
  494. line[3]->setText(QString::number(sit_ups));
  495. line[4]->setText(QString::number(push_ups));
  496. line[5]->setText(QString::number(two_mile_run));
  497. line[6]->setText(QString::number(sumaTo));
  498. if (push_ups >= 60 && sit_ups >= 60 && two_mile_run >= 60){
  499. line[7]->setText("PASS");
  500. }
  501. else{
  502. line[7]->setText("FAIL");
  503. }
  504. }