Advertisement
clairec

old shitty lab

Sep 10th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. // Mr. Liu, with all due respect sir I cannot take you seriously as a
  6. // programmer nor a lover of computer science when you blatantly
  7. // disregard ISO 8601 and willingly use the horrific and mangled MM DD
  8. // YYYY format. Not only does this fly in the face of good morals and
  9. // common sense and give headaches to those trying to sort through
  10. // timestamped files, it is a regressive thing to do and you should
  11. // feel absolutely ashamed. I beg of you to no longer follow the path
  12. // of nitwits. Turn towards the righteous path of international
  13. // standards, easy date sorting. Join us in using YYYY-MM-DD formats
  14. // and the world shall live together in unity forever. MM DD YYYY: Bad
  15. // for your health, bad for your code, BAD FOR AMERICA.
  16.  
  17. void menuPrint(); // Printing the menu in main() is ugly & cluttered.
  18.  
  19. bool checkDate(int year=1900, int month=1, int day=1);
  20.  
  21. bool leapYear(int year);
  22.  
  23. std::string zodiac(int year);
  24.  
  25. std::string dayOfWeek(int year, int month, int day);
  26.  
  27. std::vector<int> sols(int year, int month, int day);
  28.  
  29. int squareByValue(int val);
  30.  
  31. int squareByReference(int & ref);
  32.  
  33. int squareByPointer(int * ptr);
  34.  
  35.  
  36. int main()
  37. {
  38.   std::string userChoice;
  39.  
  40.   while (true)
  41.   {
  42.        
  43.     menuPrint();
  44.     std::cin >> userChoice;
  45.    
  46.  
  47.     if ((userChoice == "q") || (userChoice == "-1"))
  48.       { // User wants to quit; exit program.
  49.     break;
  50.       }
  51.    
  52.     else if (userChoice == "1")
  53.       { // User chose the leap year program
  54.     int year = 0;
  55.        
  56.     std::cout << std::endl << "Leap Year:" << std::endl;
  57.     while (!checkDate(year))
  58.       { // Ensure they only enter a valid year.
  59.         std::cout << "Please enter a year 1900 or later." << std::endl;
  60.         std::cin >> year;
  61.         // Every time we read from cin into an integer we will run
  62.         // these two statements, because otherwise the code will
  63.         // freak out if the user enters a string. clear turns off the
  64.         // error flag and ignore jumps to the next newline so that
  65.         // the program does not try to read any more letters into
  66.         // numbers.
  67.         std::cin.clear(); std::cin.ignore(100000,'\n');
  68.       }
  69.     std::cout << year << " is " << (leapYear(year) ? "" : "not ");
  70.     std::cout << "a leap year." << std::endl;
  71.       }
  72.    
  73.     else if (userChoice == "2")
  74.       { // User chose the zodiac program
  75.     int year = 0;
  76.    
  77.     std::cout << "Zodiac:" << std::endl;
  78.     while (!checkDate(year))
  79.       { // Ensure they only enter a valid year.
  80.         std::cout << "Please enter a year 1900 or later." << std::endl;
  81.         std::cin >> year;
  82.         std::cin.clear(); std::cin.ignore(100000, '\n');
  83.       }
  84.     std::cout << year << " is the year of the " << zodiac(year) << std::endl;
  85.       }
  86.  
  87.     else if (userChoice == "3")
  88.       { // user chose the day of the week program.
  89.     std::cout << "Day of the Week:" << std::endl;
  90.         int year = 0;
  91.     int month = 0;
  92.     int day = 0;
  93.  
  94.     while(!checkDate(year, month, day))
  95.       {
  96.         std::cout << "Please enter a date 1900-01-01 or later. ";
  97.         std::cout << "Use MM DD YYYY format." << std::endl;
  98.         std::cout << "Month:" << std::endl;
  99.         std::cin >> month;
  100.         std::cin.clear(); std::cin.ignore(100000, '\n');
  101.         std::cout << "Day:" << std::endl;
  102.         std::cin >> day;
  103.         std::cin.clear(); std::cin.ignore(100000, '\n');
  104.         std::cout << "Year:" << std::endl;
  105.         std::cin >> year;
  106.         std::cin.clear(); std::cin.ignore(100000, '\n');
  107.       }
  108.     std::cout << year << "-" << month << "-" << day << " falls on a ";
  109.     std::cout << dayOfWeek(year, month, day) << std::endl;
  110.       }
  111.  
  112.     else if (userChoice == "4")
  113.       { // User chose the Martian Calendar program.
  114.     std::cout << "Martian Calendar:" << std::endl;
  115.     int year = 0;
  116.     int month = 0;
  117.     int day = 0;
  118.  
  119.     while(!checkDate(year, month, day))
  120.       {
  121.         std::cout << "Please enter a date on or after 1900-1-1. Please ";
  122.         std::cout << " use MM DD YYYY format." << std::endl << "Month:\n";
  123.         std::cin >> month;
  124.         std::cin.clear(); std::cin.ignore(100000, '\n');
  125.         std::cout << "Day:\n";
  126.         std::cin >> day;
  127.         std::cin.clear(); std::cin.ignore(100000, '\n');
  128.         std::cout << "Year:\n";
  129.         std::cin >> year;
  130.         std::cin.clear(); std::cin.ignore(100000, '\n');
  131.       }
  132.     std::vector<int> marsDate = sols(year, month, day);
  133.     std::cout << "The martian date is " << marsDate.at(1) << " ";
  134.     std::cout << marsDate.at(2) << " " << marsDate.at(0) << std::endl;
  135.       }
  136.     else
  137.       {
  138.     std::cout << "\n\nFIXME\n\n";
  139.       }
  140.   }
  141.   return 0;
  142. }
  143.  
  144.  
  145. void menuPrint()
  146. {
  147.   std::cout << std::endl;
  148.   std::cout << "Main Menu — Enter an item's menu-number or input q or -1 to ";
  149.   std::cout << "exit the program" << std::endl;
  150.   std::cout << "1) Leap years" << std::endl;
  151.   std::cout << "2) Zodiac" << std::endl;
  152.   std::cout << "3) Day of the week" << std::endl;
  153.   std::cout << "4) Martian Calendar" << std::endl;
  154.   std::cout << "5) Square (by value)" << std::endl;
  155.   std::cout << "6) Square (by reference)" << std::endl;
  156.   std::cout << "7) Square (by pointer)" << std::endl;
  157. }
  158.  
  159.  
  160. bool checkDate(int year, int month, int day)
  161. { /* Returns true if the date is valid. Defaults to 1900-01-01 so you
  162.      can check just a year without checking a month or a day. */
  163.   bool validYear = (year >= 1900);
  164.   bool validMonth = ((month >= 1) && (month <= 12));
  165.   int monthMax;
  166.   switch (month)
  167.     {
  168.     case 2:
  169.       monthMax = (leapYear(year) ? 29 : 28);
  170.       break;
  171.     case 4:
  172.     case 6:
  173.     case 9:
  174.     case 11:
  175.       monthMax = 30;
  176.       break;
  177.     default:
  178.       monthMax = 31;
  179.       break;
  180.     }
  181.   bool validDay = ((day >= 1) && (day <= monthMax));
  182.   return (validYear && validMonth && validDay);
  183. }
  184.  
  185.  
  186. bool leapYear(int year)
  187. {
  188.   if (year % 4 != 0) // year is not a 4th year
  189.     {
  190.       return false;
  191.     } // any years past this point are divisible by 4
  192.   else if (year % 100 != 0) // year is a 4th year and is not divisible by 100)
  193.     {
  194.       return true; // all such years are leap years.
  195.     } // any years past this point are divisble by 4 and divisble by 100
  196.   else if (year % 400 != 0) // year is divisible by 4 and not divisible by 400
  197.     {
  198.       return false;
  199.     } // any year past here must be divisible by 4 and divisible by 400
  200.   else
  201.     {
  202.       return true; // all such ears are leap years
  203.     }
  204. }
  205.  
  206.  
  207. std::string zodiac(int year)
  208. {
  209.   int nthYear = (year - 1900) % 12; // reduce years to 12-yr cycle starting at 1900
  210.   switch (nthYear)
  211.     {
  212.     case 0:
  213.       return "rat";
  214.     case 1:
  215.       return "ox";
  216.     case 2:
  217.       return "tiger";
  218.     case 3:
  219.       return "rabbit";
  220.     case 4:
  221.       return "dragon";
  222.     case 5:
  223.       return "snake";
  224.     case 6:
  225.       return "horse";
  226.     case 7:
  227.       return "goat";
  228.     case 8:
  229.       return "monkey";
  230.     case 9:
  231.       return "rooster";
  232.     case 10:
  233.       return "dog";
  234.     case 11:
  235.       return "pig";
  236.     default:
  237.       return "unknown";
  238.     }
  239. }
  240.      
  241. std::string dayOfWeek(int year, int month, int day)
  242. { /* Wow okay this is complicated. Basically there's 4 possible
  243.      centries in the gregorian calendar, and they keep cycling over
  244.      and over. Let's call one of these a maj (for major
  245.      centuries). You can either have a century beginning with a common
  246.      year that starts on a monday, such as 1900, a century that starts
  247.      with a leap year that starts on a saturday (such as 2000), a
  248.      century that begins on a common year and starts on a friday (such
  249.      as 2100), or a century that begins with a common year and starts
  250.      on a wednesday, such as 2200. Using this knowledge, we can just
  251.      compute how many monday-saturday cycles have happened since the
  252.      start of the century and then find the day of the week based on
  253.      what day of the week the century started on. */
  254.  
  255.   // find the number of days in february
  256.   int feb = leapYear(year) ? 29 : 28;
  257.   //find how many days have passed BEFORE the month you're on.
  258.   int months[] = {0,31,feb,31,30,31,30,31,31,30,31,30,31};
  259.   int pastMon = 0;
  260.   for(int i=0; i < month; i++) {pastMon += months[i];}
  261.   // now we're calculating what we called a maj earlier.
  262.   int maj = ((year - 1900) % 400) / 100;
  263.     // get the last two digits of the year since this is all we care about now.
  264.   year %= 100;
  265.     // For calculating total # of days in the years leading up to this one.
  266.   int n = year - 1;
  267.   // Finding that total number of days and storing it in pastYrs.
  268.   int pastYrs = (n>-1) ? ((n-(n/4))*365 + (n/4)*366 + (maj==1 ? 366 : 365)) : 0;
  269.   // Add up the number of days before this month in this year, the number of days
  270.   // in the years leading up to now, and the number of days since the start of
  271.   // the month, and subtract 1 so that the first day is 0.
  272.   int julian = pastYrs + pastMon + day - 1;
  273.   // mod it by 7 to find our "naive" day of the week where every century starts on
  274.   // a monday
  275.   int septCycle = julian % 7;
  276.   // from our naive day septCycle, shift it over by what the century ACTUALLY
  277.   // started on to find the actual day of the week.
  278.   int start;
  279.   switch(maj)
  280.     {
  281.     case 0:
  282.       start = 0;
  283.       break;
  284.     case 1:
  285.       start = 5;
  286.       break;
  287.     case 2:
  288.       start = 4;
  289.       break;
  290.     case 3:
  291.       start = 2;
  292.       break;
  293.     default:
  294.       start = -1000;
  295.       break;
  296.     }
  297.   int dOW = (septCycle + start) % 7;
  298.   // FINALLY return "monday" if it's 0, "tuesday" if it's 1, etc.
  299.   switch(dOW)
  300.     {
  301.     case 0:
  302.       return "monday";
  303.     case 1:
  304.       return "tuesday";
  305.     case 2:
  306.       return "wednesday";
  307.     case 3:
  308.       return "thursday";
  309.     case 4:
  310.       return "friday";
  311.     case 5:
  312.       return "saturday";
  313.     case 6:
  314.       return "sunday";
  315.     default:
  316.       return "unknown";
  317.     }
  318. }
  319.  
  320. std::vector<int> sols(int year, int month, int day)
  321. { // You know what? The approach I took in the Days of the Week
  322.   // function was too damned mathy. Lets try something a bit more loopy
  323.   // here instead.
  324.   // cumDays adds up the ammount of days in the preceeding months
  325.   std::vector<int> leapYrDays = {0,31,60,91,121,152,182,212,243,274,304,335,366};
  326.   std::vector<int> commYrDays = {0,31,59,90,120,151,181,211,242,273,303,334,365};
  327.   std::vector<int> cumDays = leapYear(year) ? leapYrDays : commYrDays;
  328.   int yrsDays = 0;
  329.   for (int i=1900; i < year; i++)
  330.     {
  331.       yrsDays += leapYear(i) ? 366 : 365;
  332.     }
  333.   int totalDays = yrsDays + cumDays.at(month-1) + day;
  334.   // Total number of martian days since 1900-01-01 mars time.
  335.   int toMartian = totalDays / 1.027;
  336.   int marsYear = toMartian / 672 + 1900;
  337.   int marsMonth = (toMartian - (toMartian / 672 * 672)) / 56 + 1;
  338.   int marsDay = (toMartian - ((marsYear-1900)*672) - ((marsMonth-1)*56));
  339.   return { marsYear, marsMonth, marsDay };
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement