Guest User

Untitled

a guest
Jan 12th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdio>
  4. #include <sstream>
  5. #include <stdexcept>
  6. #include <limits>
  7.  
  8. using namespace std;
  9.  
  10. #define JAN "January"
  11. #define FEB "February"
  12. #define MAR "March"
  13. #define APR "April"
  14. #define MAY "May"
  15. #define JUN "June"
  16. #define JUL "July"
  17. #define AUG "August"
  18. #define SEP "September"
  19. #define OCT "October"
  20. #define NOV "November"
  21. #define DEC "December"
  22.  
  23. #define SUN "Sun"
  24. #define MON "Mon"
  25. #define TUE "Tue"
  26. #define WED "Wed"
  27. #define THU "Thu"
  28. #define FRI "Fri"
  29. #define SAT "Sat"
  30.  
  31. enum Month
  32. {
  33. Jan = 1,
  34. Feb,
  35. Mar,
  36. Apr,
  37. May,
  38. Jun,
  39. Jul,
  40. Aug,
  41. Sep,
  42. Oct,
  43. Nov,
  44. Dec
  45. };
  46.  
  47. enum Day
  48. {
  49. Sun = 0,
  50. Mon,
  51. Tue,
  52. Wed,
  53. Thu,
  54. Fri,
  55. Sat
  56. };
  57.  
  58. class Date
  59. {
  60. public:
  61. class Invalid : public runtime_error
  62. {
  63. public:
  64. Invalid() : runtime_error("Invalid date") {}
  65. };
  66.  
  67. Date() : y(2001), m(Jan), d(1), days_in_month(31) {}
  68. Date(int yy, Month mm, int dd) : y(yy), m(mm), d(dd)
  69. {
  70. if (!is_valid(yy, mm, dd))
  71. {
  72. throw Invalid();
  73. }
  74. }
  75.  
  76. int day() const { return d; }
  77. Month month() const { return m; }
  78. int year() const { return y; }
  79. int days() const { return days_in_month; }
  80.  
  81. void add_day();
  82. void add_month();
  83. void add_year(int n);
  84.  
  85. private:
  86. int y;
  87. Month m;
  88. int d;
  89. int days_in_month;
  90.  
  91. bool is_valid(int y, Month m, int d);
  92. bool leapyear(int y)
  93. {
  94. return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  95. }
  96. };
  97.  
  98. bool Date::is_valid(int y, Month m, int d)
  99. {
  100. if (y < 1583)
  101. return false; // gregorian start
  102. if (d < 0)
  103. return false;
  104. if (m < Jan || Dec < m)
  105. return false;
  106.  
  107. days_in_month = 31;
  108.  
  109. switch (m)
  110. {
  111. case Feb:
  112. days_in_month = (leapyear(y)) ? 29 : 28;
  113. break;
  114. case Apr:
  115. case Jun:
  116. case Sep:
  117. case Nov:
  118. days_in_month = 30;
  119. break;
  120. }
  121.  
  122. if (days_in_month < d)
  123. return false;
  124.  
  125. return true;
  126. }
  127.  
  128. void Date::add_day()
  129. {
  130. d++;
  131.  
  132. if (d > days_in_month)
  133. {
  134. d = 1;
  135. add_month();
  136. }
  137. }
  138.  
  139. void Date::add_month()
  140. {
  141. if (m == Dec)
  142. {
  143. m = Jan;
  144. add_year(1);
  145. }
  146. else if (m == Jan && d == 29 && !leapyear(y))
  147. {
  148. m = (Month)((int)m + 1);
  149. d = 28;
  150. }
  151. else
  152. {
  153. m = (Month)((int)m + 1);
  154. }
  155.  
  156. if (!is_valid(y, m, d))
  157. {
  158. throw Invalid();
  159. }
  160. }
  161.  
  162. void Date::add_year(int n)
  163. {
  164. if (m == Feb && d == 29 && !leapyear(y + n))
  165. {
  166. m = Mar;
  167. d = 1;
  168. }
  169. y += n;
  170. }
  171.  
  172. tm makeTM(Date date)
  173. {
  174. tm get_tm = {};
  175. int d = date.day();
  176. int m = (int)date.month();
  177. int y = date.year();
  178.  
  179. get_tm.tm_year = y - 1900;
  180. get_tm.tm_mon = m - 1;
  181. get_tm.tm_mday = d;
  182.  
  183. if (mktime(&get_tm) == -1) {
  184. cerr << "Error: Invalid date" << endl;
  185. exit(1);
  186. }
  187.  
  188. return get_tm;
  189. }
  190.  
  191. static int stoi(string &s)
  192. {
  193. int i;
  194. istringstream(s) >> i;
  195. if (istringstream(s).fail()) {
  196. cerr << "Error: Invalid string to int conversion" << endl;
  197. exit(1);
  198. }
  199. return i;
  200. }
  201.  
  202. int getDayOfWeek(const Date date)
  203. {
  204. tm get_day = makeTM(date);
  205. return get_day.tm_wday;
  206. }
  207.  
  208. string printMonth(Date &d)
  209. {
  210. switch (d.month())
  211. {
  212. case Jan: return JAN;
  213. case Feb: return FEB;
  214. case Mar: return MAR;
  215. case Apr: return APR;
  216. case May: return MAY;
  217. case Jun: return JUN;
  218. case Jul: return JUL;
  219. case Aug: return AUG;
  220. case Sep: return SEP;
  221. case Oct: return OCT;
  222. case Nov: return NOV;
  223. case Dec: return DEC;
  224. default: return "mmm";
  225. }
  226. }
  227.  
  228. void printHeader(const Date date)
  229. {
  230. cout << "==========" << endl;
  231. tm title = makeTM(date);
  232. cout << asctime(&title);
  233. cout << "==========" << endl;
  234.  
  235. printf("|");
  236. printf("%-3s|", SUN);
  237. printf("%-3s|", MON);
  238. printf("%-3s|", TUE);
  239. printf("%-3s|", WED);
  240. printf("%-3s|", THU);
  241. printf("%-3s|", FRI);
  242. printf("%-3s|", SAT);
  243. printf("\n\n");
  244. }
  245.  
  246. void printWeek(Date date)
  247. {
  248. Date begin(date.year(), date.month(), 1);
  249. int first_day = getDayOfWeek(begin);
  250. Day day = (Day)((int)first_day);
  251.  
  252. printf("|");
  253.  
  254. for (int i = 0; i < first_day; ++i)
  255. {
  256. printf("%-3s|", "X");
  257. }
  258.  
  259. for (int i = 0; i < date.days(); ++i)
  260. {
  261. if (i + 1 == date.day())
  262. {
  263. printf("%-3s|", "_");
  264. }
  265. else
  266. {
  267. printf("%-3i|", begin.day());
  268. }
  269.  
  270. switch (day)
  271. {
  272. case Sun: day = Mon; break;
  273. case Mon: day = Tue; break;
  274. case Tue: day = Wed; break;
  275. case Wed: day = Thu; break;
  276. case Thu: day = Fri; break;
  277. case Fri: day = Sat; break;
  278. case Sat:
  279. day = Sun;
  280. printf("\n|");
  281. break;
  282. }
  283.  
  284. begin.add_day();
  285. }
  286.  
  287. Date finish(date.year(), date.month(), date.days());
  288. int zero_sunday = 6;
  289. int end = zero_sunday - getDayOfWeek(finish);
  290.  
  291. for (int i = 0; i < end; ++i)
  292. {
  293. printf("%-3s|", "X");
  294. }
  295. }
  296.  
  297. int getValidInput(const string &prompt, int min, int max)
  298. {
  299. int value;
  300. int attempts = 0;
  301. while (attempts < 3)
  302. {
  303. cout << prompt;
  304. string input;
  305. getline(cin, input);
  306.  
  307. if (input.empty())
  308. {
  309. cout << "Input cannot be empty. Please try again." << endl;
  310. attempts++;
  311. continue;
  312. }
  313.  
  314. istringstream iss(input);
  315. if (!(iss >> value) || value < min || value > max)
  316. {
  317. cout << "Invalid input. Please try again." << endl;
  318. attempts++;
  319. }
  320. else
  321. {
  322. return value;
  323. }
  324. }
  325.  
  326. cout << "You have entered invalid numbers consecutively three times. The application will now close after pressing Enter." << endl;
  327. cin.get(); // Wait for the user to press Enter
  328. exit(1);
  329. }
  330.  
  331. int main()
  332. {
  333. int year = getValidInput("Enter the year (1583-9999): ", 1583, 9999);
  334. int month = getValidInput("Enter the month (1-12): ", 1, 12);
  335. int day = getValidInput("Enter the day (1-31): ", 1, 31);
  336.  
  337. try {
  338. Date date(year, (Month)month, day);
  339. printHeader(date);
  340. printWeek(date);
  341. } catch (const Date::Invalid &e) {
  342. cerr << "Error: " << e.what() << endl;
  343. return 1;
  344. } catch (const exception &e) {
  345. cerr << "Error: " << e.what() << endl;
  346. return 1;
  347. }
  348.  
  349. cout << "\n\nPress Enter to continue...";
  350. cin.get(); // Wait for the user to press Enter
  351.  
  352. return 0;
  353. }
  354.  
Advertisement
Add Comment
Please, Sign In to add comment