闰年和长/短月次日日期程序

时间:2020-12-29 20:44:57

标签: c date

有一种简单的方法可以包含一年中的所有“天”条件?闰年,长/短月等... 试图让它更短,但对于像我这样的代码新手来说更容易理解

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

typedef struct date
{
    int day;
    int month;
    int year;
} date;

int main()
{
    date d;
    printf("\nPlease enter the date DD/MM/YYYY format and i will raise it up \n");
    scanf_s("%d/%d/%d", &d.day, &d.month, &d.year);
    printf("you enter: %02d/%02d/%04d", d.day, d.month, d.year);
    d.day++;
    if (d.day == 29 && d.month == 2)
    {
        d.month++;
        d.day = 1;
    }
    if (d.day == 32 && d.month == 3)
    {
        d.month++;
        d.day = 1;
    }
    if (d.day == 32 && d.month == 12)
    {
        d.year++;
        d.month = 1;
        d.day = 1;
    }
    printf("  tomorrow date: %02d/%02d/%04d", d.day, d.month, d.year);
}

3 个答案:

答案 0 :(得分:1)

包含所有条件的一种简单方法是使用标准库 SELECT employee.employee_ID, employee.LastName, employee.FirstName, employee.Department, employee.HomePhone, employee.CellPhone, SUM(CASE WHEN ot_tracking.shiftDate BETWEEN '2020-12-01' AND '2020-12-31' AND ot_tracking.operatorResponse = 'Yes' THEN ot_tracking.hours else 0 END) AS OT_Accepted, SUM(CASE WHEN ot_tracking.shiftDate BETWEEN '2020-12-01' AND '2020-12-31' AND ot_tracking.operatorResponse = 'No' THEN ot_tracking.hours else 0 END) AS OT_Declind, SUM(CASE WHEN ot_tracking.shiftDate BETWEEN '2020-12-01' AND '2020-12-31' AND ot_tracking.operatorResponse = 'Not Available - working or off' THEN ot_tracking.hours else 0 END) AS 'OT Unable to work', SUM(CASE WHEN ot_tracking.shiftDate BETWEEN '202-12-01' AND '2020-12-31' AND ot_tracking.operatorResponse = 'Yes' OR ot_tracking.operatorResponse = 'No' THEN ot_tracking.hours else 0 END) AS 'OT Offered', employee.Senority FROM site INNER JOIN employee ON site.siteID = employee.siteID LEFT OUTER JOIN ot_tracking ON ot_tracking.employee_ID = employee.employee_ID WHERE employee.siteID = 1 AND employee.Department <> 'shop' AND Department = 'Log Yard' AND LogLoader = 'Yes' AND Active = 'Yes' GROUP BY ot_tracking.operator, employee.Senority, employee.Department ORDER BY employee.Department, `OT Offered`, employee.Senority 。它定义了一个结构来保存日期,<time.h>(所以你不需要创建自己的)并且有许多有用的函数,例如 struct tm,它可以调整日期来处理一切(改变月份、年份变化、闰年等)。 下面是一个例子:

mktime

这里我使用了 #include <stdio.h> #include <conio.h> #include <time.h> int main(void) { struct tm d; printf("Please enter the date DD/MM/YYYY format and I will raise it up\n"); scanf_s("%d/%d/%d", &d.tm_mday, &d.tm_mon, &d.tm_year); d.tm_mon -= 1; // struct tm saves months from 0 to 11 d.tm_year -= 1900; // struct tm requires years passed since 1900 printf("You entered: %02d/%02d/%04d\n", d.tm_mday, d.tm_mon + 1, d.tm_year + 1900); d.tm_mday++; mktime(&d); // Make the necessary adjustments after having modified the day printf("Tomorrow date: %02d/%02d/%04d\n", d.tm_mday, d.tm_mon + 1, d.tm_year + 1900); } ,所以我不得不重新添加我之前减去的值,但是 printf 实际上包含了在标准 (<time.h>, {{ 1}}) 和自定义格式 (asctime)。网上有很多资源(包括 SO!)可以深入了解它提供的所有内容,看看吧!

旁注:您不使用 ctime 中的任何内容,不应包含不必要的库

答案 1 :(得分:0)

回答你最初的问题:“我做错了什么?”

if (d.day == 29 && d.month == 2)

应该

if (d.day == 28 && d.month == 2)

同样地,32 应该是 31

要回答您作为答案发布的问题:“有没有一种简单的方法可以包含一年中可能存在的所有“天”条件?”

const int dInM[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

if( d.day == dInM[d.month-1] ) {
    
    ++d.month;
    d.day = 1;
}

答案 2 :(得分:0)

#include <stdio.h>

typedef struct date
{
    int day, month,year;
}date;

int main()
{
  date d;
  printf("\nPlease enter the date DD/MM/YYYY format and i will raise it up \n");
  scanf("%d%d%d",&d.day, &d.month,&d.year);
  printf("you enter: %02d/%02d/%04d", d.day, d.month, d.year); //Keep in mind that the %02d means two characters minimum width,The 0 means to pad the field using zeros and the 2 means that the field is two characters wide
  d.day++;

  switch(d.month)
  {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
      {
           if(d.day>=32)
           {
                 d.month++;
                 d.day=d.day-31;
           }
      }break;
      case 4:
      case 6:
      case 9:
      case 11:
      {
           if(d.day>=31)
           {
                 d.month++;
                 d.day=d.day-30;
           }break;
      }
      case 2:
      {
             if ((d.year%400==0)||(d.year%4==0&&d.year%100!=0)&&d.day>=30)
             {
                   d.month++;
                   d.day=d.day-29;
             }
             else
             {
                   d.month++;
                   d.day=d.day-28;
             }
      }break;

  }

   printf("\ntomorrow date: %02d/%02d/%04d\n", d.day, d.month, d.year);
}
相关问题