user-0 提问: 求高手简化代码

回复:
user: user-0
content: 谢谢     cfjtaishan自信男孩     谢谢      wodexiaojidan   学习了

user: user-1
content: #include <stdio.h>  static const int passdays[][12] = { 	0, 31, 59, 90, 120, 	151, 181, 212, 243,  	273, 304, 334, 	0, 31, 60, 91, 121, 	152, 182, 213, 244, 	274, 305, 335 };  static const char *weekday[] = { 	"MON", "TUE", "WEN", "THU",  	"FRI", "SAT", "SUN" };  static const int dayofmon[][12] = { 	31, 28, 31, 30, 	31, 30, 31, 31, 	30, 31, 30, 31, 	31, 29, 31, 30, 	31, 30, 31, 31, 	30, 31, 30, 31, };  static int FirstDayofYear(int year) { 	return (((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 + 1); }  static int FirstDayofMonth(int year, int month, int isleap) { 	int wday, mday;  	wday = FirstDayofYear(year); 	mday = (wday + passdays[isleap][month - 1]) % 7;  	return mday; }  static int WdayofToday(int year, int month, int day, int isleap) { 	int mday, tday;  	mday = FirstDayofMonth(year, month, isleap); 	tday = (mday + day - 1) % 7;  	return tday; }  static void Show(const char *s, const char *f, int day) { 	printf("%s%s%s\n", s, weekday[(day - 1 + 7) % 7], f); }  static char *NextDate(char *date, int year, int month, int day, int isleap) { 	int nyear, nmonth, nday;  	if(day == dayofmon[isleap][month - 1]){ 		nmonth = (month + 1) % 12; 		if(nmonth == 0){ 			nmonth = 12; 		}  		nday = 1; 	}else if(day >= 1 && day < dayofmon[isleap][month - 1]){ 		nmonth = month; 		nday = day + 1; 	} 	 	nyear = year + (nmonth < month);  	sprintf(date, ", %4d/%02d/%02d", nyear, nmonth, nday);  	return date; }  static char *FrontDate(char *date, int year, int month, int day, int isleap) { 	int fyear, fmonth, fday;  	if(day == 1){ 		fmonth = (month - 1 + 12) % 12; 		if(fmonth == 0){ 			fmonth = 12; 		}  		fday = dayofmon[isleap][fmonth - 1]; 	}else if(day > 1 && day <= dayofmon[isleap][month - 1]){ 		fmonth = month; 		fday = day - 1; 	}  	fyear = year - (fmonth > month);  	sprintf(date, ", %4d/%02d/%02d", fyear, fmonth, fday);  	return date; }  int main(void) { 	int year, month, day, tday, isleap, bcontinue; 	char date[16] = "";  	do{ 		scanf("%d%d%d", &year, &month, &day);  		if(year >= 1900 && year < 2100){ 			isleap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);  			if(month >= 1 && month < 13){ 				bcontinue = (day >= 1 && day <= dayofmon[isleap][month - 1]); 			}else{ 				bcontinue = 0; 			} 		}else{ 			bcontinue = 0; 		} 	}while(!bcontinue);  	tday = WdayofToday(year, month, day, isleap);  	FrontDate(date, year, month, day, isleap); 	Show("Yesterday is ", date, tday - 1);  	NextDate(date, year, month, day, isleap); 	Show("Tomorrow is ", date, tday + 1);  	return 0; }

user: user-1
content: #include <stdio.h>  static const int passdays[][12] = { 	0, 31, 59, 90, 120, 	151, 181, 212, 243,  	273, 304, 334, 	0, 31, 60, 91, 121, 	152, 182, 213, 244, 	274, 305, 335 };  static const char *weekday[] = { 	"MON", "TUE", "WEN", "THU",  	"FRI", "SAT", "SUN" };  static const int dayofmon[][12] = { 	31, 28, 31, 30, 	31, 30, 31, 31, 	30, 31, 30, 31, 	31, 29, 31, 30, 	31, 30, 31, 31, 	30, 31, 30, 31, };  static int FirstDayofYear(int year) { 	return (((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 + 1); }  static int FirstDayofMonth(int year, int month, int isleap) { 	int wday, mday;  	wday = FirstDayofYear(year); 	mday = (wday + passdays[isleap][month - 1]) % 7;  	return mday; }  static int WdayofToday(int year, int month, int day, int isleap) { 	int mday, tday;  	mday = FirstDayofMonth(year, month, isleap); 	tday = (mday + day - 1) % 7;  	return tday; }  static void Show(const char *s, const char *f, int day) { 	printf("%s%s%s\n", s, weekday[(day - 1 + 7) % 7], f); }  static char *NextDate(char *date, int year, int month, int day, int isleap) { 	int nyear, nmonth, nday;  	if(day == dayofmon[isleap][month - 1]){ 		nmonth = (month + 1) % 13; 		if(nmonth == 0){ 			nmonth = 1; 		}  		nday = 1; 	}else if(day >= 1 && day < dayofmon[isleap][month - 1]){ 		nmonth = month; 		nday = day + 1; 	} 	 	nyear = year + (nmonth < month);  	sprintf(date, ", %4d/%02d/%02d", nyear, nmonth, nday);  	return date; }  static char *FrontDate(char *date, int year, int month, int day, int isleap) { 	int fyear, fmonth, fday;  	if(day == 1){ 		fmonth = (month - 1 + 12) % 13; 		if(fmonth == 0){ 			fmonth = 1; 		}  		fday = dayofmon[isleap][fmonth - 1]; 	}else if(day > 1 && day <= dayofmon[isleap][month - 1]){ 		fmonth = month; 		fday = day - 1; 	}  	fyear = year - (fmonth > month);  	sprintf(date, ", %4d/%02d/%02d", fyear, fmonth, fday);  	return date; }  int main(void) { 	int year, month, day, tday, isleap, bcontinue; 	char date[16] = "";  	do{ 		scanf("%d%d%d", &year, &month, &day);  		if(year >= 1900 && year < 2100){ 			isleap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);  			if(month >= 1 && month < 13){ 				bcontinue = (day >= 1 && day <= dayofmon[isleap][month - 1]); 			}else{ 				bcontinue = 0; 			} 		}else{ 			bcontinue = 0; 		} 	}while(!bcontinue);  	tday = WdayofToday(year, month, day, isleap);  	FrontDate(date, year, month, day, isleap); 	Show("Yesterday is ", date, tday - 1);  	NextDate(date, year, month, day, isleap); 	Show("Tomorrow is ", date, tday + 1);  	return 0; }  2017 1 1 Yesterday is SAT, 2016/12/31 Tomorrow is MON, 2017/01/02  2017 2 1 Yesterday is TUE, 2017/01/31 Tomorrow is THU, 2017/02/02  2016 12 25 Yesterday is SAT, 2016/12/24 Tomorrow is MON, 2016/12/26  2016 2 28 Yesterday is SAT, 2016/02/27 Tomorrow is MON, 2016/02/29  2016 2 29 Yesterday is SUN, 2016/02/28 Tomorrow is TUE, 2016/03/01  2017 2 28 Yesterday is MON, 2017/02/27 Tomorrow is WEN, 2017/03/01

user: user-2
content: #include <stdio.h>  struct date_t {     int year;     int month;     int day; }; int gmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  int is_leap(int year); void tomorrow(struct date_t *ptm); void yesterday(struct date_t *ptm);  int main(void) {     struct date_t tm;     int y, m, d;      do {         scanf("%d%d%d", &y, &m, &d);         if (is_leap(y) && m == 2 && d == 29)             break;     } while (m > 12 || m <= 0 || d > gmonth[m-1] || d <= 0);      tm.year = y;     tm.month = m;     tm.day = d - 1;     yesterday(&tm);     printf("%4d-%02d-%02d's yesterday is %4d-%02d-%02d\n", y, m, d, tm.year, tm.month, tm.day);      tm.year = y;     tm.month = m;     tm.day = d + 1;     tomorrow(&tm);     printf("%4d-%02d-%02d's tomorrow  is %4d-%02d-%02d\n", y, m, d, tm.year, tm.month, tm.day);      return 0; }   int is_leap(int year) {     if ((year % 4 == 0 && year % 100 != 0)         || year % 400 == 0)         return 1;      return 0; }  void yesterday(struct date_t *ptm) {     if (ptm->day <= 0) {         ptm->month -= 1;         if (ptm->month <= 0) {             ptm->year -= 1;             if (ptm->year <= 0) {                 ptm->year = 0;                 ptm->month = 0;                 ptm->day = 0;                 return;             }             ptm->month = 12;             ptm->day = gmonth[ptm->month-1] + ptm->day;             return;         }         ptm->day = gmonth[ptm->month-1] + ptm->day;         if (ptm->month == 2 && is_leap(ptm->year))             ptm->day += 1;         return;     } }  void tomorrow(struct date_t *ptm) {     if (ptm->month == 2 && is_leap(ptm->year)         && ptm->day == gmonth[ptm->month - 1] + 1)         return;     if (ptm->day > gmonth[ptm->month - 1]) {         ptm->month += 1;         if (ptm->month > 12) {             ptm->year += 1;             ptm->month = 1;             ptm->day = 1;             return;         }         ptm->day = 1;         return;     } }  参考一下吧 就是比较比较多。

user: user-1
content: static int passdays[] = { 	0, 31, 59, 90, 120, 	151, 181, 212, 243,  	273, 304, 334 };  static char *weekday[] = { 	"MON", "TUE", "WEN", "THU",  	"FRI", "SAT", "SUN" };  static int FirstDayofYear(int year) { 	int i; 	if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){ 		for(i = 2; i < 12; ++i){ 			passdays[i] += 1; 		} 	}  	return (((year - 1) + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7 + 1); }  static int FirstDayofMonth(int year, int month) { 	int wday, mday;  	wday = FirstDayofYear(year); 	mday = (wday + passdays[month - 1]) % 7;  	return mday; }  static WdayofToday(int year, int month, int day) { 	int mday, tday; 	 	mday = FirstDayofMonth(year, month); 	tday = (mday + day - 1) % 7;  	return tday; }  static void Show(const char *s, int day) { 	printf("%s%s\n", s, weekday[(day - 1 + 7) % 7]); }  int main(void) { 	int year, month, day, tday, isleap, bcontinue;  	do{ 		scanf("%d%d%d", &year, &month, &day);  		if(year >= 1900 && year < 2100){ 			isleap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0); 		 			if(month >= 1 && month < 13){ 				switch(month){ 					case 1: 					case 3: 					case 5: 					case 7: 					case 8: 					case 10: 					case 12: 						bcontinue = (day >= 1 && day < 32); 						break;  					case 4: 					case 6: 					case 9: 					case 11: 						bcontinue = (day >= 1 && day < 31);  					case 2: 						bcontinue = (isleap ? (day >= 1 && day < 30) : (day >= 1 && day < 29)); 						break; 					default: 						break; 				} 			}else{ 				bcontinue = 0; 			} 		}else{ 			bcontinue = 0; 		} 	}while(!bcontinue);  	tday = WdayofToday(year, month, day);  	Show("Yesterday is ", tday - 1); 	Show("Tomorrow is ", tday + 1);  	return 0; }