这就是我到目前为止所拥有的。我正在学习结构,我不得不使用结构来完成作业。我不明白我的代码有什么问题。当我调试时,我得到 0 代表小时、分钟、秒。请看我的代码。
#include <stdio.h>
struct calculate
{
int hours;
int minutes;
int seconds;
};
struct calculate N1;
struct calculate N2;
int resulthr, resultmin, resultsec;
int substract(struct calculate N1, struct calculate N2)
{
resultsec = N2.seconds - N1.seconds;
if (resultsec < 0)
{
resultsec = 60 + resultsec;
--N2.minutes;
}
resultmin = N2.minutes - N1.minutes;
if (resultmin < 0)
{
resultsec = 60 + resultmin;
--N2.hours;
}
resulthr = N2.hours - N1.hours;
if (resulthr < 0)
{
resulthr = 24 + resultsec;
}
return 0;
}
int main(void)
{
printf("Enter two times.(hh:mm:ss) ");
scanf("%i:%i:%i", &N1.hours, &N1.minutes, &N1.seconds);
scanf("%i:%i:%i", &N2.hours, &N2.minutes, &N2.seconds);
int substract(calculate N1, calculate N2);
printf("The elasped time is %i:%i:%i.\n", resulthr, resultmin, resultsec);
return 0;
}