2013-02-19 82 views
0

我被問到正確的程序,檢查用戶輸入的日期是否合法或不在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!"); 

} 

我得到正確的輸出,如果一個月有31天,但在幾個月的休息,輸出是合法的,如果這一天是小於32你的幫助表示讚賞!

+0

'leap'未初始化。啓用警告! – 2013-02-19 07:22:52

回答

2

你不能鏈條件語句是這樣的:

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

相反,你將不得不專門測試場景:

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

你的版本簡單的口服補液鹽第一次測試的結果( m == 1)的值爲3,它在C中是一個非零值,因此總是布爾值爲真。

+0

謝謝。有用! – Shail 2013-02-19 06:13:17

1

這個測試肯定是錯誤的:

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

這必須是

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

執行邏輯或非零表達將始終爲true。因此,你的整個測試將永遠是真實的。

+0

感謝您的幫助! – Shail 2013-02-19 06:14:14

5

我重寫你的程序一樣簡單和容易的,我覺得這可能有助於

//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!"); 
} 
+0

使用數組!謝謝! – Shail 2013-02-19 06:12:01

+0

如果您想檢查所有錯誤,請記住您可以在輸入中獲得零和負值! – 2013-02-19 07:24:34

0

您可以檢查日期合法性簡單:

#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; 
} 
+0

我剛開始學習,所以我不是很瞭解這個部分,但感謝您的幫助! – Shail 2013-02-19 06:10:52

+0

它基於標準的時間轉換。 'mktime()'從'struct tm *'生成'time_t'。如果'time_t'具有不正確的數據,那麼'localtime()'將返回'struct tm *',這些字段與原始的'struct tm *'('time_')不同。 – 2013-02-19 06:35:31

-1
/*c program to find whether given date is correct or not*/ 
#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h> 
int date_valid(int, int, int); 
void main() 
{ 
int day, month, year,i; 
printf("enter a date in dd/mm/yyyy format:\n"); 
scanf("%d/%d/%d", &day, &month, &year); 
i = date_valid(day, month, year); 
if (i == 1) 
    printf("date is valid"); 
else 
    printf("date is invalid"); 
} 
int date_valid(int day, int month, int year) 
{ 
if ((year > 0) && (month > 0 && month <= 12)) 
{ 
    if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) 
    { 
     if (day > 0 && day <= 30) 
      return 1; 
     else 
      return 0; 
    } 
    else if (month == 2) 
    { 
     if (year % 4 == 0) 
     { 
      if (day > 0 && day <= 29) 
       return 1; 
      else 
       return 0; 
     } 
     else 
     { 
      if (day > 0 && day <= 28) 
       return 1; 
      else 
       return 0; 
     } 
    } 
    else 
    { 
     if (day > 0 && day <= 31) 
      return 1; 
     else 
      return 0; 
    } 
} 
else 
    return 0; 

} 
+0

請解釋! – 2015-11-20 08:13:55

+0

添加更多的細節和代碼可能會有所幫助。 – 2015-11-20 08:26:58

0
//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; 
}