这是全部代码,每次运行程序时我仍然收到无效输入。
#include<stdio.h>
int dpm[13]={31,28,31,30,31,30,31,31,30,31,30,31};
struct Sdate {
int month;
int day;
int year;
} begin, end;
int isleapyear(int a)
{
if((a%4==0&&a%100!=0)||a%400==0)
return 1;
else
return 0;
}
struct Sdate EnterDate(void){
int d,m,y;
struct Sdate A;
printf("Enter the date(mm/dd/yyyy): ");
scanf("%i/%i/%i", &m, &d, &y);
printf("\n");
A.month=m;
A.day=d;
A.year=y;
return(A);
}
int validate(void){
if(begin.year > end.year)
return 1;
if((begin.year == end.year) && (begin.month > end.month))
return 1;
if((begin.year == end.year) && (begin.month == end.month) && (begin.day > end.day))
return 1;
else
return 0;
}
int count_days(void){
int i;
int days=0;
int days_per_year = 365;
if(isleapyear(begin.year)){ // first year
if(begin.month == 1)
days = dpm[begin.month] - begin.day + 1;
else
days = dpm[begin.month] - begin.day;
}
else
days = dpm[begin.month] - begin.day;
for(i = (begin.month + 1); i < 13; ++i)
days +=dpm [i];
for(i = (begin.year + 1); i < end.year; ++i) //in between years
days += (days_per_year + isleapyear(i));
if(isleapyear(end.year))//end year
for(i = 0; i < end.month; ++i){
days += dpm[i] + 1;
days += dpm[end.month] - end.day;
}
else
for(i = 0; i < end.month; ++i)
days += dpm[i];
days += dpm[end.month] - end.day;
return days;
}
int main(int n){
printf("Please enter the first date then the second one\n");
begin=EnterDate();
end=EnterDate();
validate();
if(n == 1)
printf("Invalid Input!!!");
else
printf("Total days between these two dates is %i\n", count_days());
return 0;
}
代码仍然说验证总是正确的,使输出成为“无效输入”,我只能检查程序计数天数,但当我这样做时,它离一年或大约 200 天还很远。