c - 为什么另一个结构中的一个结构被更新,而另一个结构却没有更新?

标签 c structure

我想更新日期和时间结构。我正在使用 Stephen Kochan 的《编程》第四版中的示例:

“编写一个名为clockKeeper()的函数,它以本章中定义的dateAndTime结构作为参数。该函数应调用timeUpdate()函数,如果时间到达午夜,该函数应调用dateUpdate函数来切换到第二天。让函数返回更新后的 dateAndTime 结构。"

这对我来说是一个学习过程,似乎我不完全理解如何使用结构,有人可以告诉我问题出在哪里并提供简短的解释吗?该程序总是更新时间,但日期保持不变,我尝试进行更改,但无论我做什么,它都会给我一个编译错误。

#include <stdio.h>
#include <stdbool.h>

//does not work properly issues with - the date update
//Program to update date and time

struct time timeUpdate (struct time now);
struct date dateUpdate (struct date today);
struct dateAndTime  clockKeeper (struct dateAndTime  dt);

struct date
{
    int month;
    int day;
    int year;
};

struct time
{
    int hour;
    int minutes;
    int seconds;
};

struct dateAndTime 
{
    struct date   sdate;
    struct time   stime; 
};

struct dateAndTime  dt1 =
     {
         { 1, 11, 19 }, { 00, 00, 00 }
     };

bool  isLeapYear (struct date  d);
int  numberOfDays  (struct date  d);
int main(int argc, char const *argv[])
{
    printf ("Current date and time is %.2i/%.2i/%.2i "
        "%.2i:%.2i:%.2i\n",
        dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
        dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds);

    dt1 = clockKeeper (dt1);

    printf ("Updated date and time is %.2i/%.2i/%.2i "
        "%.2i:%.2i:%.2i\n\n", 
        dt1.sdate.month, dt1.sdate.day, dt1.sdate.year,
        dt1.stime.hour, dt1.stime.minutes, dt1.stime.seconds);
}

struct dateAndTime  clockKeeper (struct dateAndTime  dt)
{
     struct time  timeUpdate (struct time  now);
     struct date  dateUpdate (struct date  today);

     dt.stime = timeUpdate (dt.stime);
//looks like this is not working :(
    if ( dt.stime.hour == 0  &&  dt.stime.minutes == 0  &&
             dt.stime.seconds == 0 )
         dt.sdate = dateUpdate (dt.sdate);

        return  dt;
}

  //counting time
struct time timeUpdate (struct time now)
{
    ++now.seconds;

    if (now.seconds == 60 ) 
    {
            now.seconds = 0;
        ++now.minutes;
    }
        if( now.minutes == 60) 
        {

            now.minutes = 0;
            ++now.hour;
        }
            if( now.hour == 24) 
            {

                now.hour = 0;

            }



return now;
}

struct date dateUpdate (struct date today)
{
    struct date tomorrow;
    int numberOfDays (struct date d);

    if(today.day != numberOfDays (today))
    {
        tomorrow.day = today.day +1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
    }

    else if(today.month == 12) //end of year
    {
        tomorrow.day = 1;
        tomorrow.month = 1;
        tomorrow.year = today.year + 1;
    }
    else
    {
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }
    return tomorrow;
}

// Function to find the number of days in a month

 int  numberOfDays  (struct date  d)
{
      int days;
      bool  isLeapYear (struct date  d);

      const int   daysPerMonth[12] ={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

      if ( isLeapYear (d) == true &&  d.month == 2 )days = 29;
       else
           days = daysPerMonth[d.month - 1];

    return days; 
} 

bool  isLeapYear (struct date  d) 

{

bool  leapYearFlag;
    if ( (d.year % 4 == 0  &&  d.year % 100 != 0)  ||d.year % 400 == 0 )
        leapYearFlag = true;   // It's a leap year

        else

        leapYearFlag = false;  // Not a leap year

return leapYearFlag; 
}

当前结果: 当前日期和时间是 01/11/19 00:00:00 更新日期和时间为 01/11/19 00:00:01

我预计日期也会更新。

最佳答案

为什么你说你的代码不起作用?对我来说效果很好

01/11/19 00:00:00 一秒后就是 01/11/19 00:00:01,这一天不必改变

如果我更改初始时间:

struct dateAndTime  dt1 =
 {
     { 1, 11, 19 }, { 23, 59, 59 }
 };

给出:

Current date and time is 01/11/19 23:59:59
Updated date and time is 01/12/19 00:00:00

日期和时间都发生了变化

<小时/>

正如 Jonathan Leffler 在评论中所说,将函数声明删除到函数中

clockKeeper中删除这些行

  struct time  timeUpdate (struct time  now);
  struct date  dateUpdate (struct date  today);

并在dateUpdate中删除该行

int numberOfDays (struct date d);

您已经在文件顶部声明了函数

关于c - 为什么另一个结构中的一个结构被更新,而另一个结构却没有更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54295663/

相关文章:

c# - 如何在 C# 应用程序中转换 C 结构?

c - 为什么在这个 Linux 内核代码中截断参数?

c - 这个返回语句是什么意思

c - C是命令式还是声明式编程语言

php - 当字符串无效或在数据库中找到字符串时,如何引发PDO/MySQL错误?

c - 在c中哪里以及如何使用fwrite函数

c - 在 C 中将按位值显示为整数

c - 在C中打印带有月份和年份的日历

C++ 结构指针指向对象类型错误

c - 操作指向指针数组的指针