user-0 提问: 新手写日历，发现越调试错误越多

回复:
user: user-0
content: 1 #include<stdio.h>   2 #include<string.h>   3 #include<time.h>   4 #include<math.h>   5    6 int nmonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   7    8 long since(int year, int month, int day);   9   10   11 int main()  12 {  13         long    sum_day_1;  14         long    sum_day_2;  15         int     gap;  16         int     e_year, e_month, e_day, e_wday;  17         struct  tm *tp;  18         long    t0;  19         int     e_date;  20         int     remainder;  21         int     i, j;  22         int     m;  23         int     first_wday;  24   25         remainder                       = 0;  26         first_wday                      = 0;  27         e_year, e_month, e_day, e_wday  = 0;  28         sum_day_1 = sum_day_2           = 0;  29   30         printf("Please enter the year month day before today:(seperate wi    th blank)\n");  31   32         e_date  =scanf("%d%d%d", &e_year, &e_month, &e_day);  33   34         //Get current time  35         t0      = time(NULL);  36   37         //Get current struct time 38         tp      = localtime(&t0);  39   40         //Calculate the day from beginning by using function since  41         sum_day_1       = since(tp->tm_year+1900, tp->tm_mon+1, tp->tm_md    ay);  42         sum_day_2       = since(e_year, e_month, e_day);  43   44         //Calculate the gap between 2 days  45         gap             = sum_day_1 - sum_day_2;  46   47         //Calculate the remainder of the gap  48         remainder       = gap % 7;  49   50         //Calculate the weekday of the enter day  51         if(tp->tm_wday  == 0){  52                 if(remainder    ==0)  53                         e_wday  = 0;  54                 else  55                         e_wday  = 7 - remainder;  56         }  57         else if (tp->tm_wday >= remainder)  58                 e_wday  = tp->tm_wday - remainder;  59         else if (0 < tp->tm_wday < remainder)  60                 e_wday  = 7 - (remainder - tp->tm_wday) ;  61   62         if(e_wday == 7)  63                 e_wday == 0;  64   65         //Printf the title of the week  66         printf("Mon Tue Wed Thu Fri Sat Sun\n");  67   68         //Calculate the weekday of the first day in the month  69         if(e_wday != 0){  70                 if(e_day < e_wday)  71                         first_wday = e_wday - e_day + 1;  72                 else  73                         first_wday = 8 - (abs(e_day - e_wday) % 7); 74         }  75         else  76                 first_wday = 8 - (e_day % 7);  77   78         if (first_wday == 7)  79                 first_wday = 0;  80         if (first_wday == 8)  81                 first_wday = 1;  82   83         //Calculate the empty date of the month in calender  84         if(first_wday   != 0){  85                 for(i = 1; i < first_wday; ++i)  86                         printf("    ");  87         }  88         else  89                 for(i = 1; i <= 6; ++i)  90                         printf("    ");  91   92         i = i -1 ;//Easy for next step to do  93   94         //Enter thr current calender  95         for(m =1; m <= nmonth[e_month]; ++m){  96                 if(i % 7 == 0 )  97                         printf("\n");  98                 ++i;  99                 { 100                 if(m    == e_day) 101                         printf("[%2d]", e_day); 102                 else 103                         printf(" %2d ", m); 104                 } 105         } 106         //When leep year plus 29 to January 107         if((e_year % 4 == 0 && e_year % 100 != 0 || e_year % 400 == 0) &&     e_month ==2) 108                  printf(" %2d ", 29); 109          printf("\n"); 110  111          return 0; 112  113 } 114  115 //Calculate the day since biginning 116 long since(int year, int month, int day) 117 { 118         long    sum; 119         int     i, j; 120         sum     =0; 121  122         for(i = 0; i < year; ++i){ 123                 if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) 124                 sum += 366; 125                 else 126                 sum += 365; 127         } 128  129         if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){ 130                 for(j = 1; j < month; ++j) 131                         sum += nmonth[j]; 132                 if(month > 2) 133                         sum += 1; 134         } 135         else 136                 for(j = 1; j < month; ++j) 137                         sum += nmonth[j]; 138  139         sum     = sum + day; 140  141         return  sum; 142  143 } 144     非常感谢你的代码。 我睡觉前会仔细研究一下的。 不过我自己通过gdb已经发现了自己代码中存在的问题  并且也重新整理了格式， 只是这个算法是我自己想得太水乐。 目前只能实现在今天之前的日历打印。 不过我还是很开心。  好拉， 关电脑， 研究您的代码了~

user: user-1
content: #include<stdio.h> #include<time.h> #include<string.h> #include<math.h> #include <stdlib.h>  long since(int year, int month, int day); int empty(int year);  int nmonth[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  int main() {     long    sum_day_1;     long    sum_day_2;     int     gap;     int     e_year, e_month, e_day, e_wday;     struct  tm *tp;     time_t    t0;     int     e_date;     int     remainder;     int i, j;     int m;     int first_wday;     remainder = 0;     first_wday = 0;     e_year = 0;     e_month = 0;     e_day = 0;     e_wday = 0;      printf("Please inter year month day:(seperate with blank)\n");     e_date = scanf("%d%d%d", &e_year, &e_month, &e_day);      sum_day_1 = sum_day_2 = gap = 0;      //Get current time     t0 = time(NULL);      //Get current struct time     tp = localtime(&t0);      printf("mday = %d\n", tp->tm_mday);      //Because of my US time have to plus 4 to tm_day     sum_day_1 = since(tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday);     sum_day_2 = since(e_year, e_month, e_day);      gap = abs(sum_day_1 - sum_day_2);      printf("mwday = %d\n", tp->tm_wday);     printf("gap = %d\n", gap);      //Calulate the remainder of the gap % 7     remainder = gap % 7;      if (tp->tm_wday == 0)         e_wday = remainder;     else if (tp->tm_wday > remainder)         e_wday = tp->tm_wday - remainder;     else if (0 < tp->tm_wday && tp->tm_wday < remainder)         e_wday = 7 - abs((tp->tm_wday - remainder));      if (e_wday == 7)         e_wday = 0;      printf("remainder = %d\n", remainder);     printf("e_wday = %d\n", e_wday);      printf("Mon Tue Wed Thu Fri Sat Sun \n");       //int empty(int tm.tm_wday, int remainder);      //Calculate the weekday of the first day in the month     if (e_wday != 0) {         if (e_day < e_wday)             first_wday = e_wday - e_day;         else             first_wday = 8 - (abs(e_day - e_wday) % 7);     }     else         first_wday = 8 - (e_day % 7);      printf("first_wday = %d\n", first_wday);      if (first_wday == 7)         first_wday = 0;     //      if (first_wday == 8)     //              first_wday = 1;      //Calculate the empty date of the month in calendar     if (first_wday != 0) {         for (i = 1; i < first_wday; ++i)             printf("    ");     }     else         for (i = 1; i <= 6; ++i)             printf("    ");      i = i - 1;     //      printf("remainder = %d\n",remainder);      //enter thr current calender     for (m = 1; m <= nmonth[e_month]; ++m) {         //      for(i = i-1; i <= nmonth[e_month]; ++i)         if (i % 7 == 0)             printf("\n");         ++i;         {             if (m == e_day)                 printf("[%2d]", e_day);             else                 printf(" %2d ", m);         }     }       if ((e_year % 4 == 0 && e_year % 100 != 0 || e_year % 400 == 0) && e_month == 2)         printf(" %2d ", 29);      printf("\n");     fflush(stdin);     getchar();     return  0; }  //calculate the days since the biginning day to the specific day long since(int year, int month, int day) {      long sum;     int  i, j;     sum = 0;     for (i = 1; i < year; ++i) {         if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)             sum += 366;// leap year         else             sum += 365;// not leap year     }      if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {         // leap year         for (j = 1; j < month; ++j)             sum += nmonth[j];         sum += 1;     }     else         for (j = 1; j < month; ++j)             sum += nmonth[j];     sum += 365;// not leap year     sum = sum + day;      return sum; }

user: user-0
content: 引用 2 楼 user-1 的回复:// console.cpp : Defines the entry point for the console application. //  #include "stdafx.h" #include<stdio.h> #include<time.h> #include<string.h> #include<math.h>   long since(int year, int month, int day); int empty(int year);  int nmonth[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  int main() { 	long    sum_day_1; 	long    sum_day_2; 	int     gap; 	int     e_year, e_month, e_day, e_wday; 	struct  tm tm; 	time_t    t0; 	int     e_date; 	int     remainder; 	int i, j; 	int m; 	int first_wday; 	remainder = 0; 	first_wday = 0; 	e_year, e_month, e_day, e_wday = 0;  	printf("Please inter year month day:(seperate with blank)\n"); 	e_date = scanf_s("%d%d%d", &e_year, &e_month, &e_day);  	sum_day_1 = sum_day_2 = gap = 0;  	//Get current time 	t0 = time(NULL);  	//Get current struct time 	 localtime_s(&tm, &t0);  	printf("mday = %d\n", tm.tm_mday);  	//Because of my US time have to plus 4 to tm_day 	sum_day_1 = since(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); 	sum_day_2 = since(e_year, e_month, e_day);  	gap = abs(sum_day_1 - sum_day_2);  	printf("mwday = %d\n", tm.tm_wday); 	printf("gap = %d\n", gap);  	//Calulate the remainder of the gap % 7 	remainder = gap % 7;  	if (tm.tm_wday == 0) 		e_wday = remainder; 	else if (tm.tm_wday > remainder) 		e_wday = tm.tm_wday - remainder; 	else if (0 < tm.tm_wday && tm.tm_wday < remainder) 		e_wday = 7 - abs((tm.tm_wday - remainder));  	if (e_wday = 7) 		e_wday = 0;  	printf("remainder = %d\n", remainder); 	printf("e_wday = %d\n", e_wday);  	printf("Sun Mon Tue Wed Thu Fri Sat\n");   	//int empty(int tm.tm_wday, int remainder);  	//Calculate the weekday of the first day in the month 	if (e_wday != 0) { 		if (e_day < e_wday) 			first_wday = e_wday - e_day; 		else 			first_wday = 8 - (abs(e_day - e_wday) % 7); 	} 	else 		first_wday = 8 - (e_day % 7);  	printf("first_wday = %d\n", first_wday);  	if (first_wday == 7) 		first_wday = 0; 	//      if (first_wday == 8) 	//              first_wday = 1;  	//Calculate the empty date of the month in calendar 	if (first_wday != 0) { 		for (i = 1; i < first_wday; ++i) 			printf("    "); 	} 	else 		for (i = 1; i <= 6; ++i) 			printf("    ");  	i = i - 1; 	//      printf("remainder = %d\n",remainder);  	//enter thr current calender 	for (m = 1; m <= nmonth[e_month]; ++m) { 		//      for(i = i-1; i <= nmonth[e_month]; ++i) 		if (i % 7 == 0) 			printf("\n"); 		++i; 		{ 			if (m == e_day) 				printf("[%2d]", e_day); 			else 				printf(" %2d ", m); 		} 	}   	if ((e_year % 4 == 0 && e_year % 100 != 0 || e_year % 400 == 0) && e_month == 2) 		printf(" %2d ", 29);  	printf("\n"); 	fflush(stdin); 	getchar(); 	return  0; }  //calculate the days since the biginning day to the specific day long since(int year, int month, int day) {  	long sum; 	int  i, j; 	sum = 0; 	for (i = 1; i < year; ++i) { 		if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) 			sum += 366;// leap year 		else 			sum += 365;// not leap year 	}  	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 		// leap year 		for (j = 1; j < month; ++j) 			sum += nmonth[j]; 		sum += 1; 	} 	else 		for (j = 1; j < month; ++j) 			sum += nmonth[j]; 	sum += 365;// not leap year 	sum = sum + day;  	return sum; }   linux 中GCC 编译 报错呀。

user: user-1
content: // console.cpp : Defines the entry point for the console application. //  #include "stdafx.h" #include<stdio.h> #include<time.h> #include<string.h> #include<math.h>   long since(int year, int month, int day); int empty(int year);  int nmonth[13] = { 0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };  int main() { 	long    sum_day_1; 	long    sum_day_2; 	int     gap; 	int     e_year, e_month, e_day, e_wday; 	struct  tm tm; 	time_t    t0; 	int     e_date; 	int     remainder; 	int i, j; 	int m; 	int first_wday; 	remainder = 0; 	first_wday = 0; 	e_year, e_month, e_day, e_wday = 0;  	printf("Please inter year month day:(seperate with blank)\n"); 	e_date = scanf_s("%d%d%d", &e_year, &e_month, &e_day);  	sum_day_1 = sum_day_2 = gap = 0;  	//Get current time 	t0 = time(NULL);  	//Get current struct time 	 localtime_s(&tm, &t0);  	printf("mday = %d\n", tm.tm_mday);  	//Because of my US time have to plus 4 to tm_day 	sum_day_1 = since(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); 	sum_day_2 = since(e_year, e_month, e_day);  	gap = abs(sum_day_1 - sum_day_2);  	printf("mwday = %d\n", tm.tm_wday); 	printf("gap = %d\n", gap);  	//Calulate the remainder of the gap % 7 	remainder = gap % 7;  	if (tm.tm_wday == 0) 		e_wday = remainder; 	else if (tm.tm_wday > remainder) 		e_wday = tm.tm_wday - remainder; 	else if (0 < tm.tm_wday && tm.tm_wday < remainder) 		e_wday = 7 - abs((tm.tm_wday - remainder));  	if (e_wday = 7) 		e_wday = 0;  	printf("remainder = %d\n", remainder); 	printf("e_wday = %d\n", e_wday);  	printf("Sun Mon Tue Wed Thu Fri Sat\n");   	//int empty(int tm.tm_wday, int remainder);  	//Calculate the weekday of the first day in the month 	if (e_wday != 0) { 		if (e_day < e_wday) 			first_wday = e_wday - e_day; 		else 			first_wday = 8 - (abs(e_day - e_wday) % 7); 	} 	else 		first_wday = 8 - (e_day % 7);  	printf("first_wday = %d\n", first_wday);  	if (first_wday == 7) 		first_wday = 0; 	//      if (first_wday == 8) 	//              first_wday = 1;  	//Calculate the empty date of the month in calendar 	if (first_wday != 0) { 		for (i = 1; i < first_wday; ++i) 			printf("    "); 	} 	else 		for (i = 1; i <= 6; ++i) 			printf("    ");  	i = i - 1; 	//      printf("remainder = %d\n",remainder);  	//enter thr current calender 	for (m = 1; m <= nmonth[e_month]; ++m) { 		//      for(i = i-1; i <= nmonth[e_month]; ++i) 		if (i % 7 == 0) 			printf("\n"); 		++i; 		{ 			if (m == e_day) 				printf("[%2d]", e_day); 			else 				printf(" %2d ", m); 		} 	}   	if ((e_year % 4 == 0 && e_year % 100 != 0 || e_year % 400 == 0) && e_month == 2) 		printf(" %2d ", 29);  	printf("\n"); 	fflush(stdin); 	getchar(); 	return  0; }  //calculate the days since the biginning day to the specific day long since(int year, int month, int day) {  	long sum; 	int  i, j; 	sum = 0; 	for (i = 1; i < year; ++i) { 		if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) 			sum += 366;// leap year 		else 			sum += 365;// not leap year 	}  	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { 		// leap year 		for (j = 1; j < month; ++j) 			sum += nmonth[j]; 		sum += 1; 	} 	else 		for (j = 1; j < month; ++j) 			sum += nmonth[j]; 	sum += 365;// not leap year 	sum = sum + day;  	return sum; }

user: user-0
content: 1 #include<stdio.h>   2 #include<time.h>   3 #include<string.h>   4 #include<math.h>   5    6 long since(int year, int month, int day);   7 int empty(int year);   8    9 int nmonth[13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  10   11 int main()  12 {  13         long    sum_day_1;  14         long    sum_day_2;  15         int     gap;  16         int     e_year, e_month, e_day, e_wday;  17         struct  tm *tp;  18         long    t0;  19         int     e_date;  20         int     remainder;  21         int i, j;  22         int m;  23         int first_wday;  24         remainder       =0;  25         first_wday      =0;  26         e_year, e_month, e_day, e_wday  = 0;  27   28         printf("Please inter year month day:(seperate with blank)\n");  29         e_date  =scanf("%d%d%d", &e_year, &e_month, &e_day);  30   31         sum_day_1 = sum_day_2 = gap = 0;  32   33         //Get current time  34         t0      = time(NULL);  35   36         //Get current struct time  37         tp      = localtime(&t0);  38   39         printf("mday = %d\n", tp->tm_mday);  40   41         //Because of my US time have to plus 4 to tm_day  42         sum_day_1 = since(tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday);  43         sum_day_2 = since(e_year, e_month, e_day);  44   45         gap = abs(sum_day_1 - sum_day_2); 46   47         printf("mwday = %d\n",tp->tm_wday);  48         printf("gap = %d\n",gap);  49   50         //Calulate the remainder of the gap % 7  51         remainder       = gap % 7;  52   53         if(tp->tm_wday == 0)  54         e_wday  = remainder;  55         else if (tp->tm_wday > remainder)  56         e_wday  = tp->tm_wday - remainder;  57         else if (0 < tp->tm_wday < remainder)  58         e_wday  = 7 - abs((tp->tm_wday - remainder)) ;  59   60         if(e_wday = 7)  61                 e_wday == 0;  62   63         printf("remainder = %d\n",remainder);  64         printf("e_wday = %d\n", e_wday);  65   66         printf("Mon Tue Wed Thu Fri Sat Sun\n");  67   68   69                      //int empty(int tp->tm_wday, int remainder);  70   71         //Calculate the weekday of the first day in the month  72         if(e_wday != 0){  73                 if(e_day < e_wday)  74                         first_wday = e_wday - e_day;  75                 else  76                 first_wday = 8 - (abs(e_day - e_wday) % 7);  77         }  78         else  79                 first_wday = 8 - (e_day % 7);  80   81         printf("first_wday = %d\n", first_wday);  82   83         if (first_wday == 7)  84                 first_wday = 0;  85 //      if (first_wday == 8)  86 //              first_wday = 1;  87   88         //Calculate the empty date of the month in calendar 89         if(first_wday   != 0){  90                 for(i = 1; i < first_wday; ++i)  91                         printf("    ");  92                 }  93         else  94                 for(i = 1; i <= 6; ++i)  95                         printf("    ");  96   97         i = i -1 ;  98 //      printf("remainder = %d\n",remainder);  99  100         //enter thr current calender 101         for(m =1; m <= nmonth[e_month]; ++m){ 102         //      for(i = i-1; i <= nmonth[e_month]; ++i) 103                 if(i % 7 == 0 ) 104                         printf("\n"); 105                 ++i; 106                 { 107                 if(m    == e_day) 108                         printf("[%2d]", e_day); 109                 else 110                         printf(" %2d ", m); 111                 } 112         } 113  114  115         if((e_year % 4 == 0 && e_year % 100 != 0 || e_year % 400 == 0) && e__     month ==2) 116                 printf(" %2d ", 29); 117  118         printf("\n"); 119  120         return  0; 121 } 122  123 //calculate the days since the biginning day to the specific day 124 long since(int year, int month, int day) 125 { 126  127         long sum; 128         int  i, j; 129         sum     = 0; 130         for(i = 1; i < year; ++i){ 131                 if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) 132                         sum += 366;// leap year 133                 else 134                         sum += 365;// not leap year 135         } 136  137         if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){ 138                 // leap year 139                 for(j = 1; j < month; ++j) 140                         sum += nmonth[j]; 141                 sum += 1; 142         } 143         else 144                 for(j = 1; j < month; ++j) 145                         sum += nmonth[j]; 146                 sum += 365;// not leap year 147         sum = sum + day; 148  149                 return sum; 150 }