Skip to content

Commit c178d67

Browse files
committed
chapter 9 exercises
1 parent dceec41 commit c178d67

File tree

5 files changed

+369
-0
lines changed

5 files changed

+369
-0
lines changed

ex.9.2.c

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Calculate the number of days between two arbitrary dates
2+
3+
#include <stdio.h>
4+
5+
struct date
6+
{
7+
int month;
8+
int day;
9+
int year;
10+
};
11+
12+
int yearInt (int year, int month)
13+
{
14+
if ( month <= 2 )
15+
return year - 1;
16+
else
17+
return year;
18+
}
19+
20+
int monthInt (int month)
21+
{
22+
if ( month <= 2 )
23+
return month + 13;
24+
else
25+
return month + 1;
26+
}
27+
28+
int dateInt (struct date d)
29+
{
30+
int yearN = 1461 * yearInt (d.year, d.month) / 4;
31+
int monthN = 153 * monthInt (d.month) / 5;
32+
33+
return yearN + monthN + d.day;
34+
}
35+
36+
int main (void)
37+
{
38+
struct date date1, date2;
39+
40+
printf ("Type first date (mm dd yyyy): ");
41+
scanf ("%i %i %i", &date1.month, &date1.day, &date1.year);
42+
43+
printf ("Type second date (mm dd yyyy): ");
44+
scanf ("%i %i %i", &date2.month, &date2.day, &date2.year);
45+
46+
int n1 = dateInt (date1);
47+
int n2 = dateInt (date2);
48+
49+
printf ("There are %i days between %i/%i/%.2i and %i/%i/%.2i\n", n2 - n1,
50+
date1.month, date1.day, date1.year % 100,
51+
date2.month, date2.day, date2.year % 100);
52+
53+
return 0;
54+
}

ex.9.3.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Calculate the elapsed time between two arbitrary times
2+
3+
#include <stdio.h>
4+
5+
struct time
6+
{
7+
int hour;
8+
int minutes;
9+
int seconds;
10+
};
11+
12+
struct time elapsed_time (struct time t1, struct time t2)
13+
{
14+
int secondsDiff =
15+
(t2.hour * 3600 + t2.minutes * 60 + t2.seconds) -
16+
(t1.hour * 3600 + t1.minutes * 60 + t1.seconds);
17+
18+
if ( secondsDiff < 0 )
19+
secondsDiff += 24 * 3600;
20+
21+
struct time elapsed = {
22+
.hour = secondsDiff / 3600,
23+
.minutes = (secondsDiff % 3600) / 60,
24+
.seconds = (secondsDiff % 3600) % 60
25+
};
26+
27+
return elapsed;
28+
}
29+
30+
int main (void)
31+
{
32+
struct time time1, time2;
33+
34+
printf ("Enter start time (hh:mm:ss): ");
35+
scanf ("%i:%i:%i", &time1.hour, &time1.minutes, &time1.seconds);
36+
37+
printf ("Enter end time (hh:mm:ss): ");
38+
scanf ("%i:%i:%i", &time2.hour, &time2.minutes, &time2.seconds);
39+
40+
struct time elapsed = elapsed_time (time1, time2);
41+
42+
printf ("\nThe time elapsed between %.2i:%.2i:%.2i and %.2i:%.2i:%.2i",
43+
time1.hour, time1.minutes, time1.seconds,
44+
time2.hour, time2.minutes, time2.seconds);
45+
46+
printf (" is %.2i:%.2i:%.2i\n",
47+
elapsed.hour, elapsed.minutes, elapsed.seconds);
48+
49+
return 0;
50+
}

ex.9.4.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Calculate the day of the week for an arbitrary date
2+
3+
#include <stdio.h>
4+
5+
struct date
6+
{
7+
int month;
8+
int day;
9+
int year;
10+
};
11+
12+
int yearInt (int year, int month)
13+
{
14+
if ( month <= 2 )
15+
return year - 1;
16+
else
17+
return year;
18+
}
19+
20+
int monthInt (int month)
21+
{
22+
if ( month <= 2 )
23+
return month + 13;
24+
else
25+
return month + 1;
26+
}
27+
28+
int weekDayInt (struct date d)
29+
{
30+
int yearN = 1461 * yearInt (d.year, d.month) / 4;
31+
int monthN = 153 * monthInt (d.month) / 5;
32+
33+
return (yearN + monthN + d.day - 621049) % 7;
34+
}
35+
36+
int main (void)
37+
{
38+
const char weekDays[7][10] = {
39+
"Sunday", "Monday", "Tuesday", "Wednesday",
40+
"Thursday", "Friday", "Saturday"
41+
};
42+
struct date d;
43+
44+
printf ("Which date (mm dd yyyy) do you want to know about? ");
45+
scanf ("%i %i %i", &d.month, &d.day, &d.year);
46+
47+
int index = weekDayInt (d);
48+
49+
printf ("%i/%i/%.2i falls on a %s\n",
50+
d.month, d.day, d.year % 100, weekDays[index]);
51+
52+
return 0;
53+
}

ex.9.5.c

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// Program to update date and time
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+
struct time
14+
{
15+
int hour;
16+
int minutes;
17+
int seconds;
18+
};
19+
20+
struct dateAndTime
21+
{
22+
struct date sdate;
23+
struct time stime;
24+
};
25+
26+
bool isMidnight (struct time t)
27+
{
28+
return t.hour == 0 && t.minutes == 0 && t.seconds == 0;
29+
}
30+
31+
struct time timeUpdate (struct time t)
32+
{
33+
struct time updated = {
34+
.hour = t.hour, .minutes = t.minutes, .seconds = t.seconds + 1
35+
};
36+
37+
if ( updated.seconds == 60 ) {
38+
updated.seconds = 0;
39+
updated.minutes += 1;
40+
41+
if ( updated.minutes == 60 ) {
42+
updated.minutes = 0;
43+
updated.hour += 1;
44+
45+
if ( updated.hour == 24 ) {
46+
updated.hour = 0;
47+
}
48+
}
49+
}
50+
51+
return updated;
52+
}
53+
54+
struct date dateUpdate (struct date today)
55+
{
56+
struct date tomorrow;
57+
int numberOfDays (struct date d);
58+
59+
if ( today.day != numberOfDays (today) ) {
60+
tomorrow = (struct date) { today.month, today.day + 1, today.year };
61+
}
62+
else if ( today.month == 12 ) { // end of year
63+
tomorrow = (struct date) { 1, 1, today.year + 1 };
64+
}
65+
else { // end of month
66+
tomorrow = (struct date) { 1, today.month + 1, today.year };
67+
}
68+
69+
return tomorrow;
70+
}
71+
72+
// Function to find the number of days in a month
73+
74+
int numberOfDays (struct date d)
75+
{
76+
int days;
77+
bool isLeapYear (struct date d);
78+
79+
const int daysPerMonth[12] = {
80+
31, 28, 31, 30, 31, 30,
81+
31, 31, 30, 31, 30, 31
82+
};
83+
84+
if ( isLeapYear (d) == true && d.month == 2 )
85+
days = 29;
86+
else
87+
days = daysPerMonth[d.month - 1];
88+
89+
return days;
90+
}
91+
92+
// Function to determine if it's a leap year
93+
94+
bool isLeapYear (struct date d)
95+
{
96+
bool leapYearFlag;
97+
98+
if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 )
99+
leapYearFlag = true; // It's a leap year
100+
else
101+
leapYearFlag = false; // Not a leap year
102+
103+
return leapYearFlag;
104+
}
105+
106+
struct dateAndTime clockKeeper (struct dateAndTime datetime)
107+
{
108+
struct dateAndTime updated = { .stime = timeUpdate (datetime.stime) };
109+
110+
if ( isMidnight (updated.stime) )
111+
updated.sdate = dateUpdate (datetime.sdate);
112+
else
113+
updated.sdate = datetime.sdate;
114+
115+
return updated;
116+
}
117+
118+
int main (void)
119+
{
120+
struct dateAndTime datetime;
121+
122+
printf ("Enter starting datetime (mm dd yyyy hh:mm:ss): ");
123+
scanf ("%i %i %i %i:%i:%i",
124+
&datetime.sdate.month, &datetime.sdate.day, &datetime.sdate.year,
125+
&datetime.stime.hour, &datetime.stime.minutes, &datetime.stime.seconds);
126+
127+
struct dateAndTime updated = clockKeeper (datetime);
128+
129+
printf ("\nThe updated datetime is %i/%i/%.2i %.2i:%.2i:%.2i\n",
130+
updated.sdate.month, updated.sdate.day, updated.sdate.year,
131+
updated.stime.hour, updated.stime.minutes, updated.stime.seconds);
132+
133+
return 0;
134+
}

ex.9.6.c

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
else if ( today.month == 12 ) // end of year
23+
tomorrow = (struct date) { 1, 1, today.year + 1 };
24+
else // end of month
25+
tomorrow = (struct date) { today.month + 1, 1, today.year };
26+
27+
return tomorrow;
28+
}
29+
30+
// Function to find the number of days in a month
31+
32+
int numberOfDays (struct date d)
33+
{
34+
int days;
35+
bool isLeapYear (struct date d);
36+
37+
const int daysPerMonth[12] = {
38+
31, 28, 31, 30, 31, 30,
39+
31, 31, 30, 31, 30, 31
40+
};
41+
42+
if ( isLeapYear (d) == true && d.month == 2 )
43+
days = 29;
44+
else
45+
days = daysPerMonth[d.month - 1];
46+
47+
return days;
48+
}
49+
50+
// Function to determine if it's a leap year
51+
52+
bool isLeapYear (struct date d)
53+
{
54+
bool leapYearFlag;
55+
56+
if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0 )
57+
leapYearFlag = true; // It's a leap year
58+
else
59+
leapYearFlag = false; // Not a leap year
60+
61+
return leapYearFlag;
62+
}
63+
64+
int main (void)
65+
{
66+
struct date dateUpdate (struct date today);
67+
struct date thisDay, nextDay;
68+
69+
printf ("Enter today's date (mm dd yyyy): ");
70+
scanf ("%i%i%i", &thisDay.month, &thisDay.day, &thisDay.year);
71+
72+
nextDay = dateUpdate (thisDay);
73+
74+
printf ("Tomorrow's date is %i/%i/%.2i.\n", nextDay.month,
75+
nextDay.day, nextDay.year % 100);
76+
77+
return 0;
78+
}

0 commit comments

Comments
 (0)