Advertisement
Tyb0

CS Hw1 v2 (Final?)

Jan 19th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.34 KB | None | 0 0
  1. /*
  2.  * production.c
  3.  *
  4.  *  Created on: Jan 15, 2018
  5.  *      Author: Tyler Bouwens / trbouwens
  6.  *      @author Tyler Bouwens / trbouwens
  7.  */
  8.  
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "production.h"
  13. #include <math.h>
  14.  
  15. /** Your production code goes in this function.
  16.  *
  17.  * @param argc Number of words on the command line
  18.  * @param argv Array of pointers to character strings representing the words on the command line.
  19.  * @return true if Program was able to print a calendar.
  20.  *         false if User entered bad input.
  21.  */
  22.  
  23. bool production(int argc, char* argv[])
  24. {
  25.     bool results = false;
  26.     bool done = false;
  27.     //get the year, Usage as needed.
  28.     int year = -1;
  29.     puts("\nCALENDAR");
  30.     if(argc<2)
  31.     {
  32.         puts("Enter a year");
  33.         scanf("%d",&year);
  34.         if(year<1752)
  35.         {
  36.             printf("Usage: Year should be greater than 1751, received %d.\n",year);
  37.             done = true;
  38.         }
  39.     }
  40.     else
  41.     {
  42.         char* ptr=0;
  43.         long year_l = strtol(argv[1],&ptr,10);
  44.         year = (int)year_l;
  45.         if(year<1752)
  46.         {
  47.             printf("Usage: Year should be greater than 1751, received %d.\n",year);
  48.             done = true;
  49.         }
  50.     }
  51.     if(!done)
  52.     {
  53.         puts("January");
  54.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  55.         printCalendarDates(0, year);
  56.         puts("February");
  57.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  58.         printCalendarDates(1, year);
  59.         puts("March");
  60.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  61.         printCalendarDates(2, year);
  62.         puts("April");
  63.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  64.         printCalendarDates(3, year);
  65.         puts("May");
  66.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  67.         printCalendarDates(4, year);
  68.         puts("June");
  69.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  70.         printCalendarDates(5, year);
  71.         puts("July");
  72.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  73.         printCalendarDates(6, year);
  74.         puts("August");
  75.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  76.         printCalendarDates(7, year);
  77.         puts("September");
  78.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  79.         printCalendarDates(8, year);
  80.         puts("October");
  81.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  82.         printCalendarDates(9, year);
  83.         puts("November");
  84.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  85.         printCalendarDates(10, year);
  86.         puts("December");
  87.         puts("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
  88.         printCalendarDates(11, year);
  89.  
  90.  
  91.     }
  92.     if(!done)
  93.     {
  94.         results=true;
  95.     }
  96.     return results;
  97. }
  98.  
  99. /**
  100.  *
  101.  */
  102. void printCalendarDates(int month, int year) {
  103.     int i = 0;
  104.     int x = calculate_day_of_week(1, month, year);
  105.  
  106.     // Adds 5 lines of whitespace on every day prior to
  107.     // the correct starting date of the 1st
  108.     while(i < calculate_day_of_week(1, month, year)) {
  109.         printf("     ");
  110.         i++;
  111.     }
  112.  
  113.     int j = 1;
  114.     // This loops prints the calendar
  115.     while (j <= calculate_days_in_month(year, month)) {
  116.         // x is the counter responsible for keeping track
  117.         // of what day of the week it is
  118.         x++;
  119.         // Checks if its the last day of the year, to add a space
  120.         // for the next month name (formatting)
  121.         if (j == calculate_days_in_month(year, month)){
  122.             // Last date will always be two digits
  123.             printf(" %d\n",j);
  124.         }
  125.         // If the date is not saturday, allow for the next date to
  126.         // be printed right next to it
  127.         else if (!(x % 7 == 0)) {
  128.             // Gives two spaces before a number for single digit
  129.             if (j < 10) {
  130.                 printf("  %d  ",j);
  131.             }
  132.             // Gives one space before a number for double digit
  133.             else {
  134.                 printf(" %d  ",j);
  135.             }
  136.         }
  137.         // If the date is on a satuday, this prints a newline character so that
  138.         // the next dates are on the next line
  139.         else {
  140.             // Gives two spaces before a number for single digit w/ newline
  141.             if (j < 10) {
  142.                 printf("  %d\n",j);
  143.             }
  144.             // Gives one space before a number for double digit w/ newline
  145.             else {
  146.                 printf(" %d\n",j);
  147.             }
  148.         }
  149.         j++;
  150.     }
  151.     printf("\n");
  152.  
  153.  
  154. }
  155. /** Calculates how many days are in a given month for a given year.
  156.  * @param year The year we are checking.
  157.  * @param month The month we are checking. Range 0 through 11, where January = 0.
  158.  * @return Day of the week. Range 0 through 6, where Sunday = 0.
  159.  *         -1 if invalid input (e.g., year < 1752, month out of range).
  160.  */
  161. int calculate_days_in_month(int year, int month)
  162. {
  163.     int answer = -1;
  164.     bool done = false;
  165.     if (year < 1752) {
  166.         answer = -1;
  167.     }
  168.     else if (month == 0 || month == 2 ||
  169.              month == 4 || month == 6 ||
  170.              month == 7 || month == 9 ||
  171.              month ==11) {
  172.         answer = 31;
  173.     }
  174.     else if (month == 3 || month == 5 ||
  175.              month == 8 || month == 10) {
  176.         answer = 30;
  177.     }
  178.     else if (month == 1 && is_leap_year(year) == 0) {
  179.         answer = 28;
  180.     }
  181.     else if (month == 1 && is_leap_year(year) == 1) {
  182.         answer = 29;
  183.     }
  184.     else {
  185.         answer = -1;
  186.     }
  187.  
  188.  
  189.     return(answer);
  190. }
  191. /** Test if a given year is a leap year or not.
  192.  * @param year The year we are testing
  193.  * @return 1 if it is a valid leap year.
  194.  *         0 if a valid year, but not a leap year.
  195.  *        -1 if not a valid year (i.e. before the calendar rule changed in 1752).
  196.  */
  197.  
  198. int is_leap_year(int year)
  199. {
  200.     bool done = false;
  201.     int ans = -2; //Start with an invalid answer
  202.  
  203.     if(year < 1752) {
  204.         ans = -1;
  205.         done = true;
  206.     }
  207.     else if(year % 100 == 0 && year % 400 == 0) {
  208.         ans = 1;
  209.         done = true;
  210.     }
  211.     else if(year % 100 == 0 && !(year % 400 == 0)) {
  212.         ans = 0;
  213.         done = true;
  214.     }
  215.     else if(year % 4 == 0) {
  216.         ans = 1;
  217.         done = true;
  218.     }
  219.     else {
  220.         ans = 0;
  221.         done = true;
  222.     }
  223.  
  224.     return ans;
  225.  
  226. }
  227. /** Determines what day of the week a particular date falls on.
  228.  * @param day Day of the month, counting from 1.
  229.  * @param month Range 0 through 11, where January = 0.
  230.  * @param year The year.
  231.  * @return -1 for invalid input (e.g., year < 1752, month out of range,
  232.  *            day < 1 or > appropriate for that month & year
  233.  *         0 through 6, where Sunday = 0.
  234.  *
  235.  */
  236. int calculate_day_of_week(int day, //first day of month is 1
  237.         int month, //uses 0 for January
  238.         int year)
  239. {//invalid input gets a -1 answer
  240.     int Y = -1;
  241.     int y = -1;
  242.     int c = -1;
  243.     int d = day;
  244.     int m = 0;
  245.     int ans = -1;
  246.     int cor = 0;
  247.     bool done = false;
  248.  
  249.     // Assigning value to Y
  250.     if (month == 0 || month == 1) {
  251.         Y = year - 1;
  252.     }
  253.     else {
  254.         Y = year;
  255.     }
  256.  
  257.     y = Y % 100;
  258.     // printf("y = %d\n", y);
  259.  
  260.     // Assign Value to c
  261.     c = Y;
  262.     while (c >= 100) {
  263.         c /= 10;
  264.     }
  265.     //printf("c = %d\n", c);
  266.  
  267.     // Assigns value to m
  268.     if (month == 0) {
  269.         m = 11;
  270.     }
  271.     else if (month == 1) {
  272.         m = 12;
  273.     }
  274.     else {
  275.         m = month - 1;
  276.     }
  277.  
  278.     // runs the final equation
  279.     int a1 = floor((2.6 * m) - 0.2);
  280.     int b1 = floor(y/4);
  281.     int c1 = floor(c/4);
  282.     //printf("%d\n",y);
  283.     int z = (d + (a1) + y + (b1) + (c1) - (2 * c));
  284.     ans = (z % 7);
  285.  
  286.  
  287.     if (ans < 0) {
  288.         ans += 7;
  289.     }
  290.  
  291.     return ans;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement