Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct dateTime
  7. {
  8. int day;
  9. int month;
  10. int year;
  11. string date;
  12.  
  13. private:
  14. int mLength[13]{ 1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  15. string week[7]{ "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
  16. string monthNames[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  17. string parsedInt;
  18. string parsedString;
  19.  
  20. public:
  21.  
  22. void setData(int d, int m, int y) {
  23. day = d;
  24. month = m;
  25. year = y;
  26. date = to_string(day) + "/" + to_string(month) + "/" + to_string(year);
  27. }
  28.  
  29. string getData() {
  30. return date;
  31. }
  32.  
  33. bool leapy(int d, int m, int y) {
  34. if (y % 4 == 0) {
  35. if (y % 100 != 0) {
  36. if (m > 2) {
  37. return true;
  38. }
  39. else if (m == 2 && d == 29) {
  40. return true;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46. }
  47. else {
  48. if (y % 400 == 0) {
  49. if (m > 2) {
  50. return true;
  51. }
  52. else if (m == 2 && d == 29) {
  53. return true;
  54. }
  55. else {
  56. return false;
  57. }
  58. }
  59. else {
  60. return false;
  61. }
  62. }
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. }
  69.  
  70. int dofy(int d, int m, int y) {
  71. int th = 0;
  72.  
  73. for (int i = 1; i < m; i++) {
  74. th += mLength[i];
  75. }
  76.  
  77. th += d;
  78.  
  79. if (leapy(d, m, y)) {
  80. th += mLength[0];
  81. }
  82.  
  83. return th;
  84. }
  85.  
  86. int dayOfYear() {
  87. int dayCount = dofy(day, month, year);
  88.  
  89. return dayCount;
  90. }
  91.  
  92. bool compareTo(dateTime dt) {
  93. if (year > dt.year) {
  94. return true;
  95. }
  96. else if (year == dt.year) {
  97. if (month > dt.month) {
  98. return true;
  99. }
  100. else if (month == dt.month) {
  101. if (day >= dt.day) {
  102. return true;
  103. }
  104. else {
  105. return false;
  106. }
  107. }
  108. else {
  109. return false;
  110. }
  111. }
  112. else {
  113. return false;
  114. }
  115. }
  116.  
  117. int getTimeSpam(dateTime dt) {
  118. int dayCount = 0;
  119. if (compareTo(dt)) {
  120. if (year > dt.year) {
  121. dayCount += dt.dofy(31, 12, dt.year) - dt.dayOfYear();
  122. dayCount += dayOfYear();
  123. }
  124. else {
  125. dayCount += dayOfYear() - 1;
  126. }
  127. int yy = dt.year + 1;
  128. while (yy < year) {
  129. dayCount += dt.dofy(31, 12, yy);
  130. yy++;
  131. }
  132.  
  133.  
  134. return dayCount;
  135. }
  136. else {
  137. dayCount += dt.dayOfYear();
  138. int yy = dt.year - 1;
  139. while (yy > year) {
  140. dayCount += dt.dofy(31, 12, yy);
  141. yy--;
  142. }
  143.  
  144. dayCount += dofy(31, 12, year) - dayOfYear();
  145. return dayCount;
  146. }
  147. }
  148.  
  149. string dayOfWeek() {
  150.  
  151. //2018 1 jan Monday
  152. dateTime dt;
  153. dt.setData(1, 1, 2018);
  154.  
  155. int dayCount = getTimeSpam(dt);
  156.  
  157. while (dayCount > 6) {
  158. dayCount -= 7;
  159. }
  160.  
  161. if (compareTo(dt)) {
  162. return week[dayCount];
  163. }
  164. else {
  165. if (6 - dayCount + 1 < 7) {
  166. return week[6 - dayCount + 1];
  167. }
  168. else {
  169. return week[0];
  170. }
  171.  
  172. }
  173.  
  174. }
  175.  
  176. string getMonth() {
  177. return monthNames[month - 1];
  178. }
  179.  
  180. string getFullDateString() {
  181. return dayOfWeek() + "/" + getMonth() + "/" + to_string(year);
  182. }
  183.  
  184. int checkDL(int d, int m, int y) {
  185. if (m == 2) {
  186. if (leapy(d, m, y)) {
  187. return mLength[m] + mLength[0];
  188. }
  189. else {
  190. return mLength[m];
  191. }
  192. }
  193. else
  194. {
  195. return mLength[m];
  196. }
  197. }
  198.  
  199. int dateTester(int d, int m, int y) {
  200.  
  201. if (0 < m && m < 13) {
  202. if (0 < d && d <= checkDL(d, m, y)) {
  203. return 0;
  204. }
  205. else {
  206. return 1;
  207. }
  208. }
  209. else {
  210. return 2;
  211. }
  212.  
  213.  
  214. }
  215.  
  216. bool stringTester(string month) {
  217. int stringCount = 0;
  218. int intCount = 0;
  219.  
  220. for (int i = 0; i < month.length(); i++) {
  221. if (48 <= month[i] && month[i] <= 57) {
  222. intCount++;
  223. parsedInt += month[i];
  224. }
  225. else
  226. {
  227. stringCount++;
  228. parsedString += month[i];
  229. }
  230. }
  231.  
  232. if (intCount >= stringCount) {
  233. return true;
  234. }
  235. else {
  236. return false;
  237. }
  238. }
  239.  
  240. int stringToInt(string toInt) {
  241. int m = 0;
  242. int tenth = 1;
  243.  
  244. for (int i = toInt.length() - 1; i >= 0; i--) {
  245. m += (toInt[i] - '0') * tenth;
  246. tenth *= 10;
  247. }
  248.  
  249. return m;
  250. }
  251.  
  252. int getMonthString() {
  253. int hit[12]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 };
  254. int miss[12]{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 };
  255. int maxIndex = 0;
  256.  
  257. //parsedString
  258.  
  259. for (int i = 0; i < parsedString.length(); i++) {
  260. for (int j = 0; j < 12; j++) {
  261. for (int k = 0; k < monthNames[j].length(); k++) {
  262. if (parsedString[i] == monthNames[j][k]) {
  263. hit[j]++;
  264. }
  265. }
  266. }
  267. }
  268.  
  269. for(int i = 0; i < 12; i++) {
  270. miss[i] = hit[i] * 100 / (parsedString.length() * monthNames[i].length());
  271. if (miss[i] > miss[maxIndex]) {
  272. maxIndex = i;
  273. }
  274. }
  275. return maxIndex;
  276. }
  277.  
  278. int chooseFormat(int d, string mm, int y) {
  279. int monthCalculated;
  280.  
  281. if (stringTester(mm)) {
  282. monthCalculated = stringToInt(parsedInt);
  283. }
  284. else {
  285. monthCalculated = getMonthString() + 1;
  286. }
  287.  
  288. return monthCalculated;
  289. }
  290. };
  291.  
  292.  
  293. int main() {
  294. int d, m, y;
  295. string month;
  296. bool run = true;
  297.  
  298. cout << "Input day: ";
  299. cin >> d;
  300. cin.clear();
  301. cin.ignore(100, '\n');
  302. cout << "Input month: ";
  303. cin >> month;
  304. cout << "Input year: ";
  305. cin >> y;
  306. cin.clear();
  307. cin.ignore(100, '\n');
  308. cout << endl;
  309.  
  310. while (run) {
  311. dateTime dt;
  312. m = dt.chooseFormat(d, month, y);
  313.  
  314. if (dt.dateTester(d, m, y) == 0) {
  315. dt.setData(d, m, y);
  316. cout << dt.getData() << endl;
  317. cout << "Day of year: " << dt.dayOfYear() << endl;
  318. cout << "Day of week: " << dt.dayOfWeek() << endl;
  319. cout << "Month: " << dt.getMonth() << endl;
  320. cout << dt.getFullDateString() << endl;
  321. run = false;
  322. }
  323. else if (dt.dateTester(d, m, y) == 1) {
  324. cout << "Incorrect day format... Please enter integer(1-28/29, 1-30/31): ";
  325. cin >> d;
  326. cout << endl;
  327. }
  328. else if (dt.dateTester(d, m, y) == 2) {
  329. cout << "Incorrect month format... Please enter integer(1-12) or month name: ";
  330. cin >> month;
  331. cout << endl;
  332. }
  333. else if (dt.dateTester(d, m, y) == 3){
  334. cout << "Incorrect year format... Please enter integer: ";
  335. cin >> y;
  336. cout << endl;
  337. }
  338. }
  339.  
  340. return 0;
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement