c-clockKeeper功能.程序'作品'但是值是按错误的顺序打印出来的



我已经编写了一个充当时钟的函数。它将在时间上增加一秒(即输入),然后将给出军事时间中的新时间。

如果新时间已经过了午夜,那么它将更新日期(第二天),同时考虑月份、年份以及是否是闰年。

这个程序运行得几乎完美。

如果更新的时间在午夜之前,那么一切都很好,就像这样:

好的,所以选择你的日期:2016年7月22日现在选择您的时间:21:53:22这是新时间:21:53:23这是日期:2016年7月22日

但如果已经过了午夜,那么时间和日期就会按错误的顺序打印出来,比如:

好的,所以选择你的日期:2016年7月21日现在选择您的时间:23: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:%in", newTimeDate.hours, newTimeDate.minutes, newTimeDate.seconds);
    printf("This is the date: %i/%i/%in", 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;
}

谢谢!

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

您基本上是说,这3个整数应该复制到dateAndTime结构的前3个整数中,剩下的3个默认值初始化为0。

你应该做的是

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

您的结构按正确的顺序打印;复合文字是错误的。

最新更新