-1

我的大学课程的这个 C 程序应该找到两个日期之间的天数。它在大多数情况下都有效,但是当月 1 和月 2 不同时,输出通常只是几天太高。此外,似乎无法识别具有 31 天的月份,导致在输入“31”作为日期时出现“无效的月份和日期组合”错误消息。我完全不确定为什么会出现这些问题。提前感谢您的帮助!

// Calculates the number of calendar days between any two dates in history (beginning with 1/1/1).

#include <stdio.h>
#include <stdlib.h>

void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2);
void leap(int year1, int year2, int *leap1, int *leap2);

int main(void)
{
  int month1, day1, year1, month2, day2, year2, leap1, leap2;

  date(&month1, &day1, &year1, &month2, &day2, &year2);
  leap(year1, year2, &leap1, &leap2);

  if(year1 == year2)
  {
    int i, total = 0;

    if(month1 == month2)                            // Total days if month1 == month2
    {
      total = day2 - day1;
      printf("There are %d days between the two dates.", total);
    }
    else
    {
      if(month1 == 1||3||5||7||8||10)         // Days remaining in first month
        total = 31 - day1;
      else if(month1 == 4||6||9||11)
        total = 30 - day1;
      else
      {
        if(leap1 == 1)
          total = 29 - day1;
        else
          total = 28 - day1;
      }

      for(i = month1 + 1; i < month2; i++)    // Days remaining between dates (excluding last month)
      {
        if(i == 3||5||7||8||10)
          total += 31;
        else if(i == 4||6||9||11)
          total += 30;
        else
        {
          if(leap1 == 1)
            total += 29;
          else
            total += 28;
        }
      }

      total += day2;                          // Final sum of days between dates (including last month)

      printf("There are %d days between the two dates.", total);
    }
  }
  else                                                    // If year1 != year2 ...
  {
    int i, total, century1 = ((year1 / 100) + 1) * 100, falseleap = 0;

    if(month1 == 1||3||5||7||8||10||12)             // Days remaining in first month
      total = 31 - day1;
    else if(month1 == 4||6||9||11)
      total = 30 - day1;
    else
    {
      if(leap1 == 1)
        total = 29 - day1;
      else
        total = 28 - day1;
    }

    for(i = month1 + 1; i <= 12; i++)               // Day remaining in first year
    {
      if(i == 3||5||7||8||10||12)
        total += 31;
      else if(i == 4||6||9||11)
        total += 30;
      else
      {
        if(leap1 == 1)
          total += 29;
        else
          total += 28;
      }
    }

    for(i = 1; i < month2; i++)                     // Days remaining in final year (excluding last month)
    {
      if(i == 1||3||5||7||8||10)
        total += 31;
      else if(i == 4||6||9||11)
        total += 30;
      else
      {
        if(leap2 == 1)
          total += 29;
        else
          total += 28;
      }
    }

    int leapcount1 = year1 / 4;                     // Leap years prior to and including first year
    int leapcount2 = year2 / 4;                     // Leap years prior to and NOT including final year
    if(year2 % 4 == 0)
      leapcount2 -= 1;

    int leaptotal = leapcount2 - leapcount1;        // Leap years between dates

    for(i = century1; i < year2; i += 100)          // "False" leap years (divisible by 100 but not 400)
    {
      if((i % 400) != 0)
        falseleap += 1;
    }

    total += 365 * (year2 - year1 - 1) + day2 + leaptotal - falseleap;      // Final calculation
    printf("There are %d days between the two dates.", total);
  }
  return 0;
}

void date(int *month1, int *day1, int *year1, int *month2, int *day2, int *year2)
{
  for(;;)                                                         // Infinite loop (exited upon valid input)
  {
    int fail = 0;
    printf("Enter first date: ");
    scanf("%d/%d/%d", month1, day1, year1);
    if(*month1 < 1 || *month1 > 12)
    {
      printf("Invalid entry for month.\n");
      fail += 1;
    }
    if(*day1 < 1 || *day1 > 31)
    {
      printf("Invalid entry for day.\n");
      fail += 1;
    }
    if(*year1 < 1)
    {
      printf("Invalid entry for year.\n");
      fail += 1;
    }
    if((*month1 == 4||6||9||11) && *day1 > 30)
    {
      printf("Invalid month and day combination.\n");
      fail += 1;
    }
    if(*month1 == 2)
    {
      if(*year1 % 4 == 0)
      {
        if(*year1 % 100 == 0)
        {
          if(*year1 % 400 == 0 && *day1 > 29)
          {
            printf("Invalid month and day combination.\n");
            fail += 1;
          }
          if(*year1 % 400 != 0 && *day1 > 28)
          {
            printf("Invalid month and day combination.\n");
            fail += 1;
          }
        }
        if(*year1 % 100 != 0 && *day1 > 29)
        {
          printf("Invalid month and day combination.\n");
          fail += 1;
        }
      }
      if(*year1 % 4 != 0 && *day1 > 28)
      {
        printf("Invalid month and day combination.\n");
        fail += 1;
      }
    }
    if(fail > 0)
      continue;
    else
      break;
  }

  for(;;)
  {
    int fail = 0;
    printf("Enter second date: ");
    scanf("%d/%d/%d", month2, day2, year2);
    if(*month2 < 1 || *month2 > 12)
    {
      printf("Invalid entry for month.\n");
      fail += 1;
    }
    if(*day2 < 1 || *day2 > 31)
    {
      printf("Invalid entry for day.\n");
      fail += 1;
    }
    if(*year2 < 1)
    {
      printf("Invalid entry for year.\n");
      fail += 1;
    }
    if((*month2 == 4||6||9||11) && *day2 > 30)
    {
      printf("Invalid month and day combination.\n");
      fail += 1;
    }
    if(*month2 == 2)
    {
      if(*year2 % 4 == 0)
      {
        if(*year2 % 100 == 0)
        {
          if(*year2 % 400 == 0 && *day2 > 29)
          {
            printf("Invalid month and day combination.\n");
            fail += 1;
          }
          if(*year2 % 400 != 0 && *day2 > 28)
          {
            printf("Invalid month and day combination.\n");
            fail += 1;
          }
        }
        if(*year2 % 100 != 0 && *day2 > 29)
        {
          printf("Invalid month and day combination.\n");
          fail += 1;
        }
      }
      if(*year2 % 4 != 0 && *day2 > 28)
      {
        printf("Invalid month and day combination.\n");
        fail += 1;
      }
    }
    if(fail > 0)
      continue;
    else
      break;
  }
}

void leap(int year1, int year2, int *leap1, int *leap2)             // Determines if first and final years are leap years
{
  if(year1 % 4 == 0)
  {
    if(year1 % 100 == 0)
    {
      if(year1 % 400 == 0)
        *leap1 = 1;
      else
        *leap1 = 0;
    }
    else
      *leap1 = 1;
  }
  else
    *leap1 = 0;

  if(year2 % 4 == 0)
  {
    if(year2 % 100 == 0)
    {
      if(year2 % 400 == 0)
        *leap2 = 1;
      else
        *leap2 = 0;
    }
    else
      *leap2 = 1;
  }
  else
    *leap2 = 0;
}
4

4 回答 4

1
if(month1 == 1||3||5||7||8||10)

总是计算为true,因为它已被解析

if ((month == 1) || 3 || 5 || 7 || 8 || 10)

并且任何非零整数的计算结果为true。另外,你忘记了十二月。

于 2012-04-24T00:06:08.677 回答
0

您的代码中有很多这样的构造:

value == 1||3||5||7||8||10||12

这不像你认为它在 C 中所做的那样,你必须这样写:

value == 1 || value == 3 || value == 5 || etc...

在您解决此问题并清理您的代码后,如果问题仍然存在(尽管提出一个新问题)。

于 2012-04-24T00:06:18.560 回答
0
i == 3||5||7||8||10||12

此条件将始终评估为true因为||不能用于区分不同的情况。这是一个逻辑运算符,任何不等于 0 的值都将计算为true.

我建议您将天数存储在数组中:

int daysPerMonth[] = {31,28,31,30 .. };

这样您就可以在daysPerMonth[i]没有那么多复杂条件的情况下轻松知道一个月有多少天。它也会提高可读性。

于 2012-04-24T00:08:16.903 回答
0

这个:

if (month1 == 1||3||5||7||8||10||12)

并没有完全按照你的想法去做。你的意思是:

if (month1 == 1 || month1 == 3 || month1 == 5 || 
     month1 == 7|| month1 == 8|| month1 == 10|| month1 == 12)

您的声明所做的是评估(month == 1) || 3。为此,它需要转换3为布尔值,这意味着它变为true,因为它不是零。因此,比较的结果是正确的,这意味着整个 if 子句也是正确的(事实上,惰性意味着它在这一点上实现了这个权利(或者如果月份实际上是更早1)并且它考虑到其余月份的数字,甚至不计算任何东西。)

因此,整个 if 子句的计算结果为true。在你的代码中有一大堆这些,你的代码必然会做一些奇怪的事情。

于 2012-04-24T00:11:36.167 回答