Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Modules.

EDITTask 8 of 26 · step 4: The daytime moduleOnly main stays in main.c
Only main stays in main.c in practice/07/

Take out the five functions, the #define and the #include lines no longer needed, and add #include "daytime.h".

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
  • A multi-file program: .h files with Doxygen comments and their own CMake target.

Numbered notes in the code explain the important lines.

practice 07 main.c Code Diff +1 −45
M · MODIFIED git diff: red = old code, green = new.
--- a/practice/07/main.c
+++ b/practice/07/main.c
@@ -1,50 +1,6 @@
− #include <assert.h>
#include <stdio.h>
− #include "input.h"
−
− #define MINUTES_PER_DAY (24 * 60)
−
− /* Whether hour:min is a real time of day.
− After: 1 if 0 <= hour <= 23 and 0 <= min <= 59, otherwise 0. */
− int is_time(int hour, int min) {
− return hour >= 0 && hour <= 23 && min >= 0 && min <= 59;
− }
−
− /* Turns a time into minutes since midnight.
− Before: is_time(hour, min).
− After: 0 to MINUTES_PER_DAY - 1. */
− int to_minutes(int hour, int min) {
− assert(is_time(hour, min));
− return hour * 60 + min;
− }
−
− /* Asks for the hour and the minutes until a real time is entered.
− After: minutes since midnight. */
− int read_time(void) {
− int hour = read_int("Hour: ");
− int min = read_int("Minutes: ");
− while (!is_time(hour, min)) {
− printf("There is no such time.\n");
− hour = read_int("Hour: ");
− min = read_int("Minutes: ");
− }
− return to_minutes(hour, min);
− }
−
− /* Prints a time as HH:MM, without a newline.
− Before: 0 <= minutes < MINUTES_PER_DAY. */
− void print_time(int minutes) {
− assert(minutes >= 0 && minutes < MINUTES_PER_DAY);
− printf("%02d:%02d", minutes / 60, minutes % 60);
− }
−
− /* Prints a duration as "90 min (1 h 30 min)", without a newline.
− Before: minutes >= 0. */
− void print_duration(int minutes) {
− assert(minutes >= 0);
− printf("%d min (%d h %d min)", minutes, minutes / 60, minutes % 60);
− }
+ #include "daytime.h"
int main(void) {
printf("Start\n");