Aucune description

functions.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. // Rock Paper Scissor Functions
  103. // =========================================================
  104. int RPSAlpha(char p1, char p2) {
  105. p1 = toupper(p1);
  106. p2 = toupper(p2);
  107. if ( p1 == 'P' ) {
  108. if ( p2 == 'P' ) return TIE;
  109. else if (p2 == 'R') return P1_WON;
  110. else return P2_WON;
  111. }
  112. else if (p1 == 'R') {
  113. if ( p2 == 'R' ) return TIE;
  114. else if (p2 == 'S') return P1_WON;
  115. else return P2_WON;
  116. }
  117. else {
  118. if ( p2 == 'S' ) return TIE;
  119. else if (p2 == 'P') return P1_WON;
  120. else return P2_WON;
  121. }
  122. }
  123. int RPSBeta(char p1, char p2) {
  124. p1 = toupper(p1);
  125. p2 = toupper(p2);
  126. if ( p1 == 'P' ) {
  127. if ( p2 == 'S' ) return TIE;
  128. else if (p2 == 'R') return P1_WON;
  129. else return P2_WON;
  130. }
  131. else if (p1 == 'R') {
  132. if ( p2 == 'S' ) return TIE;
  133. else if (p2 == 'P') return P1_WON;
  134. else return P2_WON;
  135. }
  136. else {
  137. if ( p2 == 'S' ) return TIE;
  138. else if (p2 == 'R') return P1_WON;
  139. else return P2_WON;
  140. }
  141. }
  142. int RPSGamma(char p1, char p2) {
  143. p1 = toupper(p1);
  144. p2 = toupper(p2);
  145. if ( p1 == 'P' ) {
  146. if ( p2 == 'P' ) return TIE;
  147. else if (p2 == 'S') return P1_WON;
  148. else return P2_WON;
  149. }
  150. else if (p1 == 'R') {
  151. if ( p2 == 'R' ) return TIE;
  152. else if (p2 == 'P') return P1_WON;
  153. else return P2_WON;
  154. }
  155. else {
  156. if ( p2 == 'P' ) return TIE;
  157. else if (p2 == 'S') return P1_WON;
  158. else return P2_WON;
  159. }
  160. }
  161. int RPSDelta(char p1, char p2) {
  162. p1 = toupper(p1);
  163. p2 = toupper(p2);
  164. if ( p1 == 'P' ) {
  165. if ( p2 == 'P' ) return TIE;
  166. else if (p2 == 'S') return P1_WON;
  167. else return P2_WON;
  168. }
  169. else if (p1 == 'R') {
  170. if ( p2 == 'R' ) return TIE;
  171. else if (p2 == 'P') return P1_WON;
  172. else return P2_WON;
  173. }
  174. else {
  175. if ( p2 == 'P' ) return TIE;
  176. else if (p2 == 'S') return P1_WON;
  177. else return P2_WON;
  178. }
  179. }
  180. // =========================================================
  181. // Check Words Functions
  182. // =========================================================
  183. QMap<int,QString> M;
  184. void initCheckWMaps() {
  185. M[1] = "one"; M[2] = "two"; M[3] = "three"; M[4] = "four";
  186. M[5] = "five"; M[6] = "six";
  187. M[7] = "seven"; M[8] = "eight"; M[9] = "nine"; M[10] = "ten";
  188. M[11] = "eleven"; M[12] = "twelve"; M[13] = "thirteen"; M[14] = "fourteen";
  189. M[15] = "fifteen"; M[16] = "sixteen"; M[17] = "seventeen"; M[18] = "eighteen";
  190. M[19] = "nineteen";
  191. M[20] = "twenty"; M[30] = "thirty"; M[40] = "fourty"; M[50] = "fifty";
  192. M[60] = "sixty"; M[70] = "seventy"; M[80] = "eighty"; M[90] = "ninety";
  193. }
  194. bool validateCheckQty(QString st, unsigned int &qty) {
  195. int i = 0;
  196. for (i = 0; i < st.length() ; i++) {
  197. if (!st[i].isDigit()) return false;
  198. }
  199. if (i > 9) return false;
  200. qty = st.toInt();
  201. return true;
  202. }
  203. QString wordForNumber(unsigned int n) {
  204. QString st;
  205. int tens = n % 100;
  206. if (tens == 0)
  207. st = "";
  208. else if (tens <= 20)
  209. st = M[n];
  210. else {
  211. st = M[10 * (tens/10)];
  212. if (tens % 10)
  213. st.append(" " + M[tens % 10]);
  214. }
  215. n = n / 100;
  216. if (n) st.prepend(M[n % 10] + " hundred" + (st.length() > 0 ? " " : ""));
  217. return st;
  218. }
  219. QString checkWAlpha(unsigned int n) {
  220. QString st;
  221. // the cents
  222. st = wordForNumber(n % 1000);
  223. // the thousands
  224. n = n / 1000;
  225. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  226. // the millions
  227. n = n / 1000;
  228. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  229. return st;
  230. }
  231. QString checkWBeta(unsigned int n) {
  232. QString st;
  233. st = wordForNumber(n % 1000);
  234. n = n / 1000;
  235. if (n % 1000) st.append( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  236. n = n / 1000;
  237. if (n % 1000) st.append( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  238. return st;
  239. }
  240. QString checkWGamma(unsigned int n) {
  241. QString st;
  242. st = wordForNumber(n % 10);
  243. n = n / 1000;
  244. if (n % 1000) st.append( wordForNumber(n % 10) + " thousand" + (st.length() > 0 ? " " : ""));
  245. n = n / 1000;
  246. if (n % 1000) st.append( wordForNumber(n % 10) + " million" + (st.length() > 0 ? " " : ""));
  247. return st;
  248. }
  249. QString checkWDelta(unsigned int n) {
  250. QString st;
  251. n /= 10;
  252. st = wordForNumber(n % 1000);
  253. n = n / 1000;
  254. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " thousand" + (st.length() > 0 ? " " : ""));
  255. n = n / 1000;
  256. if (n % 1000) st.prepend( wordForNumber(n % 1000) + " million" + (st.length() > 0 ? " " : ""));
  257. return st;
  258. }
  259. // =========================================================
  260. // Zulu Functions
  261. // =========================================================
  262. bool validZuluTime(const QString &time, const QString &zone, int &hours, int &minutes) {
  263. int i = 0;
  264. for (i = 0; i< time.size(); i++)
  265. if (!time[i].isDigit()) return false;
  266. if (i != 4) return false;
  267. hours = time.mid(0,2).toInt();
  268. minutes = time.mid(2,2).toInt();
  269. if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59)
  270. return false;
  271. if (zone.length() < 1 || !zone[0].isLetter() || toupper(zone.toStdString()[0]) == 'J') return false;
  272. return true;
  273. }
  274. QString zuluAlpha(int hours, int minutes, char zone) {
  275. int diff = 0;
  276. zone = toupper(zone);
  277. if (zone <= 'I') diff = zone - 'A' + 1;
  278. else if (zone <= 'M') diff = 10 + (zone - 'K');
  279. else if (zone <= 'Y') diff = -(zone - 'N' + 1);
  280. else diff = 0;
  281. hours = (hours + (24 + diff) ) % 24;
  282. QString qstHours = QString::number(hours);
  283. if (qstHours.length() == 1) qstHours.prepend("0");
  284. QString qstMin = QString::number(minutes);
  285. if (qstMin.length() == 1) qstMin.prepend("0");
  286. return qstHours + qstMin;
  287. }
  288. QString zuluBeta(int hours, int minutes, char zone) {
  289. int diff = 0;
  290. if (zone <= 'I') diff = zone - 'B' + 1;
  291. else if (zone <= 'M') diff = 10 + (zone - 'M');
  292. else if (zone <= 'Y') diff = -(zone - 'N');
  293. diff = 0;
  294. hours = (hours + (24 + diff) ) % 24;
  295. return QString::number(hours) + QString::number(minutes);
  296. }
  297. QString zuluGamma(int hours, int minutes, char zone) {
  298. int diff = 0;
  299. if (zone <= 'I') diff = zone - 'A' + 1;
  300. else if (zone <= 'M') diff = 10 + (zone - 'M');
  301. else if (zone <= 'Y') diff = -(zone - 'N');
  302. else diff = 0;
  303. hours = (hours + (24 - diff) ) % 24;
  304. return QString::number(hours) + QString::number(minutes);
  305. }
  306. QString zuluDelta(int hours, int minutes, char zone) {
  307. int diff = 0;
  308. if (zone <= 'M') diff = zone - 'B' + 1;
  309. else if (zone <= 'Y') diff = -(zone - 'N');
  310. else diff = 0;
  311. hours = (hours + (24 - diff) ) % 24;
  312. return QString::number(hours) + QString::number(minutes);
  313. }