Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * production.c
- *
- * Created on: Jan 15, 2018
- * Author: Tyler Bouwens / trbouwens
- * @author Tyler Bouwens / trbouwens
- */
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "production.h"
- #include <math.h>
- /** Your production code goes in this function.
- *
- * @param argc Number of words on the command line
- * @param argv Array of pointers to character strings representing the words on the command line.
- * @return true if Program was able to print a calendar.
- * false if User entered bad input.
- */
- bool production(int argc, char* argv[])
- {
- bool results = false;
- bool done = false;
- //get the year, Usage as needed.
- int year = -1;
- puts("\nCALENDAR");
- if(argc<2)
- {
- puts("Enter a year");
- scanf("%d",&year);
- if(year<1752)
- {
- printf("Usage: Year should be greater than 1751, received %d.\n",year);
- done = true;
- }
- }
- else
- {
- char* ptr=0;
- long year_l = strtol(argv[1],&ptr,10);
- year = (int)year_l;
- if(year<1752)
- {
- printf("Usage: Year should be greater than 1751, received %d.\n",year);
- done = true;
- }
- }
- if(!done)
- {
- puts("January");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(0, year);
- puts("February");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(1, year);
- puts("March");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(2, year);
- puts("April");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(3, year);
- puts("May");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(4, year);
- puts("June");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(5, year);
- puts("July");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(6, year);
- puts("August");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(7, year);
- puts("September");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(8, year);
- puts("October");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(9, year);
- puts("November");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(10, year);
- puts("December");
- puts("Sun Mon Tue Wed Thu Fri Sat");
- printCalendarDates(11, year);
- }
- if(!done)
- {
- results=true;
- }
- return results;
- }
- /**
- *
- */
- void printCalendarDates(int month, int year) {
- int i = 0;
- int x = calculate_day_of_week(1, month, year);
- // Adds 5 lines of whitespace on every day prior to
- // the correct starting date of the 1st
- while(i < calculate_day_of_week(1, month, year)) {
- printf(" ");
- i++;
- }
- int j = 1;
- // This loops prints the calendar
- while (j <= calculate_days_in_month(year, month)) {
- // x is the counter responsible for keeping track
- // of what day of the week it is
- x++;
- // Checks if its the last day of the year, to add a space
- // for the next month name (formatting)
- if (j == calculate_days_in_month(year, month)){
- // Last date will always be two digits
- printf(" %d\n",j);
- }
- // If the date is not saturday, allow for the next date to
- // be printed right next to it
- else if (!(x % 7 == 0)) {
- // Gives two spaces before a number for single digit
- if (j < 10) {
- printf(" %d ",j);
- }
- // Gives one space before a number for double digit
- else {
- printf(" %d ",j);
- }
- }
- // If the date is on a satuday, this prints a newline character so that
- // the next dates are on the next line
- else {
- // Gives two spaces before a number for single digit w/ newline
- if (j < 10) {
- printf(" %d\n",j);
- }
- // Gives one space before a number for double digit w/ newline
- else {
- printf(" %d\n",j);
- }
- }
- j++;
- }
- printf("\n");
- }
- /** Calculates how many days are in a given month for a given year.
- * @param year The year we are checking.
- * @param month The month we are checking. Range 0 through 11, where January = 0.
- * @return Day of the week. Range 0 through 6, where Sunday = 0.
- * -1 if invalid input (e.g., year < 1752, month out of range).
- */
- int calculate_days_in_month(int year, int month)
- {
- int answer = -1;
- bool done = false;
- if (year < 1752) {
- answer = -1;
- }
- else if (month == 0 || month == 2 ||
- month == 4 || month == 6 ||
- month == 7 || month == 9 ||
- month ==11) {
- answer = 31;
- }
- else if (month == 3 || month == 5 ||
- month == 8 || month == 10) {
- answer = 30;
- }
- else if (month == 1 && is_leap_year(year) == 0) {
- answer = 28;
- }
- else if (month == 1 && is_leap_year(year) == 1) {
- answer = 29;
- }
- else {
- answer = -1;
- }
- return(answer);
- }
- /** Test if a given year is a leap year or not.
- * @param year The year we are testing
- * @return 1 if it is a valid leap year.
- * 0 if a valid year, but not a leap year.
- * -1 if not a valid year (i.e. before the calendar rule changed in 1752).
- */
- int is_leap_year(int year)
- {
- bool done = false;
- int ans = -2; //Start with an invalid answer
- if(year < 1752) {
- ans = -1;
- done = true;
- }
- else if(year % 100 == 0 && year % 400 == 0) {
- ans = 1;
- done = true;
- }
- else if(year % 100 == 0 && !(year % 400 == 0)) {
- ans = 0;
- done = true;
- }
- else if(year % 4 == 0) {
- ans = 1;
- done = true;
- }
- else {
- ans = 0;
- done = true;
- }
- return ans;
- }
- /** Determines what day of the week a particular date falls on.
- * @param day Day of the month, counting from 1.
- * @param month Range 0 through 11, where January = 0.
- * @param year The year.
- * @return -1 for invalid input (e.g., year < 1752, month out of range,
- * day < 1 or > appropriate for that month & year
- * 0 through 6, where Sunday = 0.
- *
- */
- int calculate_day_of_week(int day, //first day of month is 1
- int month, //uses 0 for January
- int year)
- {//invalid input gets a -1 answer
- int Y = -1;
- int y = -1;
- int c = -1;
- int d = day;
- int m = 0;
- int ans = -1;
- int cor = 0;
- bool done = false;
- // Assigning value to Y
- if (month == 0 || month == 1) {
- Y = year - 1;
- }
- else {
- Y = year;
- }
- y = Y % 100;
- // printf("y = %d\n", y);
- // Assign Value to c
- c = Y;
- while (c >= 100) {
- c /= 10;
- }
- //printf("c = %d\n", c);
- // Assigns value to m
- if (month == 0) {
- m = 11;
- }
- else if (month == 1) {
- m = 12;
- }
- else {
- m = month - 1;
- }
- // runs the final equation
- int a1 = floor((2.6 * m) - 0.2);
- int b1 = floor(y/4);
- int c1 = floor(c/4);
- //printf("%d\n",y);
- int z = (d + (a1) + y + (b1) + (c1) - (2 * c));
- ans = (z % 7);
- if (ans < 0) {
- ans += 7;
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement