Advertisement
clairec

shitty lab

Sep 10th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. /* Prototype all the happy lil functions we get to use, woo! */
  7. void menuPrint();
  8. bool dateInvalid(int year=1900, int month=1, int day=1);
  9. void playLeapYear();
  10. bool leapYear(int year);
  11. void playZodiac();
  12. string zodiac(int year);
  13. void playDayOfWeek();
  14. int julian(int year, int month, int day);
  15. string dayOfWeek(int year, int month, int day);
  16. void playSols();
  17. vector<int> sols(int year, int month, int day);
  18.  
  19. int main()
  20. {
  21.   string menuChoice;
  22.  
  23.   while (true)
  24.     {
  25.       menuPrint();
  26.       cin >> menuChoice;
  27.  
  28.       if ((menuChoice == "q") || (menuChoice == "-1"))
  29.     {
  30.       break;
  31.     }
  32.       else if (menuChoice == "1")
  33.     {
  34.       playLeapYear();
  35.     }
  36.       else if (menuChoice == "2")
  37.     {
  38.       playZodiac();
  39.     }
  40.       else if (menuChoice == "3")
  41.     {
  42.       playDayOfWeek();
  43.     }
  44.       else
  45.     {
  46.       cout << "FIXME" << endl;
  47.     }
  48.     }
  49.   return 0;
  50. }
  51.  
  52.  
  53. void menuPrint()
  54. {
  55.   /* For printing the program menu */
  56.   cout << "\nMain Menu — Enter an item's menu-number or input q or -1 to "
  57.   "exit the program \n"
  58.   " 1) Leap years \n"
  59.   " 2) Zodiac \n"
  60.   " 3) Day of the week \n"
  61.   " 4) Martian Calendar \n"
  62.   " 5) Square (by value) \n"
  63.   " 6) Square (reference) \n"
  64.   " 7) Square (by pointer) \n" << endl;
  65. }
  66.  
  67.  
  68. void playLeapYear()
  69. {
  70.   /* The "leap year" game */
  71.   int year = 0;
  72.   cout << "Leap year:" << endl;
  73.   while (dateInvalid(year))
  74.     {
  75.     cout << "Please enter a year 1900 or later:" << endl;
  76.     cin.clear(); cin.ignore(10000, '\n');
  77.     cin >> year;
  78.     }
  79.   cout << year << " is" << (leapYear(year) ? "" : " not") << " a leap year.";
  80.   cout << endl;
  81. }
  82.  
  83.  
  84. bool leapYear(int year)
  85. {
  86.   /* Returns true if a year is a leap year, returns false otherwise */
  87.   if (year % 4 != 0)
  88.     { // Years that aren't divisible by 4 arent' leap years.
  89.       return false;
  90.     } // All years past here are divisible by 4
  91.   else if (year % 100 != 0)
  92.     { // 4th years which aren't divisble by 100 are always leap
  93.       return true;
  94.     } // All years past here are divisble by 4 and 100.
  95.   else if (year % 400 != 0)
  96.     { // Years divisible by 4 and 100 but not by 400 aren't leap years
  97.       return false;
  98.     }
  99.   else
  100.     { // All years divisible by 4, 100, and 400 are leap years.
  101.       return true;
  102.     }
  103. }
  104.  
  105.  
  106. bool dateInvalid(int year, int month, int day)
  107. {
  108.   /* Returns true if the given date is invalid. Defaults to valid date. */
  109.   bool yearInvalid = (year < 1900);
  110.   bool monthInvalid = (month < 1 || month > 12);
  111.   int monthMax;
  112.   switch(month)
  113.     {
  114.     case 2:
  115.       monthMax = (leapYear(year) ? 29 : 28);
  116.       break;
  117.     case 4:
  118.     case 6:
  119.     case 9:
  120.     case 11:
  121.       monthMax = 30;
  122.       break;
  123.     default:
  124.       monthMax = 31;
  125.       break;
  126.     }
  127.   bool dayInvalid = ((day < 1) || (day > monthMax));
  128.   return (yearInvalid || monthInvalid || dayInvalid);
  129. }
  130.  
  131.  
  132. void playZodiac()
  133. {
  134.   /* The "zodiac game" */
  135.   int year = 0;
  136.   cout << "Zodiac:" << endl;
  137.   while(dateInvalid(year))
  138.     {
  139.       cout << "Please enter a year 1900 or later" << endl;
  140.       cin.clear(); cin.ignore(10000, '\n');
  141.       cin >> year;
  142.     }
  143.   cout << year << " is the year of the " << zodiac(year) << ". " << endl;
  144. }
  145.  
  146.  
  147. string zodiac(int year)
  148. {
  149.   /* Return a string representing the zodiac animal for year. Function reduces
  150.      years to a 12-year cycle starting at 1900, the year of the rat. */
  151.   switch((year - 1900) % 12)
  152.     {
  153.     case 0:   return "rat";
  154.     case 1:   return "ox";
  155.     case 2:   return "tiger";
  156.     case 3:   return "rabbit";
  157.     case 4:   return "dragon";
  158.     case 5:   return "snake";
  159.     case 6:   return "horse";
  160.     case 7:   return "goat";
  161.     case 8:   return "monkey";
  162.     case 9:   return "rooster";
  163.     case 10:  return "dog";
  164.     case 11:  return "pig";
  165.     default:  return "unknown";
  166.     }
  167. }
  168.  
  169.  
  170. void playDayOfWeek()
  171. {
  172.   /* The "Day of the Week" game */
  173.   int year = 0;
  174.   int month = 0;
  175.   int day = 0;
  176.   cout << "Day of the Week:" << endl;
  177.   while (dateInvalid(year, month, day))
  178.     {
  179.       cout << "Please enter a date 01-01-1900 or later. Use MM DD YYYY format.";
  180.       cout << endl;
  181.       cout << "Month:" << endl;
  182.       cin.clear(); cin.ignore(10000, '\n');
  183.       cin >> month;
  184.       cout << "Day:" << endl;
  185.       cin.clear(); cin.ignore(10000, '\n');
  186.       cin >> day;
  187.       cout << "Year:" << endl;
  188.       cin.clear(); cin.ignore(10000, '\n');
  189.       cin >> year;
  190.     }
  191.   cout << month << "-" << day << "-" << year << " falls on a ";
  192.   cout << dayOfWeek(year, month, day) << endl;
  193. }
  194.  
  195.  
  196. int julian(int year, int month, int day)
  197. {
  198.   /* Where 1900-01-01 is 1, 1900-01-02 is 2, and so on */
  199.   int julianDate = 0;
  200.   // lDays is used for leap years, cDays is used for common years.
  201.   // These vectors represent the sum of the days in all previous months.
  202.   vector<int> lDays = {0,31,60,91,121,152,182,213,244,274,305,335,366};
  203.   vector<int> cDays = {0,31,59,90,120,151,181,212,243,273,304,334,365};
  204.   // yDays is equal to cDays on a common year or lDays on a leap year.
  205.   vector<int> yDays = leapYear(year) ? lDays : cDays;
  206.   for(int i = 1900; i < year; i++)
  207.     {
  208.       julianDate += leapYear(i) ? 366 : 365;
  209.     }
  210.   julianDate += yDays.at(month-1);
  211.   julianDate += day;
  212.   return julianDate;
  213. }
  214.  
  215.  
  216. string dayOfWeek(int year, int month, int day)
  217. {
  218.   /* Returns the day of the week of a given date as a string */
  219.  
  220.   /* We Subtract 1 from our julian date so that 1900-01-01 is 0. There
  221.      is no real reason to do this other than that Monday is CLEARLY
  222.      the start of the week and deserves to be 0, not 1. */
  223.   int julianDate = julian(year, month, day) - 1;
  224.   julianDate %= 7;
  225.   switch(julianDate)
  226.     {
  227.     case 0:  return "monday";
  228.     case 1:  return "tuesday";
  229.     case 2:  return "wednesday";
  230.     case 3:  return "thursday";
  231.     case 4:  return "friday";
  232.     case 5:  return "saturday";
  233.     case 6:  return "sunday";
  234.     default: return "unknown";
  235.     }
  236. }
  237.  
  238.  
  239. void playSols()
  240. {
  241.   int year = 0;
  242.   int month = 0;
  243.   int day = 0;
  244.   cout << "Martian Calendar:" << endl;
  245.     while (dateInvalid(year, month, day))
  246.     {
  247.       cout << "Please enter a date 01-01-1900 or later. Use MM DD YYYY format.";
  248.       cout << endl;
  249.       cout << "Month:" << endl;
  250.       cin.clear(); cin.ignore(10000, '\n');
  251.       cin >> month;
  252.       cout << "Day:" << endl;
  253.       cin.clear(); cin.ignore(10000, '\n');
  254.       cin >> day;
  255.       cout << "Year:" << endl;
  256.       cin.clear(); cin.ignore(10000, '\n');
  257.       cin >> year;
  258.     }
  259.   cout << "FIXME";
  260. }
  261.  
  262.  
  263. vector<int> sols(int year, int month, int day)
  264. {
  265.   int marsJulian = julian(year, month, day) / 1.027;
  266.   int marsYear = marsJulian / 672;
  267.   int marsMonth = (marsJulian - marsYear * 672) / 56;
  268.   int marsDay = (marsJulian - marsYear * 672 - marsMonth * 56);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement