C语言 用于检查输入的日期是否有效的 AC 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14950012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:23:48  来源:igfitidea点击:

A C program to check if the entered date is valid or not

cdatelogic

提问by Shail

I was asked to right a program which checks if the date entered by the user is legitimate or not in C. I tried writing it but I guess the logic isn't right.

我被要求修改一个程序,该程序检查用户输入的日期在 C 中是否合法。我尝试编写它,但我想逻辑不正确。

//Legitimate date
#include <stdio.h>
void main()
{
    int d,m,y,leap;
    int legit = 0;
    printf("Enter the date\n");
    scanf("%i.%i.%i",&d,&m,&y);
    if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
        {leap=1;}
    if (m<13)
    {
        if (m == 1 || (3 || ( 5 || ( 7 || ( 8 || ( 10 || ( 12 )))))))
            {if (d <=31)
                {legit=1;}}
        else if (m == 4 || ( 6 || ( 9 || ( 11 ) ) ) )
            {if (d <= 30)
                {legit = 1;}}
        else
            {
                        if (leap == 1)
                              {if (d <= 29)
                                    {legit = 1;}}
                        if (leap == 0)
                              {{if (d <= 28)
                                    legit = 1;}}
             }
    }
    if (legit==1)
        printf("It is a legitimate date!\n");
    else
        printf("It's not a legitimate date!");

}

I am getting the correct output if the month has 31 days but for the rest of the months, the output is legitimate if the day is less than 32. Your help is appreciated!

如果该月有 31 天,我将获得正确的输出,但对于其余月份,如果该天小于 32,则输出是合法的。感谢您的帮助!

回答by Riskhan

i rewrite you program as simple and easy, i think this may help

我重写你的程序简单易行,我认为这可能会有所帮助

//Legitimate date
#include <stdio.h>

void main()
{
   int d,m,y;
   int daysinmonth[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
   int legit = 0;

   printf("Enter the date\n");
   scanf("%i.%i.%i",&d,&m,&y);

   // leap year checking, if ok add 29 days to february
   if(y % 400 == 0 || (y % 100 != 0 && y % 4 == 0))
    daysinmonth[1]=29;

   // days in month checking
   if (m<13)
   {
      if( d <= daysinmonth[m-1] )
        legit=1;
   }

   if (legit==1)
      printf("It is a legitimate date!\n");
   else
      printf("It's not a legitimate date!");
}

回答by chrisaycock

You can't chain conditionals like this:

你不能像这样链接条件:

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

Instead, you'll have to test each scenario specially:

相反,您必须专门测试每个场景:

if (m == 1 || m == 3 || m == 5 || ...)

Your version simply ORs the results of the first test (m == 1) with the value of 3, which in C is a non-zero and therefore always a boolean true.

您的版本只是将第一个测试 ( m == 1)的结果与 的值进行3或运算,在 C 中该值是非零值,因此始终为布尔值 true。

回答by sstn

This test is certainly wrong:

这个测试肯定是错误的:

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

This must be

这必须是

if ((m == 1) || (m == 3) || (m == 5) || ... )

Performing a logical or with a non-zero expression will always evaluate to true. Therefore, your entire test will always be true.

执行逻辑或非零表达式将始终评估为真。因此,您的整个测试将始终为真。

回答by Eddy_Em

You can check date legitimacy simpler:

您可以更简单地检查日期合法性:

#define _XOPEN_SOURCE 600
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

time_t get_date(char *line){
#define WRONG() do{printf("Wrong date!\n"); return -1;}while(0)
    time_t date;
    struct tm time_, time_now, *gmt;
    time_.tm_sec = 0;
    time_.tm_hour = 0;
    time_.tm_min = 0;
    if(strchr(line, '.') && sscanf(line, "%d.%d.%d", &time_.tm_mday, &time_.tm_mon, &time_.tm_year) == 3){
        time_.tm_mon--; time_.tm_year += (time_.tm_year < 100) ? 100 : -1900;
    }else
        WRONG();
    memcpy(&time_now, &time_, sizeof(struct tm));
    date = mktime(&time_now);
    gmt = localtime(&date);
    if(time_.tm_mday != gmt->tm_mday) WRONG();
    if(time_.tm_mon != gmt->tm_mon) WRONG();
    if(time_.tm_year != gmt->tm_year) WRONG();
    date = mktime(&time_);
    return date;
#undef WRONG
}

int main(int argc, char** argv){
    struct tm *tmp;
    if(argc != 2) return 1;
    time_t GD = get_date(argv[1]);
    if(GD == -1) return -1;
    printf("Int date = %d\n", GD);
    printf("your date: %s\n", ctime(&GD));
    return 0;
}

回答by Rithik Singh

//reading date and checking if valid or not
//firstly we will check the yeear then the month and then the date
//
//
//
//
#include<stdio.h>
int main()
{
    int d,m,y;
    printf("ENTER THE DATE IN DD/MM/YYYY FORMAT:");
    scanf("%d%d%d",&d,&m,&y);
    //check year 
    if(y>0 && y<9999)
    {
        // check month
        if(m>=1 && m<=12)
        {
            if((d>=1 && d<=31) && (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12))
                printf("the date is valid in a month with 31 days:");
            else if ((d>=1 && d<=30) && (m==4 || m==6 || m==9 || m==11 ))
                printf("the date is valid in a feb with 30 days:");
            else if ((d>=1 && d<=29) && (m==2) &&  ((y%400==0) || (y%4==0) && (y%100!=0)))
                printf("the date is valid in feb of a leap year:");
            else if ((d>=1 && d<=28) && (m==2) && (y%4==0) && (y%100==0))
                printf("the date is valid in feb of a leap year:");
            else if ((d>=1 && d<=28) && (m==2) && (y%4!=0) )
                printf("the date is valid in feb of a non leap year:"); 
            else
                printf("the date is invalid:");     
        }
        else
        {
            printf("the month is not valid:");
        }
    }
    else 
    {
        printf("the date is not valid:");
    }
    return 0;
}