您好,我刚开始在大学学习C语言。现在我遇到了一个我不知道如何解决的问题。到目前为止我们只使用过 库而且没有别的,scanf也是我们要去的主题 下周学习。现在我们只使用printf来做所有事情。 我已经学会了如何判断一年是否是闰年,但是,我的任务是下一个:需要创建一个读取日期的程序并打印出第二天的日期输出如下:
Enter a date in the form day month year: 17 5 2010
The date of the next day is: 18/5/2010
我的困境是我不知道要使用什么操作或如何设置代码以确保考虑闰年,例如,如果今天的日期是28 2 2010,则下一个日期需要为2010年1月1日,因为这不是闰年。 唯一使用的库是,还没有scanf(还有scanf) 到目前为止我得到了这个:
#include <stdio.h>
int day, month, year, ndays, leapdays;
bool leapyear;
int main () {
day = 28;
month = 2;
year = 2010;
ndays = day + 1;
leapdays = 31;
leapyear = false;
if (leapyear % 4 == 0) {
leapyear = true;
}
if (leapyear % 100 == 0) {
leapyear = false;
}
if (leapyear % 400 == 0) {
leapyear = true;
}
if ((leapyear) && (month == 12 || month == 1 || month == 3 || month == 5
|| month == 7 || month == 8 || month == 10 )) {
leapdays = 31;
}
if ((leapyear) && (month == 4 || month == 6 || month == 9 || month == 11
)) {
leapdays = 30;
}
if ((leapyear) && (month == 2 )) {
leapdays = 29;
} else if ((leapyear == false) && (month == 2)) {
leapdays = 28;
}
printf ("Enter a date in the form day month year: %d %d %d \n", day,
month, year);
printf ("The date of the next day is: %d/%d/%d", ndays, month, year);
}
答案 0 :(得分:0)
在当天添加1后,请检查该值是否大于该月的天数。如果是这样,请将日期设置为1并将月份添加1。然后检查月份是否大于12,如果是,则将月份设置为1并将年份加1。
至于确定一个月中的天数,除了二月之外的所有月份都具有相同的天数,无论该年份是否为闰年。现在,您要检查这一年是否是其他月份的闰年。您可以退出该支票,只需查看月份编号。
if (month == 12 || month == 1 || month == 3 || month == 5
|| month == 7 || month == 8 || month == 10 ) {
leapdays = 31;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
leapdays = 30;
}
答案 1 :(得分:0)
做对了:)感谢所有的帮助
#include <stdio.h>
int day, month, year, ndays, leapdays;
bool leapyear;
int main () {
day = 31;
month = 12;
year = 2010;
ndays = day + 1;
leapyear = false;
printf ("Enter a date in the form day month year: %d %d %d \n", day, month, year);
if (year % 4 == 0) {
leapyear = true;
}
if (year % 100 == 0) {
leapyear = false;
}
if (year % 400 == 0) {
leapyear = true;
}
if ((leapyear) && (month == 12 || month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 )) {
leapdays = 31;
}
if ((leapyear) && (month == 4 || month == 6 || month == 9 || month == 11 )) {
leapdays = 30;
}
if ((leapyear) && (month == 2 )) {
leapdays = 29;
} else if (leapyear == false) {
leapdays = 28;
}
if ((leapdays == 31) && (day == 31)) {
ndays = 1;
month = month + 1;
}else if ((leapdays == 30) && (day == 30)) {
ndays = 1;
month = month + 1;
}else if ((leapdays == 29) && (day == 29)) {
ndays = 1;
month = month + 1;
}else if ((leapdays == 28) && (day == 28)) {
ndays = 1;
month = month + 1;
}else if ((month == 12) && (day == 31)) {
ndays = 1;
month = 1;
year = year + 1;
}
printf ("The date of the next day is: %d/%d/%d", ndays, month, year);
}
答案 2 :(得分:0)
考虑不同的流程。首先找到每月的日期,然后测试月末和年末。
int year, month, day;
// set year, month, day in some fashion
day++; // tomorrow
int days_per_month = 31;
if (month == 4 || month == 6 || month == 9 || month == 11) {
days_per_month = 30;
} else if (month == 2) {
days_per_month = 28;
if (year % 4 == 0) {
days_per_month = 29;
if (year % 100 == 0) {
days_per_month = 28;
if (year % 400 == 0) {
days_per_month = 29;
}
}
}
}
if (day > days_per_month) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
其他改进将使用辅助函数,枚举类型和各种定义。然而,这段代码似乎反映了OP的水平。
答案 3 :(得分:0)
更简短。仅在需要时检查leap年。
int main()
{
int Iyear, Imonth, Iday;
Iyear = 2016;
Imonth = 4;
Iday = 24;
printf ("Enter a date in the form day month year: %d %d %d \n", Iday, Imonth, Iyear);
NextDate(&Iyear, &Imonth, &Iday);
printf ("The date of the next day is: %d/%d/%d", Iday, Imonth, Iyear);
}
void NextDate(int *year, int *month, int *day)
{
int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
*day = *day +1;
if (( *month == 2 ) && (*day == 29))
{
// Leap year checking, if yes, Feb is 29 days.
if(*year % 400 == 0 || (*year % 100 != 0 && *year % 4 == 0))
{
daysInMonth[1] = 29;
}
}
if (*day > daysInMonth[*month -1])
{
*day = 1;
*month = *month +1;
if (*month > 12)
{
*month = 1;
*year = *year +1;
}
}
}