-
Notifications
You must be signed in to change notification settings - Fork 0
/
c11p11.c
68 lines (68 loc) · 1.54 KB
/
c11p11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Given the definition of a date structure as defined
//in this chapter, write a function called dateUpdate that
//takes a pointer to a date structure as its argument and
//that updates the structure to the following day
#include<stdio.h>
#include<stdbool.h>
struct date
{
int day;
int month;
int year;
};
int numberOfDays(struct date *d);
bool isLeapYear(struct date *d);
struct date dateUpdate(struct date *d);
int main(void)
{
struct date today,nextDay;
struct date *todayPtr;
todayPtr=&today;
printf("enter today's date (MM DD YYYY): ");
scanf("%i %i %i",&today.month,&today.day,&today.year);
nextDay=dateUpdate(todayPtr);
printf("Tomorrow's date is %d/%d/%d \n",nextDay.month,nextDay.day,nextDay.year);
return 0;
}
struct date dateUpdate(struct date *today)
{
struct date tomorrow;
if(today->day!=numberOfDays(today))
{
tomorrow.day=today->day+1;
tomorrow.month=today->month;
tomorrow.year=today->year;
}
else if(today->month==12)
{
tomorrow.day=1;
tomorrow.month=1;
tomorrow.year=today->year+1;
}
else
{
tomorrow.day=1;
tomorrow.month=today->month+1;
tomorrow.year=today->year;
}
return tomorrow;
}
int numberOfDays(struct date *d)
{
const int daysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int days;
if(isLeapYear(d)==true && d->month==2)
days=29;
else
days=daysPerMonth[d->month-1];
return days;
}
bool isLeapYear(struct date *d)
{
bool leap;
if((d->year%4==0 && d->year%100!=0)||d->year%400==0)
leap=true;
else
leap=false;
return leap;
}