我会这么写:
- void dateIncrease(int *year , int *month , int *day)
- {
- int daysInMonth;
- (*day)++;
- switch(*month)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- daysInMonth = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- daysInMonth = 30;
- break;
- case 2:
- if ( ((*year)%4 == 0 && (*year%100) != 0) || (*year)%400 == 0)
- daysInMonth = 29;
- else
- daysInMonth = 28;
- break;
- }
- if ((*day) > daysInMonth)
- {
- *day = 1;
- (*month)++;
- }
- if ((*month) > 12)
- {
- *month = 1;
- (*year)++;
- }
- }
- void main()
- {
- int cnt = 0;
- int year = 2000 , month = 1 , day = 1;
- int newYear , newMonth , newDay;
- printf("input a date:");
- scanf("%d%d%d" , &newYear , &newMonth , &newDay);
- while (1)
- {
- if (newYear == year && newMonth == month && newDay == day)
- break;
- dateIncrease(&year , &month , &day);//如果要计算任意两个日期的话,两个日期进行比较,时间较早的那个日期作为此函数的参数
- cnt++;
- }
- printf("there are %d days between the date %d, %d, %d and the date %d, %d, %d\n" , cnt , year , month , day , newYear , newMonth , newDay);
- }
复制代码 |