如何在C编程中将代码划分为函数

ver*_*röm 3 c function

我有一个完美的运行代码,但这项作业的标准之一是它至少具有两个不同的功能。我怎样才能将此代码用于其他功能?

我想将alarmClock()函数排序为更多函数。那里有很多事。也许是updateTime()函数。我尝试过类似的操作,但这不起作用:

#include <stdio.h>

void alarmClock(int, int);
void updateTime(int, int, int);

int main() {
  int present_time;
  int time_for_alarm;

  printf("Time indicates in HHMMSS! \nPresent time: ");
  scanf("%d", &present_time);

  printf("Time for alarm: ");
  scanf("%d", &time_for_alarm);

  if (present_time == time_for_alarm)
    printf("ALARM!");
  else
    alarmClock(present_time, time_for_alarm);

  return 0;
}

void alarmClock(int presT, int alarmT) {
  int presentHH = presT / 10000;
  int presentMM = (presT / 100) % 100;
  int presentSS = presT % 100;

  int combineTime;

  while (presT != alarmT) {
    printf("%02d:%02d:%02d \n", presentHH, presentMM, presentSS);
    presentSS++;
    updateTime(presentHH, presentMM, presentSS);
    combineTime = presentHH * 100 + presentMM;
    presT = combineTime * 100 + presentSS;
  }
  printf("ALARM!");
}

void updateTime(int presentHH, int presentMM, int presentSS) {
  if (presentSS > 59) {
    presentSS = 0;
    presentMM++;
    if (presentMM > 59) {
      presentMM = 0;
      presentHH++;
      if (presentHH > 24) {
        presentHH = 1;
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我的老师提示我说“您可以使用printTime()函数和一个updateTime()函数来发送present_time作为参数”。但是我不知道怎么...

这是我的工作代码,至少还需要一个功能。

#include <stdio.h>

void alarmClock(int, int);

int main() {
  int present_time;
  int time_for_alarm;

  printf("Time indicates in HHMMSS! \nPresent time: ");
  / scanf("%d", &present_time);
  /

      printf("Time for alarm: ");
  scanf("%d", &time_for_alarm);

  if (present_time == time_for_alarm)
    printf("ALARM!");
  else
    alarmClock(present_time, time_for_alarm);

  return 0;
}

void alarmClock(int presT, int alarmT) {
  int presentHH = presT / 10000;
  int presentMM = (presT / 100) % 100;
  int presentSS = presT % 100;

  int combineTime;

  while (presT != alarmT) {
    printf("%02d:%02d:%02d \n", presentHH, presentMM, presentSS);
    presentSS++;

    if (presentSS > 59) {
      presentSS = 0;
      presentMM++;
      if (presentMM > 59) {
        presentMM = 0;
        presentHH++;
        if (presentHH > 24) {
          presentHH = 1;
        }
      }
    }
    combineTime = presentHH * 100 + presentMM;
    presT = combineTime * 100 + presentSS;
  }
  printf("ALARM!");
}
Run Code Online (Sandbox Code Playgroud)

工作代码给出了这个输出(正确的输出);

if present_time = 115957
and time_for_alarm = 120001
Run Code Online (Sandbox Code Playgroud)

输出是11:59:57 11:59:58 11:59:59 12:00:00警报

但是当我创建updateTime()函数时,如果我具有以下值,代码将永远运行:

if present_time = 115957
and time_for_alarm = 120001
Run Code Online (Sandbox Code Playgroud)

输出是11:59:57 11:59:58 11:59:59 11:59:60 11:59:61 11:59:62 11:59:63 ...依此类推(presentSS持续不断+永远= 1)

dbu*_*ush 5

变量presentHHpresentMM以及presentSS在功能上updateTime是从那些不同的alarmClock,在这样的变化对这些变量updateTime在调用函数是不可见的。

您需要传递每个变量的地址,并取消引用这些指针updateTime。然后,您实际上正在更改中定义的变量alarmClock

因此,将的定义更改updateTime为:

void updateTime(int *presentHH, int *presentMM, int *presentSS);
Run Code Online (Sandbox Code Playgroud)

并这样称呼它:

updateTime(&presentHH, &presentMM, &presentSS);
Run Code Online (Sandbox Code Playgroud)

您还需要更改的主体updateTime以反映参数现在是指针并取消引用它们。我将其留给读者练习。