2016-07-22 8 views
1

私は時計として機能する関数を書いています。それは時間(入力されている)に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つの整数にコピーする必要があることを言っている

+0

デバッガを使用する絶好の機会です。 – alk

+1

私は 'newDate =(dateAndTime){date.month、date.day + 1、date.year};のようなポジティブな行です。私はあなたのコードの領域を確認することをお勧めします。また、 'newTimeDate = dateUpdate(timeDate);'もあなたの更新された時刻値を使用していないことを確信しています。 'newTimeDate = dateUpdate(newTimeDate);でなければなりません。 – WhozCraig

+0

ああ、どうやってそれを行うのか分かりません。私は「Programming in C」の第8章(Structures)にあります。最後の演習を終えてから、文字列に移ります。 デバッグは第17章です。著者は、各章の演習が完了するまで先読みしないよう特別に頼んでいます:s – gloopit

答えて

1
newDate = (dateAndTime) {date.month + 1, 1, date.year}; 

です
newDate = (dateAndTime) {date.hours, date.minutes, date.seconds, date.month + 1, 1, date.year}; 

構造体は正しい順序で印刷されます。複合リテラルは間違っています。

+0

ありがとう、それは完璧な意味があります... しかし、上記の@WhozCraigに私の最後のコメントを見てください。私はあなたが提案した修正を行わずにプログラムを稼働させることができました。 私はあなたが正しいことを知っているので、私には意味がありません! これを説明できますか? – gloopit

+0

@MattClearyは、私の変更なしでコンパイルした場合でも、私のコンパイラで間違った値を出力してしまうので、事故の可能性があります。 –

+0

@MattClearyしかし、正しいプログラムの実行には両方とも必須であるため、両方の修正を含める必要があります。 –

関連する問題