阅读日期并打印出第二天日期的C程序
嗨我刚刚开始在大学学习C语言。现在我面临着一个我不知道如何解决的问题。到目前为止,我们只使用了 库,没有别的,scanf也是我们下周要学的 。现在我们只是使用printf来处理所有事情。 我已经学会如何说如果一年是闰年,但是,我的任务是下一个:需要创建一个程序来读取日期并打印出第二天的日期,输出如下:阅读日期并打印出第二天日期的C程序
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年下一个日期需要是1 3 2010年,因为它不是闰年。 使用的唯一库并没有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);
}
你加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;
}
明白了吧:)感谢所有帮助
#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);
}
您应该使用函数;如果你正在处理二月份,你应该只测试闰年;你应该使用'scanf()'或类似的函数来读取要处理的值 - 此刻,你必须重新编译程序来更改日期。十二月,一月,三月,五月,七月,八月,十月有多少天是非年?在if((leapyear)&&(month == 12 || month == 1 || month == 3 || month == 5 || month == 7 || month)中测试闰年似乎很奇怪== 8 || month == 10)){leapdays = 31; }'。同样的四月,六月,九月和十一月? –
OTOH,如果你下周了解'scanf()',也许你不知道'main()'以外的函数......当你学习编写函数时,编写可读代码会更容易。无论您是否使用函数,本月的日期问题都在那里。 –
考虑不同的流量。首先先找到每月的日子,然后再测试月底和年底。
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的水平。
如果你不能使用'scanf'你打算如何获得输入? –
你不输入,你只需创建变量并在其周围改变它 –
你不需要喊,但是你确实需要处理你的问题描述,因为它说'需要创建一个读取日期的程序'和你的printf请求输入。 –