user-0 提问: 怎么用二维数组计算这天是一年中的第几天呢

回复:
user: user-1
content: 引用 2 楼 qq_33131973 的回复:[quote=引用 1楼赵4老师 的回复:]COleDateTime::GetDayOfYear int GetDayOfYear( ) const;  Return Value  The day of the year represented by the value of this COleDateTime object.  Remarks  Call this member function to get the day of the year represented by this date/time value.   The COleDateTime class handles dates from 1 January 100 – 31 December 9999.看不懂…[/quote] #include <afxdisp.h> #include <stdio.h> int main() {     COleDateTime t=COleDateTime::GetCurrentTime();     CString s=t.Format(_T("%Y-%m-%d"));     _tprintf(_T("%s %d\n"),s,t.GetDayOfYear());     return 0; } //2015-12-02 336 //

user: user-1
content: COleDateTime::GetDayOfYear int GetDayOfYear( ) const;  Return Value  The day of the year represented by the value of this COleDateTime object.  Remarks  Call this member function to get the day of the year represented by this date/time value.   The COleDateTime class handles dates from 1 January 100 – 31 December 9999.

user: user-2
content: 引用 6 楼 qq_33131973 的回复:[quote=引用 3楼chehw_1 的回复:] #include<stdio.h>  int main(int argc, char ** argv) { 	int y, m, d; 	static int n[2][12] = { 		//	 1	2	3	4	5	  6		7	8	 9	  10,  11, 12	 			{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, // 平年 			{0, 31, 60, 91, 121, 152, 182, 213, 244, 275, 305, 335}  // 闰年 		}; 	int flag; 	 	while(1) 	{ 		printf("yyyy-mm-dd: ");	 		scanf("%d-%d-%d", &y, &m, &d); 		 		flag = (((y % 400) == 0) || ((y % 100) && ((y % 4) == 0)))?1:0; 		if(m > 12 || m <= 0) 		{ 			fprintf(stderr, "invalid month value.\n"); 			continue; 		}else if((d > (n[flag][m] - n[flag][m - 1])) || (d < 1)) 		{ 			fprintf(stderr, "invalid day value.\n"); 			continue; 		} 		 		 		break; 	} 	 	 	int n_day = n[flag][m - 1] + d; 	 	printf("n_day = %d\n", n_day); 	 	return 0; } 你这样更长了啊，别人说用二维数组代码会更短一点[/quote]  我额外增加了判断月和日的输入的数字是否有效性。  另外，使用二维数组，不是让代码更短，而是减少代码执行的步骤，用空间换取时间。（加法运算的次数减少了）

user: user-0
content: 引用 4楼user-3 的回复:flag的判定看着蛋疼怎么办？楼主你少了判定输入是否有效的环节。一维数组也好用啊，增加一个判断if(flag&&b>2) {....... c+=1;}不用啊，flag那行把输出的值直接加进第二个月了

user: user-0
content: 引用 3楼chehw_1 的回复: #include<stdio.h>  int main(int argc, char ** argv) { 	int y, m, d; 	static int n[2][12] = { 		//	 1	2	3	4	5	  6		7	8	 9	  10,  11, 12	 			{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, // 平年 			{0, 31, 60, 91, 121, 152, 182, 213, 244, 275, 305, 335}  // 闰年 		}; 	int flag; 	 	while(1) 	{ 		printf("yyyy-mm-dd: ");	 		scanf("%d-%d-%d", &y, &m, &d); 		 		flag = (((y % 400) == 0) || ((y % 100) && ((y % 4) == 0)))?1:0; 		if(m > 12 || m <= 0) 		{ 			fprintf(stderr, "invalid month value.\n"); 			continue; 		}else if((d > (n[flag][m] - n[flag][m - 1])) || (d < 1)) 		{ 			fprintf(stderr, "invalid day value.\n"); 			continue; 		} 		 		 		break; 	} 	 	 	int n_day = n[flag][m - 1] + d; 	 	printf("n_day = %d\n", n_day); 	 	return 0; } 你这样更长了啊，别人说用二维数组代码会更短一点

user: user-3
content: flag的判定看着蛋疼怎么办？楼主你少了判定输入是否有效的环节。一维数组也好用啊，增加一个判断if(flag&&b>2) {....... c+=1;}

user: user-2
content: #include<stdio.h>  int main(int argc, char ** argv) { 	int y, m, d; 	static int n[2][12] = { 		//	 1	2	3	4	5	  6		7	8	 9	  10,  11, 12	 			{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, // 平年 			{0, 31, 60, 91, 121, 152, 182, 213, 244, 275, 305, 335}  // 闰年 		}; 	int flag; 	 	while(1) 	{ 		printf("yyyy-mm-dd: ");	 		scanf("%d-%d-%d", &y, &m, &d); 		 		flag = (((y % 400) == 0) || ((y % 100) && ((y % 4) == 0)))?1:0; 		if(m > 12 || m <= 0) 		{ 			fprintf(stderr, "invalid month value.\n"); 			continue; 		}else if((d > (n[flag][m] - n[flag][m - 1])) || (d < 1)) 		{ 			fprintf(stderr, "invalid day value.\n"); 			continue; 		} 		 		 		break; 	} 	 	 	int n_day = n[flag][m - 1] + d; 	 	printf("n_day = %d\n", n_day); 	 	return 0; }

user: user-0
content: 引用 1楼赵4老师 的回复:COleDateTime::GetDayOfYear int GetDayOfYear( ) const;  Return Value  The day of the year represented by the value of this COleDateTime object.  Remarks  Call this member function to get the day of the year represented by this date/time value.   The COleDateTime class handles dates from 1 January 100 – 31 December 9999.看不懂…