私は、2つの所与の時間の時間差を計算する次のコードを書いています。しかし、この仕事は真夜中を越えることを認識していることも求めた。デルタが正の値として報告されていることを確認することを除いて、コード内でそれを本当に扱う方法は考えられません。私もオンラインで検索し、他のコードも見つけましたが、これはそれ以上のことはしていません。ご注意ください;私はアルゴリズムについて尋ねています。私はこれを行う関数を探していません。ここに私のコードは次のとおりです。ここで2つの異なる時間の時間差を計算する - ミリ秒の時間
struct time {
int hour;
int minutes;
int seconds;
};
int main (void)
{
struct time timeDiff (struct time now, struct time later);
struct time result, one, two;
printf("Enter the first time (hh:mm:sec): ");
scanf("%i:%i:%i", &one.hour, &one.minutes, &one.seconds);
printf("Enter the second time (hh:mm:sec): ");
scanf("%i:%i:%i", &two.hour, &two.minutes, &two.seconds);
result = timeDiff(one, two);
printf("Time is: %.2i:%.2i:%.2i\n", result.hour, result.minutes, result.seconds);
return 0;
}
struct time timeDiff (struct time now, struct time later)
{
struct time timeDiff;
timeDiff.hour = later.hour - now.hour;
timeDiff.minutes = later.minutes - now.minutes;
timeDiff.seconds = later.seconds - now.seconds;
return timeDiff;
}
は、私はオンラインを見つけたコードは次のとおりです。
#include <stdio.h>
struct time
{
int hour;
int minute;
int second;
};
int main(void)
{
struct time time3;
//struct time get_time(struct time d);
//struct time elapsed_time(struct time d, struct time e);
int convert_to_seconds(struct time d);
int elapsed_time(int d, int e);
struct time conver_to_normal_time(int a);
struct time time1 = { 3, 45,15};
struct time time2 = { 9, 44, 03};
int a, b, c;
a = convert_to_seconds(time1);
b = convert_to_seconds(time2);
c = elapsed_time(a, b);
time3 = conver_to_normal_time(c);
printf(" %d:%d:%d", time3.hour, time3.minute, time3.second);
return 0;
}
struct time get_time(struct time d)
{
printf("Give me the time\n");
scanf(" %d:%d:%d", &d.hour, &d.minute, &d.second);
}
int convert_to_seconds(struct time d)
{
struct time time1_seconds;
int totalTime1_seconds;
time1_seconds.hour = d.hour * 3600;
time1_seconds.minute = d.second*60;
time1_seconds.second = d.second;
totalTime1_seconds = time1_seconds.hour + time1_seconds.minute + time1_seconds.second;
return totalTime1_seconds;
totalTime1_seconds = time1_seconds.hour + time1_seconds.minute + time1_seconds.second;
return totalTime1_seconds;
}
int elapsed_time(int d, int e)
{
int result;
result = d - e;
return result;
}
struct time conver_to_normal_time(int a)
{
struct time final_elapse_time;
final_elapse_time.hour = a/3600;
final_elapse_time.minute = (a/60) % 60;
final_elapse_time.second = a % 60;
return final_elapse_time;
}
あなたの問題(デルタ記号)の解決策があるように聞こえるので、何が問題なのですか? –
「借り入れ」を考慮せずに独自の時間減算を行いました。それが数百、数十、単位であれば、それを減算する方法を知っているでしょう。秒間、分から60秒を "借り"ます。それを紙の上で試してみてください。 –
こんにちはスコット、はい、私はこれを処理するための最良の方法であるかどうか、私は間違った前提をしていたかどうかを確認しようとしていた。ベーンのコメントが助けになりました。 – maverick