user-0 提问: 求教一道C语言题目 急！

回复:
user: user-0
content: 谢谢各位高手的鼎力支持 我还需要继续努力消化 谢谢~

user: user-0
content: 大师 还有一事我在想 如果下面5个周六 要排到2015了 我要阻止2015年中6月份的出现 这个代码怎么加

user: user-0
content: void main() {     int year,day,fsn;     cout<<"Enter the year number:";     cin>>year;     while(year<1970)     {         cout<<"please enter ther year number(>1970):";         cin>>year;     }     cout<<"Enter the day number (1 - 365):";     cin>>day;     while(day>366||day<1)     {         cout<<"please enter the day number (1 - 365):";         cin>>day;     }     cout<<"Enter desired number of future Saturdays:";     cin>>fsn;     while(fsn>12||fsn<1)     {         cout<<"please enter desired number of future Saturdays(1-12):";         cin>>fsn;     }       time a;     a.year=year;     a.monthday=day;     a.month=1;     a.weekday=0;     a=newtime(a);     cout<<"Input date:"<<a.year<<"年"<<a.month<<"月"         <<a.monthday<<"日"<<" "<<"星期"<<a.weekday<<endl;       for (int i=1;i<=monthtotalday(year,fsn);++i)     {         time b;         b.year=year;         b.month=fsn;         b.monthday=i;         b.weekday=0;         b=newtime(b);         if (6==b.weekday)         {             cout<<"Next Saturday:"<<b.year<<"年"<<b.month<<"月"                 <<b.monthday<<"日"<<" "<<"星期"<<b.weekday<<endl;         }     }     system("pause"); } 大师 这段我没怎么想明白啊 求解惑

user: user-1
content: 讲的好透彻了，谢谢分享

user: user-2
content: 145行改为dayNum += (3 - weekDay);

user: user-2
content: #include <stdio.h> #include "test.h"  const char daysOfEachMonth[12] = { 	31,		/* 1 */ 	28,		/* 2, not leap year */ 	31,		/* 3 */ 	30,		/* 4 */ 	31,		/* 5 */ 	30,		/* 6 */ 	31,		/* 7 */ 	31,		/* 8 */ 	30,		/* 9 */ 	31,		/* 10 */ 	30,		/* 11 */ 	31		/* 12 */ };  const char *monthName[] = { 	"January",		/* 1 */ 	"February",		/* 2 */ 	"March",		/* 3 */ 	"April",		/* 4 */ 	"May",			/* 5 */ 	"June",			/* 6 */ 	"July",			/* 7 */ 	"August",		/* 8 */ 	"September",	/* 9 */ 	"October",		/* 10 */ 	"November",		/* 11 */ 	"December"		/* 12 */ };  const char *weekName[] = { 	"Thursday",		/* 4, 1.1.1970 */ 	"Friday",		/* 5 */ 	"Saturday",		/* 6 */ 	"Sunday",		/* 7 */ 	"Monday",		/* 1 */ 	"Tuesday",		/* 2 */ 	"Wednesday",	/* 3 */ };   int isYearLeap(unsigned int year) { 	if(year%400 == 0 || (year%100 != 0 && year%4 == 0)) 		return 1; 	else 		return 0; }  unsigned int totalDays(unsigned int year, unsigned int month) { 	unsigned int i, days;   	days = isYearLeap(year);  	if(month == 1) 		return 31; 	else{ 		for(i = 0; i < month; i++)  			days += daysOfEachMonth[i];  	} 								 	return days; 	 }  unsigned int weekday(unsigned int year, unsigned int dayNum) { 	unsigned int i, days = 0;  	unsigned int remainder; 	 	for(i = 1970; i < year; i++) 		days += (isYearLeap(i) ? 366 : 365);  	days += dayNum; 	remainder = days % 7; 								 	return remainder; 	 }  unsigned int days2month(unsigned int yearNum, unsigned int dayNum) { 	unsigned int month;  	if(0 < dayNum && dayNum <= totalDays(yearNum, 1)) 		month = 1; 	else if(totalDays(yearNum, 1) < dayNum && dayNum <= totalDays(yearNum, 2)) 		month = 2; 	else if(totalDays(yearNum, 2) < dayNum && dayNum <= totalDays(yearNum, 3)) 		month = 3; 	else if(totalDays(yearNum, 3) < dayNum && dayNum <= totalDays(yearNum, 4)) 		month = 4; 	else if(totalDays(yearNum, 4) < dayNum && dayNum <= totalDays(yearNum, 5)) 		month = 5; 	else if(totalDays(yearNum, 5) < dayNum && dayNum <= totalDays(yearNum, 6)) 		month = 6; 	else if(totalDays(yearNum, 6) < dayNum && dayNum <= totalDays(yearNum, 7)) 		month = 7; 	else if(totalDays(yearNum, 7) < dayNum && dayNum <= totalDays(yearNum, 8)) 		month = 8; 	else if(totalDays(yearNum, 8) < dayNum && dayNum <= totalDays(yearNum, 9)) 		month = 9; 	else if(totalDays(yearNum, 9) < dayNum && dayNum <= totalDays(yearNum, 10)) 		month = 10; 	else if(totalDays(yearNum, 10) < dayNum && dayNum <= totalDays(yearNum, 11)) 		month = 11; 	else if(totalDays(yearNum, 11) < dayNum && dayNum <= totalDays(yearNum, 12)) 		month = 12; 	else{ 		printf("Enter the ERROR day number!!!\r\n"); 		return 0; 	}  	return month; }   void days2date(unsigned int yearNum, unsigned int dayNum) { 	unsigned int month; 	unsigned int day; 	unsigned int weekDay;  	month = days2month(yearNum, dayNum); 	if(month == 0) 		return;  	day = dayNum - totalDays(yearNum, month-1); 	weekDay = weekday(yearNum, dayNum);  	printf("Input date: %s %s %d %d\r\n", weekName[weekDay-1], monthName[month-1], day, yearNum); }  void getDesiredFutureDay(unsigned int yearNum, unsigned int dayNum,  int desiredNum) { 	unsigned int weekDay; 	unsigned int month, day; 	int i;  	weekDay = weekday(yearNum, dayNum);  	if(weekDay <= 3) 		dayNum += (weekDay - 3); 	else 		dayNum += (10 - weekDay);  	for(i = 0; i < desiredNum; i++){ 		if(dayNum > (365+isYearLeap(yearNum))){ 			yearNum++; 			dayNum -= (365+isYearLeap(yearNum)); 		} 		month = days2month(yearNum, dayNum); 		if(month == 0) 			return; 		day = dayNum - totalDays(yearNum, month-1); 		printf("Next Saturday: %s %d %d\r\n", monthName[month-1], day, yearNum); 		dayNum += 7; 	}  }   int main(int argc, char** argv) { 	int yearNum; 	int dayNum; 	int desiredNum;  	printf("Enter the year number: "); 	scanf("%d", &yearNum);  	printf("Enter the day number (1-365): "); 	scanf("%d", &dayNum);  	printf("Enter desired number of future Saturdays: "); 	scanf("%d", &desiredNum);      days2date(yearNum, dayNum); 	getDesiredFutureDay(yearNum, dayNum, desiredNum);      return 0; }写了一个，C的

user: user-3
content: 上面代码 把第211行 换到 193-194之间，必须要先转换时间，然后再进行判断！

user: user-3
content: 看看这是不是你要的结果  #include <iostream> #include <string> using namespace std;  struct time{ 	int year; 	int month; 	int monthday; 	int weekday; };  //leapyear 计算 year是否为闰年 是返回true 否返回false bool leapyear(int year) { 	if((year%4==0&&year%100!=0)||(year%400==0)) return true; 	else return false; }  //返回某年份中某月份天数 int monthtotalday(int year,int month) { 	switch(month) 	{ 	case 2: 		if (true==leapyear(year))return 29; 		else return 28; 		break; 	case 1:;case 3:;case 5:;case 7:;case 8:;case 10:;case 12: 		return 31; 		break; 	case 4:;case 6:;case 9:;case 11: 		return 30; 		break; 	default: 		return 0; 		break; 	} }  //以年月日为基础计算星期 int weekday(time t) { 	int a; 	int totalday=0;//从格林威治开始时间计算到输入年份总天数 	for(int i=1970;i<t.year;++i)//以格林威治标准年份计算1970.1.1为星期四 	{ 		if (leapyear(i)==true)//闰年加366 平年加365 		{ 			totalday+=366; 		}else 		{ 			totalday+=365; 		} 	} 	for (int i=1;i<=t.month;++i)//加上每个月的天数 	{ 		totalday+=monthtotalday(t.year,i-1); 	} 	totalday+=t.monthday-1;//加上剩余天数（这里需要-1） 	totalday+=4;//加上格林威治的初始星期 	a=totalday%7;//总天数+初始星期数再摩除7得星期数。周日为0 	return a; }  //将时间格式重新转换 time newtime(time oldtime) { 	while(oldtime.monthday>monthtotalday(oldtime.year,oldtime.month))//天数大约当月最大天数进入循环 	{ 		oldtime.monthday-=monthtotalday(oldtime.year,oldtime.month);//天数大于当月最大天数进位 		oldtime.month++; 		if (oldtime.month>12)//月份数大于12则进位 		{ 			oldtime.month-=12; 			oldtime.year++; 		} 	} 	oldtime.weekday=weekday(oldtime);//计算星期 	return oldtime; }  string putenglishmonth(int month)//输出英文月份 { 	string englishmonth; 	switch(month) 	{ 	case 1:englishmonth="January"; 		break; 	case 2:englishmonth="February"; 		break; 	case 3:englishmonth="March"; 		break; 	case 4:englishmonth="April"; 		break; 	case 5:englishmonth="May"; 		break; 	case 6:englishmonth="June"; 		break; 	case 7:englishmonth="July"; 		break; 	case 8:englishmonth="August"; 		break; 	case 9:englishmonth="September"; 		break; 	case 10:englishmonth="October"; 		break; 	case 11:englishmonth="November"; 		break; 	case 12:englishmonth="December"; 		break; 	} 	return englishmonth; }  string putenglishweekday(int weekday)//输出英文星期 { 	string englishweekday; 	switch(weekday) 	{ 	case 0:englishweekday="Sunday"; 		break; 	case 1:englishweekday="Monday"; 		break; 	case 2:englishweekday="Tuesday"; 		break; 	case 3:englishweekday="Wednesday"; 		break; 	case 4:englishweekday="Thursday"; 		break; 	case 5:englishweekday="Friday"; 		break; 	case 6:englishweekday="Saturday"; 		break; 	} 	return englishweekday; }  void main() { 	int recordyear;//记录输入时的年份 	int year,day,fsn; //year 输入年份（>1970） //day 输入一年中的天数（1-365） //输入要输出的未来星期六数(>0) 	cout<<"Enter the year number:"; 	cin>>year; 	recordyear=year; 	while(year<1970) 	{ 		cout<<"please enter ther year number(>1970):"; 		cin>>year; 	} 	cout<<"Enter the day number (1 - 365):"; 	cin>>day; 	while(day>366||day<1) 	{ 		cout<<"please enter the day number (1 - 365):"; 		cin>>day; 	} 	cout<<"Enter desired number of future Saturdays:"; 	cin>>fsn; 	while(fsn<1) 	{ 		cout<<"please enter desired number of future Saturdays(>0):"; 		cin>>fsn; 	}  	time a;//定义一个时间 	a.year=year;//初始化年份 	a.monthday=day;//初始化天数 	a.month=1;//这个一定要初始化为1 如果输入2014 56  初始化时间表示为2014年1月56日 	a.weekday=0;//随意初始化星期，也可不初始化，因为星期是根据年月日计算出来的 	a=newtime(a);//将时间重新计算  	cout<<"Input date:"<<putenglishweekday(a.weekday)<<" "<<putenglishmonth(a.month)<<" " 		<<a.monthday<<" "<<a.year<<endl;  	int b=0; 	while(0==b)//找到输入日期的下一个周六 	{ 		a.monthday++; 		a=newtime(a); 		if (6==a.weekday) 		{ 			b++; 		} 	} 	for (int i=0;i<fsn;i++)//输出接下来fsn个周六的日期 	{ 		if (0!=i)//第一次不加一周输出，以后都加一周输出 		{ 			a.monthday+=7; 		} 		if (a.year>recordyear)//跨年则判断是否继续输出，输入1继续输出 输入0结束输出 		{ 			int pass; 			cout<<recordyear<<"has gone by,whether to show "<<a.year<<" <yes put 1 or no put 0>"; 			cin>>pass; 			while(pass!=0&&pass!=1) 			{ 				cout<<recordyear<<"has gone by,whether to show "<<a.year<<" <yes put 1 or no put 0>"; 				cin>>pass; 			} 			if (0==pass) 			{ 				system("pause"); 				return; 			} 			recordyear=a.year; 		} 		a=newtime(a);//时间转换 并输出 		cout<<"Next Saturday:"<<putenglishweekday(a.weekday)<<" "<<putenglishmonth(a.month)<<" " 			<<a.monthday<<" "<<a.year<<endl; 	} 	system("pause"); }

user: user-0
content: #include <stdio.h>  int week_calc[2][12] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5 ,                          0, 3, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6};  char str_month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};  char str_week[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};  int ifLeapYear(int year) {   return (           year % 400 == 0 ||            (year % 4 == 0 && year % 100 != 0)           )           ? 1 : 0; }  int getDate(int year, int num_day, int *month, int *date) {   int days_month[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   int i = 0;   for (; i < 12; ++i)   {     int days = days_month[i] + (1 == i ? ifLeapYear(year) : 0);     if (num_day <= days)     {       *month = i;       *date = num_day;       return 1;     }     num_day -= days;   }   *month = -1; *date = -1;   return 0; }  int getWeek(int year, int month, int date) {   return (year-1 + (year-1)/4 - (year-1)/100 + (year-1)/400 + week_calc[ifLeapYear(year)][month] + date) % 7; }  int getMaxDays(int year) {   return 365 + ifLeapYear(year); }  main() {   int num_year, num_day;   printf("Enter the year number: ");   scanf("%d", &num_year);   printf("\n");   int year = num_year;   int max_days = getMaxDays(year);    printf("Enter the year number (1 - %d): ", max_days);   scanf("%d", &num_day);   printf("\n");    int month = -1, date = -1;   getDate(year, num_day, &month, &date);   int num_week = getWeek(year, month, date);      int prev_day = num_day - (num_week - 0), next_day = num_day + (6 - num_week);   if (prev_day == num_day)     prev_day = num_day - 7;   else if (next_day == num_day)     next_day = num_day + 7;    int prev_year = year, next_year = year;   if (prev_day <= 0)   {     --prev_year;     prev_day = getMaxDays(prev_day) - prev_day;   }   if (next_day > max_days)   {     ++ next_year;     next_day = next_day - max_days;   }    int prev_month = -1, prev_date = -1, next_month = -1, next_date = -1;   getDate(prev_year, prev_day, &prev_month, &prev_date);    getDate(next_year, next_day, &next_month, &next_date);    printf("Input date: %s %s %d, %d\n", str_week[num_week], str_month[month], date, year);   printf("Previous Sunday: %s %d, %d\n", str_month[prev_month], prev_date, prev_year);   printf("Next Saturday: %s %d, %d\n", str_month[next_month], next_date, next_year);    return 0; } 这个应该是计算的代码 但是需要实现Enter desired number of future Saturdays: 5 这个样子的 这段代码没有 麻烦帮我看下吧

user: user-4
content: 引用 2 楼 jacksto 的回复:首先先谢谢您的分析  顺便大师 求代码。。。 最好有注释 谢谢！ 还有如果输入不出5行数据 这个怎么处理啊 你已经有代码了？发出来看一下。 主要输不知道这个在考什么，有现成的函数可以用。但如果是在考计算日期，那用现成的函数好像不行。

user: user-0
content: 首先先谢谢您的分析  顺便大师 求代码。。。 最好有注释 谢谢！ 还有如果输入不出5行数据 这个怎么处理啊

user: user-4
content: Enter the year number: 2014 输入年份 Enter the day number (1 - 365): 56 输入一年当中的已经过去的天数（从结果中猜出来的） Enter desired number of future Saturdays: 5 希望列出未来是是星期6的日期的个数 Input date: Tuesday February 25, 2014 输入：星期2,2月25,2014 Next Saturday: March 1, 2014 Next Saturday: March 8, 2014 Next Saturday: March 15, 2014 Next Saturday: March 22, 2014 Next Saturday: March 29, 2014  程序需要做的工作。 根据日期，计算出已经过去的天数（函数1） 根据提供的日期是星期几，计算出后面每个星期六的天数（函数2） 根据天数，计算出日期（函数1的反函数）  OK了。分析的应当比较明白了，还要代码？

user: user-3
content: 误会你最后一问的意思了，我以为是某一月份中所有周六的天数 更改如下，把下方注释掉的换成上面代码即可  	int b=0; 	while(0==b) 	{ 		a.monthday++; 		a=newtime(a); 		if (6==a.weekday) 		{ 			b++; 		} 	} 	for (int i=0;i<fsn;i++) 	{ 		if (0!=i) 		{ 			a.monthday+=7; 		} 		a=newtime(a); 		cout<<"Next Saturday:"<<a.year<<"年"<<a.month<<"月" 			<<a.monthday<<"日"<<" "<<"星期"<<a.weekday<<endl; 	}  	//for (int i=1;i<=monthtotalday(year,fsn);++i) 	//{ 	//	time b; 	//	b.year=year; 	//	b.month=fsn; 	//	b.monthday=i; 	//	b.weekday=0; 	//	b=newtime(b); 	//	if (6==b.weekday) 	//	{ 	//		cout<<"Next Saturday:"<<b.year<<"年"<<b.month<<"月" 	//			<<b.monthday<<"日"<<" "<<"星期"<<b.weekday<<endl; 	//	} 	//}

user: user-0
content: 万分感谢您的支援 带我细细拜读下您的算法

user: user-3
content: 忘记注释了 然后没有严格按照你的格式用英文输出，相信你可以解决了  #include <iostream> using namespace std;  struct time{ 	int year; 	int month; 	int monthday; 	int weekday; };  //leapyear 计算 year是否为闰年 是返回true 否返回false bool leapyear(int year) { 	if((year%4==0&&year%100!=0)||(year%400==0)) return true; 	else return false; }  //返回某年份中某月份天数 int monthtotalday(int year,int month) { 	switch(month) 	{ 	case 2: 		if (true==leapyear(year))return 29; 		else return 28; 		break; 	case 1:;case 3:;case 5:;case 7:;case 8:;case 10:;case 12: 		return 31; 		break; 	case 4:;case 6:;case 9:;case 11: 		return 30; 		break; 	default: 		return 0; 		break; 	} }  //以年月日为基础计算星期 int weekday(time t) { 	int a; 	int totalday=0;//从格林威治开始时间计算到输入年份总天数 	for(int i=1970;i<t.year;++i)//以格林威治标准年份计算1970.1.1为星期四 	{ 		if (leapyear(i)==true)//闰年加366 平年加365 		{ 			totalday+=366; 		}else 		{ 			totalday+=365; 		} 	} 	for (int i=1;i<=t.month;++i)//加上每个月的天数 	{ 		totalday+=monthtotalday(t.year,i-1); 	} 	totalday+=t.monthday-1;//加上剩余天数（这里需要-1） 	totalday+=4;//加上格林威治的初始星期 	a=totalday%7;//总天数+初始星期数再摩除7得星期数。周日为0 	return a; }  //将时间格式重新转换 time newtime(time oldtime) { 	while(oldtime.monthday>monthtotalday(oldtime.year,oldtime.month))//天数大约当月最大天数进入循环 	{ 		oldtime.monthday-=monthtotalday(oldtime.year,oldtime.month);//天数大于当月最大天数进位 		oldtime.month++; 		if (oldtime.month>12)//月份数大于12则进位 		{ 			oldtime.month-=12; 			oldtime.year++; 		} 	} 	oldtime.weekday=weekday(oldtime);//计算星期 	return oldtime; }  void main() { 	int year,day,fsn; 	cout<<"Enter the year number:"; 	cin>>year; 	while(year<1970) 	{ 		cout<<"please enter ther year number(>1970):"; 		cin>>year; 	} 	cout<<"Enter the day number (1 - 365):"; 	cin>>day; 	while(day>366||day<1) 	{ 		cout<<"please enter the day number (1 - 365):"; 		cin>>day; 	} 	cout<<"Enter desired number of future Saturdays:"; 	cin>>fsn; 	while(fsn>12||fsn<1) 	{ 		cout<<"please enter desired number of future Saturdays(1-12):"; 		cin>>fsn; 	}  	time a; 	a.year=year; 	a.monthday=day; 	a.month=1; 	a.weekday=0; 	a=newtime(a); 	cout<<"Input date:"<<a.year<<"年"<<a.month<<"月" 		<<a.monthday<<"日"<<" "<<"星期"<<a.weekday<<endl;  	for (int i=1;i<=monthtotalday(year,fsn);++i) 	{ 		time b; 		b.year=year; 		b.month=fsn; 		b.monthday=i; 		b.weekday=0; 		b=newtime(b); 		if (6==b.weekday) 		{ 			cout<<"Next Saturday:"<<b.year<<"年"<<b.month<<"月" 				<<b.monthday<<"日"<<" "<<"星期"<<b.weekday<<endl; 		} 	} 	system("pause"); }

user: user-3
content: #include <iostream> using namespace std;  struct time{ 	int year; 	int month; 	int monthday; 	int weekday; };  bool leapyear(int year) { 	if((year%4==0&&year%100!=0)||(year%400==0)) return true; 	else return false; }  int monthtotalday(int year,int month) { 	switch(month) 	{ 	case 2: 		if (true==leapyear(year))return 29; 		else return 28; 		break; 	case 1:;case 3:;case 5:;case 7:;case 8:;case 10:;case 12: 		return 31; 		break; 	case 4:;case 6:;case 9:;case 11: 		return 30; 		break; 	default: 		return 0; 		break; 	} }  int weekday(time t) { 	int a; 	int totalday=0; 	for(int i=1970;i<t.year;++i)//以格林威治标准年份计算1970.1.1为星期四 	{ 		if (leapyear(i)==true) 		{ 			totalday+=366; 		}else 		{ 			totalday+=365; 		} 	} 	for (int i=1;i<=t.month;++i) 	{ 		totalday+=monthtotalday(t.year,i-1); 	} 	totalday+=t.monthday-1; 	totalday+=4; 	a=totalday%7; 	return a; }  time newtime(time oldtime) { 	while(oldtime.monthday>monthtotalday(oldtime.year,oldtime.month)) 	{ 		oldtime.monthday-=monthtotalday(oldtime.year,oldtime.month); 		oldtime.month++; 		if (oldtime.month>12) 		{ 			oldtime.month-=12; 			oldtime.year++; 		} 	} 	oldtime.weekday=weekday(oldtime); 	return oldtime; }  void main() { 	int year,day,fsn; 	cout<<"Enter the year number:"; 	cin>>year; 	while(year<1970) 	{ 		cout<<"please enter ther year number(>1970):"; 		cin>>year; 	} 	cout<<"Enter the day number (1 - 365):"; 	cin>>day; 	while(day>366||day<1) 	{ 		cout<<"please enter the day number (1 - 365):"; 		cin>>day; 	} 	cout<<"Enter desired number of future Saturdays:"; 	cin>>fsn; 	while(fsn>12||fsn<1) 	{ 		cout<<"please enter desired number of future Saturdays(1-12):"; 		cin>>fsn; 	}  	time a; 	a.year=year; 	a.monthday=day; 	a.month=1; 	a.weekday=0; 	a=newtime(a); 	cout<<"Input date:"<<a.year<<"年"<<a.month<<"月" 		<<a.monthday<<"日"<<" "<<"星期"<<a.weekday<<endl;  	for (int i=1;i<=monthtotalday(year,fsn);++i) 	{ 		time b; 		b.year=year; 		b.month=fsn; 		b.monthday=i; 		b.weekday=0; 		b=newtime(b); 		if (6==b.weekday) 		{ 			cout<<"Next Saturday:"<<b.year<<"年"<<b.month<<"月" 				<<b.monthday<<"日"<<" "<<"星期"<<b.weekday<<endl; 		} 	} 	system("pause"); }