1
//import library
#include <stdio.h>
#include <stdlib.h>

//declare variable structure
struct time{
    int hour;
    int min;
    int sec;
}startTime, endTime, different, elapsed;

//mould struct and compute elapsedTime
struct time elapsedTime(struct time start, struct time end){

    int secondStart, secondEnd, secondDif;

    secondEnd = end.hour * 60 * 60 + end.min * 60 + end.sec;
    secondStart = start.hour * 60 * 60 + start.min * 60 + start.sec;
    if (secondEnd>secondStart)
        secondDif = secondEnd - secondStart;
    else
        secondDif = secondStart - secondEnd;

    different.hour = secondDif / 60 / 60;
    different.min = secondDif / 60;

    return different;
}

//main function
void main(){
    printf("Enter start time (Hour Minute Second) using 24 hours system : ");
    scanf("%d %d %d", startTime.hour, startTime.min, startTime.sec);

    printf("Enter end time (Hour Minute Second) using 24 hours system : ");
    scanf("%d %d %d", endTime.hour, endTime.min, endTime.sec);

    elapsed = elapsedTime(startTime, endTime);
}

誰かがコードをチェックして実行し、それが機能しているかどうかを確認するのを手伝ってもらえますか?

4

2 に答える 2

1

int *main 関数に誤りがあります。代わりに scanf で使用する必要があるintため、以下に示すように追加する必要があり&ます。

//main function
void main(){
    printf("Enter start time (Hour Minute Second) using 24 hours system : ");
    scanf("%d %d %d", &startTime.hour, &startTime.min, &startTime.sec);

    printf("Enter end time (Hour Minute Second) using 24 hours system : ");
    scanf("%d %d %d", &endTime.hour, &endTime.min, &endTime.sec);

    elapsed = elapsedTime(startTime, endTime);
}
于 2015-12-30T10:10:05.450 に答える
0

計算したいとdifferent.sec思うので、異なる時間の正しい計算は次のとおりだと思います。

secPerHr = 60 * 60;
secPerMin = 60;

if (secondDif >= secPerHr) {
different.hour = secondDif / secPerHr;
secondDif -= different.hour * secPerHr;
}
if (secondDif >= secPerMin) {
different.min = secondDif / secPerMin;
secondDif -= different.min * secPerMin;
}
different.sec = secondDif;

また、テスト目的で、メイン関数で結果を表示したい場合があります。

于 2015-12-30T11:55:33.153 に答える