|
| 1 | +// Program to determine tomorrow's date |
| 2 | + |
| 3 | +#include <stdio.h> |
| 4 | +#include <stdbool.h> |
| 5 | + |
| 6 | +struct date |
| 7 | +{ |
| 8 | + int month; |
| 9 | + int day; |
| 10 | + int year; |
| 11 | +}; |
| 12 | + |
| 13 | +// Function to calculate tomorrow's date |
| 14 | + |
| 15 | +struct date dateUpdate (struct date today) |
| 16 | +{ |
| 17 | + struct date tomorrow; |
| 18 | + int numberOfDays (struct date d); |
| 19 | + |
| 20 | + if ( today.day != numberOfDays (today) ) { |
| 21 | + tomorrow = (struct date) { today.month, today.day + 1, today.year }; |
| 22 | + } |
| 23 | + else if ( today.month == 12 ) { // end of year |
| 24 | + tomorrow = (struct date) { 1, 1, today.year + 1 }; |
| 25 | + } |
| 26 | + else { // end of month |
| 27 | + tomorrow = (struct date) { 1, today.month + 1, today.year }; |
| 28 | + } |
| 29 | + |
| 30 | + return tomorrow; |
| 31 | +} |
| 32 | + |
| 33 | +// Function to find the number of days in a month |
| 34 | + |
| 35 | +int numberOfDays (struct date d) |
| 36 | +{ |
| 37 | + int days; |
| 38 | + bool isLeapYear (struct date d); |
| 39 | + |
| 40 | + const int daysPerMonth[12] = { |
| 41 | + 31, 28, 31, 30, 31, 30, |
| 42 | + 31, 31, 30, 31, 30, 31 |
| 43 | + }; |
| 44 | + |
| 45 | + if ( isLeapYear (d) == true && d.month == 2 ) |
| 46 | + days = 29; |
| 47 | + else |
| 48 | + days = daysPerMonth[d.month - 1]; |
| 49 | + |
| 50 | + return days; |
| 51 | +} |
| 52 | + |
| 53 | +// Function to determine if it's a leap year |
| 54 | + |
| 55 | +bool isLeapYear (struct date d) |
| 56 | +{ |
| 57 | + bool leapYearFlag; |
| 58 | + |
| 59 | + if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 ) |
| 60 | + leapYearFlag = true; // It's a leap year |
| 61 | + else |
| 62 | + leapYearFlag = false; // Not a leap year |
| 63 | + |
| 64 | + return leapYearFlag; |
| 65 | +} |
| 66 | + |
| 67 | +int main (void) |
| 68 | +{ |
| 69 | + struct date dateUpdate (struct date today); |
| 70 | + struct date thisDay, nextDay; |
| 71 | + |
| 72 | + printf ("Enter today's date (mm dd yyyy): "); |
| 73 | + scanf ("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year); |
| 74 | + |
| 75 | + nextDay = dateUpdate (thisDay); |
| 76 | + |
| 77 | + printf ("Tomorrow's date is %i/%i/%.2i.\n", nextDay.month, |
| 78 | + nextDay.day, nextDay.year % 100); |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
0 commit comments