user-0 提问: 大学c的题目，要求输入任意日期，输出相应的星期，各位帮我看看哪有问题

回复:
user: user-1
content: 对！好的程序对一些函数或语句都有必要的说明或注释，这叫完整的文档O(∩_∩)O

user: user-2
content: 不加注释看参数  楼主你回头看能一下子明白吗

user: user-3
content: // 某年是否是闰年 bool isLeapYear(int y) { 	return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0); }  // 某年某月有多少天 int daysOfMonth(int y, int m) { 	static int dm[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  	return dm[m] + ((m == 2 && (isLeapYear(y)) ? 1 : 0)); }  // 某日期在该年的哪一天（即在该年的累计天数） int daysInYear(int y, int m, int d) { 	while (--m > 0) 		d += daysOfMonth(y, m);  	return d; }  // 某日期是星期几 int dayOfWeek(int y, int m, int d) { 	return (y - 1 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + daysInYear(y, m, d)) % 7; }

user: user-4
content: http://www.cnblogs.com/xingluzhe/archive/2009/08/17/1548405.html

user: user-5
content: 失误：case 3: 	b += 28 + leap(n); // 这里 n 应该 + 1

user: user-1
content: 楼上的思路不错，确实简单很多O(∩_∩)O

user: user-1
content: “输入格式，如：2017-10-12。你那样 %d%d%d 是不对的，你让如何区分三个数字？”使用空格来区分年月日的，使用“-”符号，在输入时必须使用“-”来分隔年月日，并且在输入时“-”符号是不可见的。别人如何知道“-”，并且正确的输入O(∩_∩)O。

user: user-5
content: 写得太啰嗦了，我简化一下：#include <stdio.h>  static const char *weekdays[] = { 	"Sunday", 	"Monday", 	"Tuesday", 	"Wednesday", 	"Thursday", 	"Friday", 	"Saturday" };  static const int DAYS_IN_WEEK = sizeof(weekdays) / sizeof(*weekdays);  static int leap(int y) { 	return ((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0); }  int main(void) { 	int y = 0; 	int m = 0; 	int d = 0; 	int n = 0; 	int b = 0; 	scanf("%d-%d-%d", &y, &m, &d); 	while (++n < y) { 		b += 365 + leap(n); 	} 	switch (m) { 	case 12: 		b += 30; 	case 11: 		b += 31; 	case 10: 		b += 30; 	case 9: 		b += 31; 	case 8: 		b += 31; 	case 7: 		b += 30; 	case 6: 		b += 31; 	case 5: 		b += 30; 	case 4: 		b += 31; 	case 3: 		b += 28 + leap(n); 	case 2: 		b += 31; 	case 1: 		b += d; 	default: 		break; 	} 	if (b >= DAYS_IN_WEEK) { 		b %= DAYS_IN_WEEK; 	} 	printf("%d/%d/%d: %s\n", y, m, d, weekdays[b]); 	return 0; }输入格式，如：2017-10-12。你那样 %d%d%d 是不对的，你让如何区分三个数字？

user: user-1
content: 消除一些错误的最终源码 #define _CRT_SECURE_NO_WARNINGS #include<string.h> #include<stdio.h> #include<memory.h>    struct SDate//Date时间数组结构体 { int nYear; int nMonth; int nDay; }dt = { 2008,10,7 };  int iYear(SDate *p)//输入年份转化为对基准的超前天数，注：基准年份为公元1年1月1日为星期一 { 	int y=p->nYear; 	unsigned int nDay; 	y = y - 1; 	nDay = y * 365; 	while (y> 0) 	{ 		if ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) 			++nDay; 		else 			nDay = nDay; 		--y; 	} 	return nDay;  	 }  int iMonth(SDate *p)//一年中第几天计算函数 { 	 	int ms[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; 	int i = 0; 	unsigned int nDay = p->nDay; 	while (i < p->nMonth - 1) 	{ 		nDay += ms[i]; 		++i; 	} 	if ((p->nYear % 4 == 0) && (p->nYear % 100 != 0) || (p->nYear % 400 == 0)) 		if (p->nMonth > 2) 			++nDay; 	return nDay;  }  void iWeek(int d)//一年中的天数转化成星期几函数 { 	unsigned int day=d; 	 	day = (day-1) % 7; 	switch (day) 	{ 	case 0:printf("Monday(星期一)"); break; 	case 1:printf("Tuesday(星期二)"); break; 	case 2:printf("Wednesday(星期三)"); break; 	case 3:printf("Thursday(星期四)"); break; 	case 4:printf("Friday(星期五)"); break; 	case 5:printf("Saturday(星期六)"); break; 	case 6:printf("Sunday(星期日)"); break; 	} }   void InputDate(SDate *p) { 	printf("请输入需要计算的年月日"); 	scanf("%d%d%d", &p->nYear, &p->nMonth, &p->nDay);  }  int main()//计算给定年月日后在年的第几天 { 	 	SDate *p = &dt; 	unsigned int Calculateday = 0; 	InputDate(&dt);  	Calculateday = iYear(&dt) + iMonth(&dt);     printf("%d年%d月%d日是星期：",p->nYear,p->nMonth,p->nDay); 	iWeek(Calculateday);  	return 0; }

user: user-6
content: 355 what is it?

user: user-1
content: #define _CRT_SECURE_NO_WARNINGS #include <stdio.h>  int Calculateday(int y,int m,int d) { 	unsigned int  year,  b = 0, month, c, day; 	day = d;  	month = m;  	year = y; 	c = y; 	year = year - 1; 	while (y> 0) 	{ 		if ((y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0)) 			++b; 		else 			b = b; 		--y; 	} 	day = year * 365 + b+day;  	if (month == 1 && month == 2) 	{ 		if ((c % 4 == 0) && (c % 100 != 0) || (c % 400 == 0)) 			day = day + 29; 		else 			day = day + 28; 	} 	month = month - 2;  	while (month > 0) 	{ 		if (month % 2 == 0) 			day = day + 31; 		else 			day = day + 30; 		month--; 	}  	 	return day;  }  void iWeek(int d) { 	unsigned int day; 	day = d; 	day = day % 7; 	switch (day) 	{ 	case 0:printf("Monday"); break; 	case 1:printf("Tuesday"); break; 	case 2:printf("Wednesday"); break; 	case 3:printf("Thursday"); break; 	case 4:printf("Friday"); break; 	case 5:printf("Saturday"); break; 	case 6:printf("Sunday"); break; 	} }  int main() { unsigned int  year,month,day; printf("请输入需要计算的年月日："); scanf("%d%d%d",&year,&month,&day);//任意输入一个年月日,把公元元年元旦(公元1年1月1日)规定为星期一 day = Calculateday(year,month,day);//统计对应的日期数 iWeek(day);                        //计算具体为星期几  return 0; } 这是优化后的代码O(∩_∩)O

user: user-1
content: 还有错，改正如下： #define _CRT_SECURE_NO_WARNINGS #include <stdio.h>     int main() { unsigned int  year,b = 0,c,month,d,day; scanf("%d%d%d",&year,&month,&day);//任意输入一个年月日 c = year; year = year - 1; while (year > 0) { 	if ((c % 4 == 0) && (c % 100 != 0) || (c % 400 == 0)) 		b = b + 1; 	else 	b = b; 	year--; } b = b * 366; c = (c - b) * 355; if ((c % 4 == 0) && (c % 100 != 0) || (c % 400 == 0)) switch (month) { case 1:d = 0; break; case 2:d = 31; break; case 3:d = 29 + 31; break; case 4:d = 31 + 29 + 31; break; case 5:d = 30 + 31 + 29 + 31; break; case 6:d = 31 + 30 + 31 + 29 + 31; break; case 7:d = 30 + 31 + 30 + 31 + 29 + 31; break; case 8:d = 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 9:d = 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 10:d = 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 11:d = 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 12:d = 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; } else switch (month) { case 1:d = 0; break; case 2:d = 31; break; case 3:d = 28 + 31; break; case 4:d = 31 + 28 + 31; break; case 5:d = 30 + 31 + 28 + 31; break; case 6:d = 31 + 30 + 31 + 28 + 31; break; case 7:d = 30 + 31 + 30 + 31 + 28 + 31; break; case 8:d = 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 9:d = 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 10:d = 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 11:d = 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 12:d = 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; } day = day + d + c + b; day = day % 7; switch (day) { case 0:printf("Monday"); break; case 1:printf("Tuesday"); break; case 2:printf("Wednesday"); break; case 3:printf("Thursday"); break; case 4:printf("Friday"); break; case 5:printf("Saturday"); break; case 6:printf("Sunday");break; } return 0;

user: user-1
content: 有几个地方错了改正如下： #include <stdio.h>     int main() { unsigned int  year,b = 0,c,month,d,day; scanf("%d%d%d",&year,&month,&day);//任意输入一个年月日 c = year; year = year - 1; while (year > 0) { 	if ((c % 4 == 0) && (c % 100 != 0) || (c % 400 == 0)) 		b = b + 1; 	else 	b = b; 	year--; } b = b * 366; c = (c - b) * 355; if ((c % 4 == 0) && (c % 100 != 0) || (c % 400 == 0)) switch (month) { case 1:d = 0; break; case 2:d = 31; break; case 3:d = 29 + 31; break; case 4:d = 31 + 29 + 31; break; case 5:d = 30 + 31 + 29 + 31; break; case 6:d = 31 + 30 + 31 + 29 + 31; break; case 7:d = 30 + 31 + 30 + 31 + 29 + 31; break; case 8:d = 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 9:d = 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 'a':d = 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 'b':d = 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; case 'c':d = 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 29 + 31; break; } else switch (month) { case 1:d = 0; break; case 2:d = 31; break; case 3:d = 28 + 31; break; case 4:d = 31 + 28 + 31; break; case 5:d = 30 + 31 + 28 + 31; break; case 6:d = 31 + 30 + 31 + 28 + 31; break; case 7:d = 30 + 31 + 30 + 31 + 28 + 31; break; case 8:d = 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 9:d = 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 'a':d = 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 'b':d = 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; case 'c':d = 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31; break; } day = day + d + c + b; day = day % 7; switch (day) { 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; case 0:printf("Sunday");	break; } return 0; }

user: user-7
content: case为什么要加单引号，试一试不加。我好像没加过。

user: user-8
content: 这样的程序看起来真的不太好，不知道你参加ACM吗？你这样写编程的效率太低了。你这样写就算没有BUG，写完的时候别人第二题都快AC了，建议找一下规律递归一下就可以了。不建议养成这样编程习惯。这样写，正确率，编程效率都不高。定义宏，递归处理比较好

user: user-9
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-10
content: 这个是算法问题吧。只要弄懂日期与星期几的转换，应该一下子出来。

user: user-11
content: d是整型，所以case 后面的选项都应该用整型的1， 2， 3，4...