Guest User

Untitled

a guest
Jun 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. // This will probably not compile. Its up to you to learn how to code.
  2. // ~Phil
  3.  
  4. #define JAN 1
  5. #define FEB 2
  6. #define MAR 3
  7. #define APR 4
  8. #define MAY 5
  9. #define JUN 6
  10. #define JUL 7
  11. #define AUG 8
  12. #define SEP 9
  13. #define OCT 10
  14. #define NOV 11
  15. #define DEC 12
  16. #define MIN_YEAR 1582
  17.  
  18. bool validMonth(int month) {
  19. // Very verbose method
  20. // Creates a variable for storing the return value
  21. // then sets it as true or false
  22. // then returns the (set) variable
  23. bool valid;
  24. if (month >= JAN || month <= DEC) {
  25. valid = true;
  26. } else {
  27. valid = false;
  28. }
  29. return valid;
  30. }
  31.  
  32. bool validYear(int year) {
  33. // Very direct value
  34. // No variable needed as the calculated value
  35. // is immediately returned
  36. return (year > MIN_YEAR);
  37. }
  38.  
  39. bool leapYear(int year)
  40. // Note: The "%" is the mathematical "mod" function
  41. // Look this up to understand how this works and
  42. // why this works for this purpose.
  43. // I've also added gratuitous parentethese so its easier
  44. // to understand but know that they aren't all needed
  45. // return year % 4 == 0 && !(year % 100 == 0 && year % 400 !0 0);
  46. return ((year % 4 == 0) && !((year % 100 == 0) && (year % 400 != 0)));
  47. }
  48.  
  49. bool validDay(int year, int month, int day) {
  50. // Approach:
  51. // Check all the "false" conditions
  52. // If any of these conditions trigger, immediately return false
  53. // If the function reaches the end, that means all
  54. // the "false" conditions were not met, so only then can you return true
  55.  
  56. // Check for bad year/month
  57. if (!validMonth(month) || !validYear(year))
  58. return false;
  59.  
  60. // Check for bad day
  61. switch (month) {
  62. // Since there's no "break" between these cases,
  63. // they all "fall through" and execute the same code
  64. case APR:
  65. case JUN:
  66. case SEP:
  67. case NOV:
  68. // 31 days
  69. if (day < 1 || day > 31)
  70. return false;
  71. break;
  72. // February is the special case
  73. case FEB:
  74. if (leapYear(year)) {
  75. if (day < 1 || day > 29)
  76. return false;
  77. } else {
  78. if (day < 1 || day > 28)
  79. return false;
  80. }
  81. break;
  82. // "default" is all the other months
  83. default:
  84. if (day < 1 || day > 30)
  85. return false;
  86. break;
  87. }
  88.  
  89. // If your code has reached this point, it means the day is valid
  90. // This is the function you want to thoroughly check with every
  91. // single bad case you can think of, then some random good cases
  92. return true;
  93. }
  94.  
  95. void getData(int& month, int& day, int& year) {
  96. // You figure this out, this will always be something language-specific.
  97. // Know though that this function takes POINTERS to variables as its arguments
  98. // meaning that when you manipulate the variables locally, you're actually
  99. // manipulating the original variables.
  100. // e.g.
  101. // int a = 1;
  102. // int b = 2;
  103. // add(&a, &b);
  104. // After this, a = 3, b = 2
  105. // BUT
  106. // int a = 1;
  107. // int b = 2;
  108. // add(a, b);
  109. // After this, a = 1, b = 2
  110. // This is a bad example and depends on
  111. // how "add" is implemented, but you get the idea - see your textbook for examples
  112. }
  113.  
  114. void easterDate(int& month, int&day, int year) {
  115. // Exactly as the instructions say. Use "/" and "%".
  116. }
  117.  
  118. int daysBetween(int month1, int day1, int month2, int day2, int year) {
  119. return dayOfYear(day2, month2, year) - dayOfYear(day1, month1, year);
  120. }
  121.  
  122. int dayOfYear(int day, int month, int year) {
  123. int value;
  124.  
  125. // Add months values
  126. // We can do this because we know the set values
  127. // Deal with leap year later
  128. // Note: Yes, this can be a bunch of "if" statements followed by each other
  129. // that step-by-step adds things up, but a single switch statement is
  130. // MUCH MUCH faster since it allows for compile-time calculated values
  131. // and has less checks
  132. switch(month) {
  133. case JAN:
  134. break;
  135. case FEB:
  136. value += (31);
  137. break;
  138. case MAR:
  139. value += (31 + 28);
  140. break;
  141. case APR:
  142. value += (31 + 28 + 31);
  143. break;
  144. case MAY:
  145. value += (31 + 28 + 31 + 30);
  146. break;
  147. case JUN:
  148. value += (31 + 28 + 31 + 30 + 31);
  149. break;
  150. case JUL:
  151. value += (31 + 28 + 31 + 30 + 31 + 30);
  152. break;
  153. case AUG:
  154. value += (31 + 28 + 31 + 30 + 31 + 30 + 31);
  155. break;
  156. case SEP:
  157. value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
  158. break;
  159. case OCT:
  160. value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
  161. break;
  162. case NOV:
  163. value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
  164. break;
  165. case DEC:
  166. value += (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
  167. break;
  168. }
  169.  
  170. // Check leap-years
  171. if (leapYear(year) && month > 2) {
  172. value += 1;
  173. }
  174.  
  175. // Add the actual day
  176. value += day;
  177.  
  178. // Return calculated value
  179. return value;
  180. }
  181.  
  182. int main() {
  183. bool loop = true;
  184. string command;
  185. int month1, day1, month2, day2, year;
  186. int dayDiff;
  187.  
  188. // Somewhere in here you should check when the user wants to exit
  189. // and if found, set loop to false.
  190. // The easiest way would be to change getData to return a bool
  191. // (which is false if the user indicates he wants to exit)
  192. // and set loop to the returned bool
  193. while (loop){
  194. command = getCommand(); // Some prompt, this is an example
  195. // Here we just use a series of "if-else" because there
  196. // aren't many cases and each is very different
  197. if (strcmp(command, "exit") == 0) {
  198. loop = false;
  199. } else if (strcmp(command, "day") == 0) {
  200. getData(&month1, &day1, &year); // Overload getData or made a new function
  201. if (!validDay(year, month1, day1)) {
  202. // print error
  203. } else {
  204. // print dayOfYear(day1, month1, year);
  205. }
  206. } else if (strcmp(command, "easter") == 0) {
  207. getData(&year);
  208. easterDate(&month1, &day1, year);
  209. // print month and day
  210. } else if (strcmp(command, "diff") == 0) {
  211. getData(&month1, &day1, &month2, &day2, &year);
  212. dayDiff = daysBetween(month1, day1, month2, day2, year);
  213. if (dayDiff < 0) {
  214. // print error
  215. } else {
  216. // print dayDiff
  217. }
  218. }
  219. }
  220.  
  221. // print exit message
  222. }
Add Comment
Please, Sign In to add comment