user-0 提问: C语言 万年历 一行输出两个月

回复:
user: user-1
content: 引用 5 楼 棉猴 的回复： #include "stdafx.h" #include <stdio.h>  int year(int y) { 	if ((y % 4 == 0) && (y % 100 != 0) || y % 400 == 0) 		return 366; 	else 		return 365; }   int main() { 	int y; 	int i, j, sum = 0, t = 0; 	int begin, week; 	int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };  	scanf_s("%d", &y); 	for (i = 1; i<y; i++) 		sum += year(i);  	week = (sum + 1) % 7;     //表示该年1月1日为星期几 	int next_begin, next_week; 	 	if (year(y) == 366) 		days[1] = 29;  	printf("\n%d年日历如下：\n\n", y); 	int week_num, next_week_num, max_week_num;//本月和下一个月包含的星期数 	char two_monthes_week[200] = { 0 }; 	for (i = 0; i<12; i = i + 2) 	{ 		printf("       %d月                       %d月                \n", i + 1, i+2); 		printf("  7  1  2  3  4  5  6      7  1  2  3  4  5  6  \n "); 		printf("=======================  =======================\n"); 		begin = 1; 		next_begin = 1; 		int temp_sum = sum; 		for (int j = 0; j < i; ++j) 		{ 			temp_sum = (temp_sum +days[j]); 		} 		week = (temp_sum + 1) % 7;//当前月的1号是星期几 		next_week = (days[i] + week) % 7;//下一个月的1号是星期几 		//获取两个月中包含的星期数的最大值 		//第一个月包含的星期数 		if (week + days[i] % 7 == 0) 		{ 			week_num = (week + days[i]) / 7; 		} 		else 		{ 			week_num = ((week + days[i]) / 7) + 1; 		} 		//第二个月包含的星期数 		if (next_week + days[i+1] % 7 == 0) 		{ 			next_week_num = (next_week + days[i+1]) / 7; 		} 		else 		{ 			next_week_num = ((next_week + days[i+1]) / 7) + 1; 		} 		//取两个月星期数的最大值 		if (week_num >= next_week_num) 		{ 			max_week_num = week_num; 		} 		else 		{ 			max_week_num = next_week_num; 		} 		//循环，每个循环为一个星期 		bool is_first_week = true; 		int length = 0; 		for (int j = 0; j < max_week_num; ++j)//遍历每个星期 		{  			while ((week % 7 != 0) || (week == 0))//一个星期是否结束 			{ 				if (is_first_week)//如果是第一个星期，，则需要输出空格 				{ 					for (int k = 0; k < week; ++k)//输出空格 					{ 						length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 					} 					is_first_week = false;//只需要第一个星期输出 				} 				if (begin <= days[i])//如果超出了这个月的日期，则输出空格 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%3d", begin); 				} 				else 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 				} 				week++; 				begin++; 			} 			//输出下一个月 			is_first_week = true; 			length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "    "); 			while ((next_week % 7 != 0) || (next_week == 0))//一个星期是否结束 			{ 				if (is_first_week)//如果是第一个星期，，则需要输出空格 				{ 					for (int k = 0; k < next_week; ++k) 					{ 						length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 					} 					is_first_week = false; 				} 				if (next_begin <= days[i + 1]) 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%3d", next_begin); 				} 				else 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 				} 				next_week++; 				next_begin++; 			} 			printf(two_monthes_week); 			printf("\n"); 			week = 0; 			next_week = 0; 			length = 0; 		} 	}      return 0; }   能发送一下代码嘛，不能复制

user: user-2
content: 写了两篇关于万年历的小文章，请多指教 http://blog.csdn.net/hou09tian/article/details/73928669 http://blog.csdn.net/hou09tian/article/details/73928712

user: user-2
content: #include "stdafx.h" #include <stdio.h>  int year(int y) { 	if ((y % 4 == 0) && (y % 100 != 0) || y % 400 == 0) 		return 366; 	else 		return 365; }   int main() { 	int y; 	int i, j, sum = 0, t = 0; 	int begin, week; 	int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };  	scanf_s("%d", &y); 	for (i = 1; i<y; i++) 		sum += year(i);  	week = (sum + 1) % 7;     //表示该年1月1日为星期几 	int next_begin, next_week; 	 	if (year(y) == 366) 		days[1] = 29;  	printf("\n%d年日历如下：\n\n", y); 	int week_num, next_week_num, max_week_num;//本月和下一个月包含的星期数 	char two_monthes_week[200] = { 0 }; 	for (i = 0; i<12; i = i + 2) 	{ 		printf("       %d月                       %d月                \n", i + 1, i+2); 		printf("  7  1  2  3  4  5  6      7  1  2  3  4  5  6  \n "); 		printf("=======================  =======================\n"); 		begin = 1; 		next_begin = 1; 		int temp_sum = sum; 		for (int j = 0; j < i; ++j) 		{ 			temp_sum = (temp_sum +days[j]); 		} 		week = (temp_sum + 1) % 7;//当前月的1号是星期几 		next_week = (days[i] + week) % 7;//下一个月的1号是星期几 		//获取两个月中包含的星期数的最大值 		//第一个月包含的星期数 		if (week + days[i] % 7 == 0) 		{ 			week_num = (week + days[i]) / 7; 		} 		else 		{ 			week_num = ((week + days[i]) / 7) + 1; 		} 		//第二个月包含的星期数 		if (next_week + days[i+1] % 7 == 0) 		{ 			next_week_num = (next_week + days[i+1]) / 7; 		} 		else 		{ 			next_week_num = ((next_week + days[i+1]) / 7) + 1; 		} 		//取两个月星期数的最大值 		if (week_num >= next_week_num) 		{ 			max_week_num = week_num; 		} 		else 		{ 			max_week_num = next_week_num; 		} 		//循环，每个循环为一个星期 		bool is_first_week = true; 		int length = 0; 		for (int j = 0; j < max_week_num; ++j)//遍历每个星期 		{  			while ((week % 7 != 0) || (week == 0))//一个星期是否结束 			{ 				if (is_first_week)//如果是第一个星期，，则需要输出空格 				{ 					for (int k = 0; k < week; ++k)//输出空格 					{ 						length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 					} 					is_first_week = false;//只需要第一个星期输出 				} 				if (begin <= days[i])//如果超出了这个月的日期，则输出空格 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%3d", begin); 				} 				else 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 				} 				week++; 				begin++; 			} 			//输出下一个月 			is_first_week = true; 			length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "    "); 			while ((next_week % 7 != 0) || (next_week == 0))//一个星期是否结束 			{ 				if (is_first_week)//如果是第一个星期，，则需要输出空格 				{ 					for (int k = 0; k < next_week; ++k) 					{ 						length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 					} 					is_first_week = false; 				} 				if (next_begin <= days[i + 1]) 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%3d", next_begin); 				} 				else 				{ 					length += sprintf_s(two_monthes_week + length, 200 - length, "%s", "   "); 				} 				next_week++; 				next_begin++; 			} 			printf(two_monthes_week); 			printf("\n"); 			week = 0; 			next_week = 0; 			length = 0; 		} 	}      return 0; }

user: user-2

user: user-3
content: 百度搜相关关键字。

user: user-0
content: 怎么做到一行输出两个月？

user: user-4
content: 引用 1 楼 qq_37560806 的回复:怎么做到一行输出两个月？  sprintf到一个字符数组 int len=sprintf(buffer,"。。。"); len=sprintf(buffer+len,"。。。");