私は時計として機能する関数を書いています。それは時間(入力されている)に1秒を追加し、軍事時間の新しい時間を与えます。clockKeeper機能。プログラムは動作しますが、値は間違った順序で出力されますか?
新しい時刻が真夜中を過ぎた場合は、月、年、およびうるう年を考慮して日付を更新します(翌日)。
プログラムは完全にALMOSTを実行します。
更新時間は真夜中の前にある場合、それはすべてこのように、正常に動作します:
わかりましたので、あなたの日付を選択します。 22/07/2016 は、今すぐあなたの時間を選択します: 午前21時53分: 22 これは新しい時刻さ:午前21時53分23秒 これは日付:22/7/2016
しかし、それは過去の深夜である場合には、時刻と日付が間違った順序でプリントアウトされ、このように:
わかりましたので、あなたの日付を選択します。 21/07/2016 今、あなたの時間を選択します。 午後11時59分59秒 これは新しいの時間です:7:22:2016 これは日付がある:0/0/0
また、どのように曜日と月が切り替えられたかにも注目してください。
これは私にかなりの時間をかなり混乱させてしまいました。私は何か提案をお願いします。
#include <stdio.h>
#include <stdbool.h>
typedef struct
{
int hours;
int minutes;
int seconds;
int day;
int month;
int year;
}dateAndTime;
// Function Declarations
dateAndTime timeUpdate (dateAndTime);
dateAndTime dateUpdate (dateAndTime);
dateAndTime clockKeeper (dateAndTime);
int numberOfDays (dateAndTime);
bool isLeapYear(dateAndTime);
int main (void)
{
// Two functions of struct dateAndTime.
dateAndTime timeDate, newTimeDate;
// Enter in dates and time
printf("Okay, so choose your date:\n");
scanf("%i/%i/%i", &timeDate.day, &timeDate.month, &timeDate.year);
printf("Now choose your time:\n");
scanf("%i:%i:%i", &timeDate.hours, &timeDate.minutes, &timeDate.seconds);
// Call clockKeeper function, store new values in newTimeDate
newTimeDate = clockKeeper(timeDate);
//print updated time and date
printf("This is the new time: %i:%i:%i\n", newTimeDate.hours, newTimeDate.minutes, newTimeDate.seconds);
printf("This is the date: %i/%i/%i\n", newTimeDate.day, newTimeDate.month, newTimeDate.year);
}
// This function calls the timeUpdate function. If it's the new time is past midnight, call dateUpdate function
dateAndTime clockKeeper (dateAndTime timeDate)
{
dateAndTime newTimeDate = timeUpdate (timeDate);
if(newTimeDate.hours < timeDate.hours)
{
newTimeDate = dateUpdate(timeDate);
}
return newTimeDate;
}
// Update time
dateAndTime timeUpdate (dateAndTime now)
{
now.seconds++;
if(now.seconds == 60)
{
now.seconds = 0;
now.minutes++;
if(now.minutes == 60)
{
now.minutes = 0;
now.hours++;
if(now.hours == 24)
{
now.hours = 0;
}
}
}
return now;
}
// Update date
dateAndTime dateUpdate (dateAndTime date)
{
dateAndTime newDate;
// call function to check how many days are in the month
int numberOfDays (dateAndTime d);
if(date.day != numberOfDays (date))
{
newDate = (dateAndTime) {date.month, date.day + 1, date.year};
}
else if(date.month == 12)
{
newDate = (dateAndTime) {1, 1, date.year + 1};
}
else
{
newDate = (dateAndTime) {date.month + 1, 1, date.year};
}
return newDate;
}
int numberOfDays (dateAndTime d)
{
int days;
//call function to check if it's a leap year so amount of days in february can be upated.
bool isLeapYear (dateAndTime d);
const int daysPerMonth[12] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear (d) && d.month == 2)
{
days = 29;
}
else
{
days = daysPerMonth[d.month - 1];
}
return days;
}
bool isLeapYear(dateAndTime d)
{
bool leapYearFlag;
if ((d.year % 4 == 0 && d.year % d.year % 100 != 0) || d.year % 400 == 0)
{
leapYearFlag = true;
}
else
{
leapYearFlag = false;
}
return leapYearFlag;
}
ありがとう:
は、ここに私のコードです!あなたは基本的にあなたがやるべきことは何0
に他の3デフォルトの初期化を残して、これらの3つの整数があなたのdateAndTimeの構造体の最初の3つの整数にコピーする必要があることを言っている
デバッガを使用する絶好の機会です。 – alk
私は 'newDate =(dateAndTime){date.month、date.day + 1、date.year};のようなポジティブな行です。私はあなたのコードの領域を確認することをお勧めします。また、 'newTimeDate = dateUpdate(timeDate);'もあなたの更新された時刻値を使用していないことを確信しています。 'newTimeDate = dateUpdate(newTimeDate);でなければなりません。 – WhozCraig
ああ、どうやってそれを行うのか分かりません。私は「Programming in C」の第8章(Structures)にあります。最後の演習を終えてから、文字列に移ります。 デバッグは第17章です。著者は、各章の演習が完了するまで先読みしないよう特別に頼んでいます:s – gloopit