我想同时更新日期和时间结构。我正在使用Stephen Kochan编写的第4版编程中的示例:
“编写一个名为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
我希望日期也会更新。
答案 0 :(得分:1)
您为什么说您的代码不起作用?对我来说效果很好
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);
您已经在文件顶部声明了函数