Code
One library, two programs
Now a second program needs the module: a lecture timetable. It asks when the first lecture starts and how many lectures there are, and prints the time of each one: a lecture lasts 90 min. and a break 10 min.
daytime as a library
Instead of putting daytime.c into both programs' file lists, it becomes a library and both programs use it. CMakeLists.txt:
add_library(p07_daytime STATIC daytime.c)
target_include_directories(p07_daytime PUBLIC .)
target_link_libraries(p07_daytime PRIVATE course_input)
add_executable(p07_duration main.c)
target_link_libraries(p07_duration PRIVATE p07_daytime)
add_executable(p07_timetable timetable.c)
target_link_libraries(p07_timetable PRIVATE p07_daytime course_input)
- The library itself uses
course_input:read_timecallsread_int. p07_durationno longer hasdaytime.cin its list, norcourse_input:main.cdoesn't callread_intdirectly.p07_timetablecallsread_intitself, so it needscourse_inputtoo.
daytime.c is compiled once, even though two programs use it.
timetable.c
LECTURE_MIN 90,BREAK_MIN 10,MAX_LECTURES 6– constants.- The start comes from
read_time(), the count is 1 to 6. - The loop prints
1. 08:30-10:00and addsLECTURE_MIN + BREAK_MINto the start. - The loop also stops when the next lecture no longer fits into the day:
start + LECTURE_MIN < MINUTES_PER_DAY. That constant comes fromdaytime.h.
The program doesn't know how print_time prints the leading zero or how a time is checked: daytime.h is enough for it.
Run it
p07_timetable and ▶. Enter 8, 30, 4:
Start of the first lecture
Hour: 8
Minutes: 30
How many lectures? 4
1. 08:30-10:00
2. 10:10-11:40
3. 11:50-13:20
4. 13:30-15:00
And evening ones: 20, 0, 9, then 5:
Start of the first lecture
Hour: 20
Minutes: 0
How many lectures? 9
There must be 1 to 6 lectures.
How many lectures? 5
1. 20:00-21:30
2. 21:40-23:10
The other lectures don't fit into this day.
Run p07_duration too: it works as before, only now with the library.
Commit: Build timetable on daytime.