1

这是全部代码,每次运行程序时我仍然收到无效输入。

#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 天还很远。

4

1 回答 1

4

让我们把这个长得可笑的条件分开,并添加一些间距来看看会发生什么?

if( (begin.year>end.year) ||
    ( (begin.year==end.year) && (begin.month>end.month) ) || 
    begin.year==end.year) && (begin.month==end.month) && (begin.day>end.day)))

显然,第三行至少缺少一个括号。即使添加它,您也缺少另一对括号。让我们解决这两个问题,好吗?

if( (begin.year>end.year) ||
    ( (begin.year==end.year) && (begin.month>end.month) ) || 
    ( (begin.year==end.year) && (begin.month==end.month) && (begin.day>end.day)))

尽管如此,这很难快速阅读和理解。让我们把它分开并添加一些合理的间距,好吗?

int validate(void) 
{
    if(begin.year > end.year)
        return 1; /* time travelling is not allowed. Yet! */

    if((begin.year == end.year) && (begin.month > end.month))
        return 1; /* you can't begin after you end! */

    if((begin.year == end.year) && (begin.month == end.month) && (begin.day > end.day))
        return 1; /* finishing before you start isn't allowed by the union */

    return 0;
}

如果不了解这些数组dpm以及您调用的那些函数的更多信息,就不可能回答有关“关闭 200 天”的问题。

更新:您发布了一些额外的代码;仍然不够,但现在我的蜘蛛侠感觉刺痛了。有些事情告诉我,问题出在您EnterDate接受日期为“基于 1”的日期(即 2012 年 1 月 3 日输入为 2012 年 1 月 3 日),但您的数组是基于 0 的。

更新2:嗯......让我们在这里看看:

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!!!");

我没有走得更远。首先,int main(int n)不是main. 其次,我很震惊你声称你的validate()功能失败了,声称一直在努力寻找错误......你怎么知道?你甚至没有检查它的结果!

这个怎么样:

int main(int argc, char **argv) {
  printf("Please enter the first date then the second one\n");
  begin = EnterDate();
  end = EnterDate();

  if(validate()) {
      printf("Invalid Input!!!\n");
      return 1;
  }

  ...

更新 4:如果您费心在函数中添加printf语句count_days以查看执行情况,错误将立即对您变得明显。尝试这个:

int count_days(void){
    int i;
    int days=0;
    int days_per_year = 365;

    if(isleapyear(begin.year)){ // first year
        printf("%d is a leap year!\n", begin.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;

    printf("At first checkpoint: %d days\n", days); 

    for(i = (begin.month + 1); i < 13; ++i)
        days +=dpm [i];

    printf("At second checkpoint: %d days\n", days); 

    for(i = (begin.year + 1); i < end.year; ++i) //in between years
        days += (days_per_year + isleapyear(i));

    printf("At third checkpoint: %d days\n", days); 

    if(isleapyear(end.year))//end year
        printf("%d is a leap year!\n", 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;
}
于 2013-04-30T16:59:50.027 回答