我通过实用C编程书工作,似乎无法弄清楚我搞砸了哪里。我的程序代码计算日期之间的日期是:
/* Write a program to perform date arithmetic such as how many days there are
* between 6/6/90 and 4/3/92. Include a specification and a code design. */
/****************************************************************
* This program will ask a user to enter in a beginning date *
* and an ending date formated as MM/DD/YYYY and calculate the *
* number of days between those two dates. *
***************************************************************/
#include <stdio.h>
//function to check leap years.
int leapYear(int year) {
if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0)
return 1;
else
return 0;
}
//function to calculate the days between to dates.
int daysBetweenDates(int start_date[], int end_date[]) {
int days = 0; /*****THIS IS WHERE THE ERROR WAS I DIDN'T INITIALIZE THE VARIABLE*****/ //variable to keep track of the days counted.
int month_counter, year_counter; //counters for the current month and year in the loops.
//loop over the years
for (year_counter = start_date[2]; year_counter <= end_date[2];
year_counter += 1) {
if (year_counter == start_date[2]) //if we're in the beginning year,
month_counter = start_date[0]; //start the month counter at the given start month.
else
month_counter = 1; //otherwise start in January.
//loop through the months
while (month_counter <= 12) {
//if we're in the final month of the final year we break and add the days in that month separately.
if (year_counter == end_date[2] && month_counter == end_date[0])
break;
//add the number of days in the current month to the days variable.
switch (month_counter) {
case 1:
days += 31;
break;
case 2:
days += 28;
//add extra day for leap years.
if (leapYear(year_counter))
days += 1;
break;
case 3:
days += 31;
break;
case 4:
days += 30;
break;
case 5:
days += 31;
break;
case 6:
days += 30;
break;
case 7:
days += 31;
break;
case 8:
days += 31;
break;
case 9:
days += 30;
break;
case 10:
days += 31;
break;
case 11:
days += 30;
break;
case 12:
days += 31;
break;
default:
break;
}
//next month.
month_counter += 1;
}
}
//add difference of the days from the start and end dates. (why we skipped the last month.)
days += (end_date[1] - start_date[1]);
return days;
}
//function to make sure dates given are valid.
int checkDates(int start_date[], int end_date[]) {
if (start_date[2] > end_date[2])
return 1;
if (start_date[2] == end_date[2] && start_date[0] > end_date[0])
return 1;
if (start_date[2] == end_date[2] && start_date[0] == end_date[0]
&& start_date[1] > end_date[1])
return 1;
else
return 0;
}
int main() {
int start_date[3];
int end_date[3];
char last_day[3];
int add_last_day;
printf("Please enter in the start date (MM/DD/YYYY): ");
scanf("%d/%d/%d", &start_date[0], &start_date[1], &start_date[2]);
printf("Please enter in the end date (MM/DD/YYYY): ");
scanf("%d/%d/%d", &end_date[0], &end_date[1], &end_date[2]);
if(checkDates(start_date, end_date)){
printf("Invalid Dates.\n");
return 1;
}
printf("Should the last day be counted? (Y/N) ");
scanf("%s", last_day);
if(last_day[0] == 'Y' || last_day[0] == 'y')
add_last_day = 1;
else
add_last_day = 0;
printf("between %d/%d/%d and %d/%d/%d there are %d days.\n", start_date[0],
start_date[1], start_date[2], end_date[0], end_date[1], end_date[2],
daysBetweenDates(start_date, end_date) + add_last_day);
return 0;
}
该计划的结果如下:
$ ./ex_7-2
Please enter in the start date (MM/DD/YYYY): 1/1/2014
Please enter in the end date (MM/DD/YYYY): 1/31/2014
between 1/1/2014 and 1/31/2014 there are 32797 days.
这显然是错误的。我错过了什么?