fork(1) download
  1. /**
  2.  * birthdays.cpp
  3.  *
  4.  * <#Name#>changyu liu
  5.  * <#Uniqname#>lchangyu
  6.  *
  7.  * EECS 183: Project 2
  8.  *
  9.  * <#description#>
  10.  */
  11.  
  12. #include <iostream>
  13. #include <string>
  14. #include <cmath>
  15.  
  16.  
  17. using namespace std;
  18.  
  19.  
  20. /**
  21.  * Requires: nothing
  22.  * Modifies: cout
  23.  * Effects: prints out the initial heading for the program
  24.  */
  25. void printHeading();
  26.  
  27.  
  28. /**
  29.  * Requires: nothing
  30.  * Modifies: cout
  31.  * Effects: prints out the final greeting for the program
  32.  */
  33. void printCloser();
  34.  
  35.  
  36. /**
  37.  * Requires: nothing
  38.  * Modifies: cout
  39.  * Effects: prints the menu
  40.  */
  41. void printMenu();
  42.  
  43.  
  44. /**
  45.  * Note: write your test cases in main() BEFORE you implement this function
  46.  *
  47.  * Requires: nothing
  48.  * Modifies: cout, cin
  49.  * Effects: prints the menu
  50.  * reads the input from the user
  51.  * checks to make sure the input is within range for the menu
  52.  * If not prints "Invalid menu choice"
  53.  * continues to print the menu and read an input until a valid one is
  54.  * entered
  55.  * returns the users choice of menu options
  56.  
  57.  */
  58. int getMenuChoice();
  59.  
  60.  
  61. /**
  62.  * Note: write your test cases in main() BEFORE you implement this function
  63.  *
  64.  * Requires: month, day, year may represent a date
  65.  * Modifies: nothing
  66.  * Effects: returns 'true' if the date is in the limits
  67.  * of the Gregorian calendar otherwise returns 'false'
  68.  */
  69. bool isGregorianDate(int month, int day, int year);
  70.  
  71.  
  72. /**
  73.  * Note: write your test cases in main() BEFORE you implement this function
  74.  *
  75.  * Requires: year is a Gregorian year
  76.  * Modifies: nothing
  77.  * Effects: Returns 'true' if the year is a leap year
  78.  * otherwise returns 'false'
  79.  */
  80. bool isLeapYear(int year);
  81.  
  82.  
  83. /**
  84.  * Note: write your test cases in main() BEFORE you implement this function
  85.  *
  86.  * Requires: month, day, year may represent a date
  87.  * Modifies: nothing
  88.  * Effects: Returns 'true' if the date is valid
  89.  * otherwise returns 'false'
  90.  * see the spec for definition of "valid"
  91.  */
  92. bool isValidDate(int month, int day, int year);
  93.  
  94.  
  95. /**
  96.  * Note: write your test cases in main() BEFORE you implement this function
  97.  *
  98.  * Requires: month, day, year is a valid date
  99.  * i.e., the date passed to this function has already passed
  100.  * isValidDate()
  101.  * Modifies: nothing
  102.  * Effects: Returns the value that Zeller's formula calculates
  103.  */
  104. int determineDay(int month, int day, int year);
  105.  
  106.  
  107. /**
  108.  * Note: write your test cases in main() BEFORE you implement this function
  109.  *
  110.  * Requires: day (0 represents Saturday, 1 Sunday, 2 Monday, 3 Tuesday, etc)
  111.  * Modifies: cout
  112.  * Effects: prints the day you were born on
  113.  * Sunday, Monday, ..., Saturday
  114.  */
  115. void printDayOfBirth(int day);
  116.  
  117.  
  118. /**
  119.  * Note: write your test cases in main() BEFORE you implement this function
  120.  *
  121.  * Requires: nothing
  122.  * Modifies: cout, cin
  123.  * Effects: Asks for the Month/day/year of their birth
  124.  * If the date is valid, it will print the day
  125.  * of the week you were born on
  126.  * Otherwise, it will print "Invalid date" prompt
  127.  */
  128. void determineDayOfBirth();
  129.  
  130.  
  131. /**
  132.  * Note: write your test cases in main() BEFORE you implement this function
  133.  *
  134.  * Base Project
  135.  * Requires: nothing
  136.  * Modifies: cout
  137.  * Effects: prints "Under Construction"
  138.  *
  139.  * S'more version of this function
  140.  * Requires: nothing
  141.  * Modifies: cout, cin
  142.  * Effects: reads the month and day of birthday
  143.  * loops through 10 years printing the day of the week
  144.  * the birthday falls on
  145.  * If the month/day is not valid, it prints nothing
  146.  */
  147. void print10Birthdays();
  148.  
  149.  
  150. /**
  151.  * Note: write your test cases in main() BEFORE you implement this function
  152.  *
  153.  * Requires: nothing
  154.  * Modifies: nothing
  155.  * Effects: Asks for a Gregorian year and prints the
  156.  * 10 leap years after (not including) the year entered.
  157.  * If the year is invalid, it prints nothing.
  158.  */
  159. void print10LeapYears();
  160.  
  161.  
  162. int main() {
  163.  
  164. int month;
  165. int day;
  166. int year;
  167. char placeHolder1;//holds "/"
  168. char placeHolder2;//holds 2nd "/"
  169.  
  170.  
  171. printHeading();//prints the heading in int
  172. cout << "Enter your date of birth -->" << endl << endl;
  173. cin >> month >> placeHolder1 >> day >> placeHolder2 >> year;
  174.  
  175. if (!isValidDate(month, day, year)) {
  176. //if the function returns true, this will output "invalid"
  177. cout << "Invalid date";
  178. }
  179. else {//if the date passes through the two previous functions, the date is valid and will be evaluated
  180.  
  181. day = determineDay(month, day, year);
  182.  
  183.  
  184. cout << endl << endl << "Have a great birthday!!!";
  185.  
  186. }
  187.  
  188. return 0;
  189. }
  190.  
  191.  
  192. void printHeading() {
  193. cout << "*******************************" << endl
  194. << " Birthday Calculator " << endl
  195. << "*******************************" << endl << endl;
  196. }
  197.  
  198.  
  199. void printCloser() {
  200. cout << endl;
  201. cout << "****************************************************" << endl
  202. << " Thanks for using the Birthday Calculator " << endl
  203. << "****************************************************" << endl
  204. << endl;
  205. }
  206.  
  207.  
  208. void printMenu() {
  209. cout << endl << endl;
  210. cout << "Menu Options" << endl
  211. << "------------" << endl;
  212. cout << "1) Determine day of birth" << endl;
  213. cout << "2) Print the next 10 leap years" << endl;
  214. cout << "3) Determine birthdays for the next 10 years" << endl;
  215. cout << "4) Finished" << endl << endl;
  216.  
  217. cout << "Choice --> ";
  218. }
  219.  
  220. int getMenuChoice() {
  221.  
  222. int userChoice = 0;
  223.  
  224. string clearInputStream;
  225.  
  226.  
  227.  
  228. printMenu();
  229.  
  230. cin >> userChoice;
  231.  
  232. if (userChoice != 1 && userChoice != 2 && userChoice != 3 && userChoice !=4)
  233. {
  234.  
  235. while (userChoice != 1 && userChoice != 2 && userChoice != 3 && userChoice !=4) {
  236.  
  237. if (!cin) {
  238. cin.clear();
  239. getline (cin, clearInputStream);
  240. }
  241.  
  242. cout << "Invalid menu choice" << endl;
  243.  
  244.  
  245. printMenu();
  246.  
  247.  
  248. cin >> userChoice;
  249.  
  250. }
  251.  
  252. }
  253.  
  254.  
  255. return userChoice;
  256.  
  257. return 0;
  258. }
  259.  
  260.  
  261.  
  262. bool isGregorianDate(int month, int day, int year) {
  263. if (year > 1752) {
  264. return true;
  265. }
  266. else if (year == 1752) {
  267. if (month < 9) {
  268. return false;
  269. }
  270. else if (month > 9) {
  271. return true;
  272. }
  273. else if (month == 9) {
  274. if (day < 14) {
  275. return false;
  276. }
  277. else return true;
  278. }
  279. }
  280.  
  281.  
  282. return false;
  283. }
  284.  
  285. bool isLeapYear(int year) {
  286.  
  287. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  288. return true;
  289. }
  290. else{
  291. return false;
  292. }
  293. }
  294.  
  295.  
  296.  
  297. bool isValidDate(int month, int day, int year) {
  298.  
  299. if (!isGregorianDate(month, day, year)) {
  300. return false;
  301. }
  302. switch (month) {
  303. case 1:
  304. case 3:
  305. case 5:
  306. case 7:
  307. case 8:
  308. case 10:
  309. case 12:
  310. if (day >= 1 && day <32) {
  311. return true;
  312. }
  313. else{
  314. return false;
  315.  
  316. }
  317. break;
  318.  
  319. case 4:
  320. case 6:
  321. case 9:
  322. case 11:
  323. if (day >= 1 && day < 31) {
  324. return true;
  325. }
  326. else{
  327. return false;
  328. }
  329. break;
  330.  
  331. case 2:
  332. if (isLeapYear(year)) {
  333. if (day >= 1 && day <30) {
  334. return true;
  335. }
  336. }
  337. else if (day >= 1 && day < 29) {
  338. return true;
  339. }
  340. else{
  341. return false;
  342. }
  343. break;
  344. default:
  345. return false;
  346. break;
  347. }
  348.  
  349. }
  350.  
  351.  
  352.  
  353. int determineDay(int month, int day, int year) {
  354. int zday = day;
  355. int zmonth;
  356. int zyear;
  357. int zyear1;
  358. int zcentury;
  359.  
  360. if (month == 1 || month == 2) {
  361. zmonth = month + 12;
  362. zyear = year - 1;
  363. }
  364. else {
  365. zmonth = month;
  366. zyear = year;
  367. }
  368. zyear1 = zyear % 100;
  369. zcentury = zyear / 100;
  370.  
  371. day = (zday + (static_cast<int>((13 * (zmonth + 1)) / 5.0)) + zyear1
  372. + (zyear1 / 4) + (zcentury / 4) + 5 * zcentury) % 7;
  373.  
  374.  
  375. return day;
  376. }
  377.  
  378.  
  379.  
  380. void printDayOfBirth(int day) {
  381.  
  382. switch (day) {
  383. case 0:
  384. cout << "You were born on a: Saturday";
  385. break;
  386. case 1:
  387. cout << "You were born on a: Sunday";
  388. break;
  389. case 2:
  390. cout << "You were born on a: Monday";
  391. break;
  392. case 3:
  393. cout << "You were born on a: Tuesday";
  394. break;
  395. case 4:
  396. cout << "You were born on a: Wednesday";
  397. break;
  398. case 5:
  399. cout << "You were born on a: Thursday";
  400. break;
  401. case 6:
  402. cout << "You were born on a: Friday";
  403. default:
  404.  
  405. break;
  406.  
  407. }
  408.  
  409. }
  410.  
  411.  
  412.  
  413. void determineDayOfBirth() {
  414.  
  415. int month;
  416. int day;
  417. int year;
  418. char forwardslash; // reads character between month and day
  419. char forwardslash2; // reads character between day and year
  420.  
  421. cout << "Enter your date of birth " << endl << "format: month / day / year -->" << endl;
  422. cin >> month >> forwardslash >> day >> forwardslash2 >> year;
  423.  
  424. if (!isValidDate(month, day, year)) {
  425.  
  426. cout << "Invalid date";
  427. }
  428.  
  429.  
  430. else {
  431. day = determineDay(month, day, year);
  432.  
  433. printDayOfBirth(day); }
  434.  
  435. cout << endl << endl << "Have a great birthday!!!" << endl;
  436.  
  437. }
  438.  
  439.  
  440.  
  441.  
  442. void print10Birthdays() {
  443.  
  444. cout << "Under Construction" << endl;
  445.  
  446. return;
  447. }
  448.  
  449. void print10LeapYears() {
  450.  
  451. int count=1,i;
  452. cout << "Enter the Gregorian year :" <<endl;
  453. cin >> i;
  454.  
  455.  
  456. cout << "Next 10 leap years are :" <<endl;
  457.  
  458. while (count <11){
  459. i = i + 1;
  460.  
  461. if (isLeapYear(i)) {
  462.  
  463. cout << i <<endl;
  464.  
  465. count++;
  466.  
  467. }
  468. }
  469.  
  470. return;
  471. }
  472.  
Success #stdin #stdout 0.01s 5556KB
stdin
Standard input is empty
stdout
*******************************
      Birthday Calculator      
*******************************

Enter your date of birth -->

Invalid date