C 语言的日期计算器程序
我正在尝试用 C 程序编写一个日期计算器,范围从 1/1/1902 到 12/31/2299,我遵循 http://en.wikipedia.org/wiki/Calculated_the_day_of_the_week 带有世纪表、月份表和日期表,但是当我尝试打印出来时,这就是我的 而不是
Enter Year, Month and Day as YYYY,M,DD
1982 4 24
Saturday1982 ,6, 24, is 6
说 1982 4 24 is a Saturday
我写的程序有什么问题?开关盒的位置?
#include <stdio.h>
int main (void)
{
// insert code here...
int day,month,year;
printf("Enter Year, Month and Day as YYYY,M,DD\n");
scanf("%4d%d%2d", &year, &month, &day);
if (year >= 1901 && year <= 2299 &&
month >= 1 && month <= 12 &&
day >= 0 && day <= 31)
{
int century = year/100;
/* making a century table for calculation*/
switch(century)
{
case 19:
century=0;
break;
case 20:
century=6;
break;
case 21:
century=4;
break;
case 22:
century=2;
break;
}
int last2_of_year= year % 100;
/* Last 2 digits of the year entered*/
int last2_div_4 = last2_of_year/4;
switch (month)
{
case 1:
month=0;
break;
case 2:
month=3;
break;
case 3:
month=3;
break;
case 4:
month=6;
break;
case 5:
month=1;
break;
case 6:
month=4;
break;
case 7:
month=6;
break;
case 8:
month=2;
break;
case 9:
month=5;
break;
case 10:
month=0;
break;
case 11:
month=3;
break;
case 12:
month=5;
break;
}
int total_num = (century+ last2_of_year +day +month +last2_div_4)%7;
switch (total_num)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
}
printf("%d ,%d, %d, is a %d", year,month,day,total_num);
}
else
{
printf("invalid\n");
}
return 0;
}
I'm trying to write a date calculator in C program with the range from 1/1/1902 to 12/31/2299, I've follow the algorithm from http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week with the century table,month table and day table, but when I tried to print out, this is what I have
Enter Year, Month and Day as YYYY,M,DD
1982 4 24
Saturday1982 ,6, 24, is 6
Instead of saying 1982 4 24 is a Saturday
What is wrong in the program I wrote? The placement of the switch cases?
#include <stdio.h>
int main (void)
{
// insert code here...
int day,month,year;
printf("Enter Year, Month and Day as YYYY,M,DD\n");
scanf("%4d%d%2d", &year, &month, &day);
if (year >= 1901 && year <= 2299 &&
month >= 1 && month <= 12 &&
day >= 0 && day <= 31)
{
int century = year/100;
/* making a century table for calculation*/
switch(century)
{
case 19:
century=0;
break;
case 20:
century=6;
break;
case 21:
century=4;
break;
case 22:
century=2;
break;
}
int last2_of_year= year % 100;
/* Last 2 digits of the year entered*/
int last2_div_4 = last2_of_year/4;
switch (month)
{
case 1:
month=0;
break;
case 2:
month=3;
break;
case 3:
month=3;
break;
case 4:
month=6;
break;
case 5:
month=1;
break;
case 6:
month=4;
break;
case 7:
month=6;
break;
case 8:
month=2;
break;
case 9:
month=5;
break;
case 10:
month=0;
break;
case 11:
month=3;
break;
case 12:
month=5;
break;
}
int total_num = (century+ last2_of_year +day +month +last2_div_4)%7;
switch (total_num)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
}
printf("%d ,%d, %d, is a %d", year,month,day,total_num);
}
else
{
printf("invalid\n");
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说:
这将打印
其中
L
、M
、N
和P
是数字的地方。您需要在日期名称
开关
之前使用printf()
,并且需要删除最后的%d
以及来自printf
的total_num
。然后printf
将打印出来,并且 name-of-days
switch
中的printf
将在同一行打印出当天的名称,为您提供编辑地址注释:
查看您的程序中的输出语句。
遇到的第一个输出语句是日期名称开关中的
printf
调用,用于打印日期名称。因此,当您的程序在给定您提到的输入的情况下运行时,将打印出的第一件事是然后在日期名称切换之后,下一个
printf
是因为
year
是1982
,月
为4
,日
为24
,total_num
为6
,printf
将与之前的
Saturday
输出在同一行输出,这意味着整个输出为You say:
That's going to print
where
L
,M
,N
, andP
are numbers.You need that
printf()
before the name-of-daysswitch
and need to remove the final%d
and thetotal_num
from theprintf
. Then thatprintf
will printand the
printf
s in the name-of-daysswitch
will print out the name of the day on the same line, giving youEdited to address comment:
Look at your program for output statements.
The first output statement encountered are the
printf
calls in the name-of-days switch that prints out the day names. So when your program runs given the input you mention, the first thing that will be printed out isThen after the name-of-days switch the next
printf
isSince
year
is1982
,month
is4
,day
is24
, andtotal_num
is6
, thatprintf
will outputon the same line as the previous
Saturday
output, which means the entire output is看起来 @QuantumMechanic 找到了问题的根本原因,但我想建议一些更改:
我非常对使用单个变量来表示两个不同的事物持怀疑态度。这里,世纪代表了用户输入的人类可读的世纪和该世纪一周第一天的偏移量。两个变量可以提供更清晰的代码,并允许您在以后需要时重新使用世纪信息。
其次,使用
case
语句来存储月份的偏移量感觉有点……过头了:这可以通过数组查找来处理:
-1
只是为了允许使用人类友好的索引(Jan == 1
)引用表格。如果您觉得更简单,请随意删除它并使用leap_month[month-1]
或类似的方法。Looks like @QuantumMechanic found the root cause of the problem, but I'd like to suggest a few changes:
I'm very leery of using a single variable to represent two different things. Here,
century
represents both the user-input human-readable century and an offset to the first day of the week for the century. Two variables would give clearer code, and allow you to re-use thecentury
information later, should the need arise.Second, using a
case
statement to store offsets for the months feels a little ... overdone:This could instead be handled with an array lookup:
The
-1
is just to allow referring to the table with human-friendly indexes (Jan == 1
). Feel free to remove it and useleap_month[month-1]
or similar if you find that easier.