编程新手!!!想请教一下关于怎样编程日期合法性的问题!!!

WeiGuoSJ 2017-08-11 03:48:30
编程新手现在在计蒜客上学习C语言编程,遇到了一个瓶颈问题一直找不出错误在哪里希望各位看看(已经看了2天了。。。)!如果可以的话能否帮忙解释一下if语句中的镶嵌于else if的用途!!!非常感谢!!!!!


#include <stdio.h>
int main(){
int year,month,day;
scanf("%d-%d-%d",&year,&month,&day);

if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<=31)
{printf("YES");}

else
{printf("NO");}

else if((month==4||month==6||month==9||month==11)&&day<=30)
{printf("YES");}

else
{printf("NO");}

else if(month==2&&((year%100!=0&&year%4==0)||(year%400==0))&&day<=29)
{printf("YES");}

else
{printf("NO");}

return 0;
}
...全文
443 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhagoodwell 2017-09-03
  • 打赏
  • 举报
回复
# include <stdio.h>  
int gainint(int *p,int a,int b);  
int main(){  
int month[]={31,28,31,30,31,30,31,31,30,31,30,31},i;  
int charge[6]={1,1,1,2016,12,0};  
char zifu[][5]={"年份","月份","天数"};  
int a[3];  
for(i=0;i<3;i++)  
{  
printf("请输入%s(%d--%d):\n",zifu[i],charge[i],charge[i+3]);  
gainint(&a[i],charge[i],charge[i+3]);  
if(i==0)  
month[1]=!(a[0]%4&&a[0]%100)||!(a[0]%400)?29:28;  
if(i==1)  
charge[5]=month[a[1]-1];  
}  
printf("%04d年%02d月%02d日",a[0],a[1],a[2]);  
getchar();  
return 0;  
}  
int gainint(int *p,int a,int b)//a为最小值b为最大值  
{  
int c[2]={0,1};  
do{  
scanf("%d",p);  
while(getchar()!='\n');    
if(*p>b||*p<a)  
printf("输入有误,请重新输入(%d--%d):",a,b);  
}while(*p>b||*p<a);  
return *p;//返回数字的位数  
}  
zhagoodwell 2017-09-03
  • 打赏
  • 举报
回复
//我相信我的代码
# include <stdio.h>  
int gainint(int *p,int a,int b);  
int main(){  
int month[]={31,28,31,30,31,30,31,31,30,31,30,31},i;  
int charge[6]={1,1,1,2016,12,0};  
char zifu[][5]={"年份","月份","天数"};  
int a[3];  
for(i=0;i<3;i++)  
{  
printf("请输入%s(%d--%d):\n",zifu[i],charge[i],charge[i+3]);  
gainint(&a[i],charge[i],charge[i+3]);  
if(i==0)  
month[1]=!(a[0]%4&&a[0]%100)||!(a[0]%400)?29:28;  
if(i==1)  
charge[5]=month[a[1]-1];  
}  
printf("%04d年%02d月%02d日",a[0],a[1],a[2]);  
getchar();  
return 0;  
}  
int gainint(int *p,int a,int b)//a为最小值b为最大值  
{  
int c[2]={0,1};  
do{  
scanf("%d",p);  
while(getchar()!='\n');    
if(*p>b||*p<a)  
printf("输入有误,请重新输入(%d--%d):",a,b);  
}while(*p>b||*p<a);  
    while(*p/c[1])  
{  
c[1]*=10;  
c[0]++;  
}  
return c[0];//返回数字的位数  
}  
Bob_Wikkings 2017-08-27
  • 打赏
  • 举报
回复
选择语句就简单来说有双边选择语句和多边选择语句,楼主的代码混在一起,应该参考1楼的代码,清晰又不会出错。
ooolinux 2017-08-11
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
COleDateTime::COleDateTime COleDateTime( ); COleDateTime( const COleDateTime& dateSrc ); COleDateTime( const VARIANT& varSrc ); COleDateTime( DATE dtSrc ); COleDateTime( time_t timeSrc ); COleDateTime( const SYSTEMTIME& systimeSrc ); COleDateTime( const FILETIME& filetimeSrc ); COleDateTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec ); COleDateTime( WORD wDosDate, WORD wDosTime ); Parameters dateSrc An existing COleDateTime object to be copied into the new COleDateTime object. varSrc An existing VARIANT data structure (possibly a COleVariant object) to be converted to a date/time value (VT_DATE) and copied into the new COleDateTime object. dtSrc A date/time (DATE) value to be copied into the new COleDateTime object. timeSrc A time_t value to be converted to a date/time value and copied into the new COleDateTime object. systimeSrc A SYSTEMTIME structure to be converted to a date/time value and copied into the new COleDateTime object. filetimeSrc A FILETIME structure to be converted to a date/time value and copied into the new COleDateTime object. nYear, nMonth, nDay, nHour, nMin, nSec Indicate the date and time values to be copied into the new COleDateTime object. wDosDate, wDosTime MS-DOS date and time values to be converted to a date/time value and copied into the new COleDateTime object. Remarks All of these constructors create new COleDateTime objects initialized to the specified value. The following table shows valid ranges for each date and time component: Date/Time Component Valid Range year 100 – 9999 month 0 – 12 day 0 – 31 hour 0 – 23 minute 0 – 59 second 0 – 59 Note that the actual upper bound for the day component varies based on the month and year components. For details, see the SetDate or SetDateTime member functions. Following is a brief description of each constructor: COleDateTime( ) Constructs a COleDateTime object initialized to 0 (midnight, 30 December 1899). COleDateTime( dateSrc ) Constructs a COleDateTime object from an existing COleDateTime object. COleDateTime( varSrc ) Constructs a COleDateTime object. Attempts to convert aVARIANT structure or COleVariant object to a date/time (VT_DATE) value. If this conversion is successful, the converted value is copied into the new COleDateTime object. If it is not, the value of the COleDateTime object is set to 0 (midnight, 30 December 1899) and its status to invalid. COleDateTime( dtSrc ) Constructs a COleDateTime object from a DATE value. COleDateTime( timeSrc ) Constructs a COleDateTime object from a time_t value. COleDateTime( systimeSrc ) Constructs a COleDateTime object from a SYSTEMTIME value. COleDateTime( filetimeSrc ) Constructs a COleDateTime object from a FILETIME value. COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec ) Constructs a COleDateTime object from the specified numerical values. COleDateTime( wDosDate, wDosTime ) Constructs a COleDateTime object from the specified MS-DOS date and time values. For more information, see theVARIANT entry in the Win32 SDK OLE Programmer's Reference. For more information on the time_t data type, see thetime function in the Run-Time Library Reference. For more information, see theSYSTEMTIME andFILETIME structures in the Win32 SDK documentation. For more information on MS-DOS date and time values, seeDosDateTimeToVariantTime in the Win32 SDK documentation. For more information about the bounds for COleDateTime values, see the articleDate and Time: Automation Support in Visual C++ Programmer’s Guide. COleDateTime Overview | Class Members | Hierarchy Chart See Also COleDateTime::SetDate, COleDateTime::SetDateTime, COleDateTime::SetTime, COleDateTime::GetStatus, COleDateTime::operator =, COleDateTime::m_dt, COleDateTime::m_status
老赵神回复,草木皆兵了
赵4老师 2017-08-11
  • 打赏
  • 举报
回复
COleDateTime::COleDateTime COleDateTime( ); COleDateTime( const COleDateTime& dateSrc ); COleDateTime( const VARIANT& varSrc ); COleDateTime( DATE dtSrc ); COleDateTime( time_t timeSrc ); COleDateTime( const SYSTEMTIME& systimeSrc ); COleDateTime( const FILETIME& filetimeSrc ); COleDateTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec ); COleDateTime( WORD wDosDate, WORD wDosTime ); Parameters dateSrc An existing COleDateTime object to be copied into the new COleDateTime object. varSrc An existing VARIANT data structure (possibly a COleVariant object) to be converted to a date/time value (VT_DATE) and copied into the new COleDateTime object. dtSrc A date/time (DATE) value to be copied into the new COleDateTime object. timeSrc A time_t value to be converted to a date/time value and copied into the new COleDateTime object. systimeSrc A SYSTEMTIME structure to be converted to a date/time value and copied into the new COleDateTime object. filetimeSrc A FILETIME structure to be converted to a date/time value and copied into the new COleDateTime object. nYear, nMonth, nDay, nHour, nMin, nSec Indicate the date and time values to be copied into the new COleDateTime object. wDosDate, wDosTime MS-DOS date and time values to be converted to a date/time value and copied into the new COleDateTime object. Remarks All of these constructors create new COleDateTime objects initialized to the specified value. The following table shows valid ranges for each date and time component: Date/Time Component Valid Range year 100 – 9999 month 0 – 12 day 0 – 31 hour 0 – 23 minute 0 – 59 second 0 – 59 Note that the actual upper bound for the day component varies based on the month and year components. For details, see the SetDate or SetDateTime member functions. Following is a brief description of each constructor: COleDateTime( ) Constructs a COleDateTime object initialized to 0 (midnight, 30 December 1899). COleDateTime( dateSrc ) Constructs a COleDateTime object from an existing COleDateTime object. COleDateTime( varSrc ) Constructs a COleDateTime object. Attempts to convert aVARIANT structure or COleVariant object to a date/time (VT_DATE) value. If this conversion is successful, the converted value is copied into the new COleDateTime object. If it is not, the value of the COleDateTime object is set to 0 (midnight, 30 December 1899) and its status to invalid. COleDateTime( dtSrc ) Constructs a COleDateTime object from a DATE value. COleDateTime( timeSrc ) Constructs a COleDateTime object from a time_t value. COleDateTime( systimeSrc ) Constructs a COleDateTime object from a SYSTEMTIME value. COleDateTime( filetimeSrc ) Constructs a COleDateTime object from a FILETIME value. COleDateTime( nYear, nMonth, nDay, nHour, nMin, nSec ) Constructs a COleDateTime object from the specified numerical values. COleDateTime( wDosDate, wDosTime ) Constructs a COleDateTime object from the specified MS-DOS date and time values. For more information, see theVARIANT entry in the Win32 SDK OLE Programmer's Reference. For more information on the time_t data type, see thetime function in the Run-Time Library Reference. For more information, see theSYSTEMTIME andFILETIME structures in the Win32 SDK documentation. For more information on MS-DOS date and time values, seeDosDateTimeToVariantTime in the Win32 SDK documentation. For more information about the bounds for COleDateTime values, see the articleDate and Time: Automation Support in Visual C++ Programmer’s Guide. COleDateTime Overview | Class Members | Hierarchy Chart See Also COleDateTime::SetDate, COleDateTime::SetDateTime, COleDateTime::SetTime, COleDateTime::GetStatus, COleDateTime::operator =, COleDateTime::m_dt, COleDateTime::m_status
赵4老师 2017-08-11
  • 打赏
  • 举报
回复
COleDateTime::GetStatus DateTimeStatus GetStatus( ) const; Return Value Returns the status of this COleDateTime value. Remarks Call this member function to get the status (validity) of a given COleDateTime object. The return value is defined by the DateTimeStatus enumerated type, which is defined within the COleDateTime class. enum DateTimeStatus{ valid = 0, invalid = 1, null = 2, }; For a brief description of these status values, see the following list: COleDateTime::valid Indicates that this COleDateTime object is valid. COleDateTime::invalid Indicates that this COleDateTime object is invalid; that is, its value may be incorrect. COleDateTime::null Indicates that this COleDateTime object is null, that is, that no value has been supplied for this object. (This is “null” in the database sense of “having no value,” as opposed to the C++ NULL.) The status of a COleDateTime object is invalid in the following cases: If its value is set from a VARIANT or COleVariant value that could not be converted to a date/time value. If its value is set from a time_t, SYSTEMTIME, or FILETIME value that could not be converted to a valid date/time value. If its value is set by SetDateTime with invalid parameter values. If this object has experienced an overflow or underflow during an arithmetic assignment operation, namely, += or -=. If an invalid value was assigned to this object. If the status of this object was explicitly set to invalid using SetStatus. For more information about the operations that may set the status to invalid, see the following member functions: COleDateTime SetDateTime operator +, - operator +=, -= For more information about the bounds for COleDateTime values, see the articleDate and Time: Automation Support in Visual C++ Programmer’s Guide. COleDateTime Overview | Class Members | Hierarchy Chart See Also COleDateTime::SetStatus, COleDateTime::m_status
自信男孩 2017-08-11
  • 打赏
  • 举报
回复
case 2少一个break,建议加上。
自信男孩 2017-08-11
  • 打赏
  • 举报
回复
#include <stdio.h>

int main()
{
    int year,month,day;
    scanf("%d-%d-%d",&year,&month,&day);
#if 1
    if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<=31) {
        printf("YES");
    } else if((month==4||month==6||month==9||month==11)&&day<=30) {
        printf("YES");
    } else if(month==2&&((year%100!=0&&year%4==0)||(year%400==0))&&day<=29) {
        printf("YES");
    } else {
        printf("NO");
    }
#else
    switch (month) {
        case 1: case 3:
        case 5: case 7:
        case 8: case 10:
        case 12:
            if (day <= 31) {
                printf("YES\n");
            } else {
                printf("NO\n");
            }
            break;

        case 4: case 6:
        case 9: case 11:
            if (day <= 30)
                printf("YES\n");
            else
                printf("NO\n");
            break;

        case 2:
            if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
                if (day <= 29)
                    printf("YES\n");
                else
                    printf("NO\n");
            } else if (day <= 28) {
                printf("YES\n");
            } else {
                  printf("NO\n");
            }

        default:
            printf("Not support!\n");
            break;

    }
#endif
    return 0;
}
参考一下上面的代码, else总是和上面和它最近的if匹配。 若if else超过3个,建议改用switch case代替。
hekylej 2017-08-11
  • 打赏
  • 举报
回复
#include <stdio.h>

int main()
{
	int year,month,day;
	scanf("%d-%d-%d",&year,&month,&day);
	if((month==1||month==3||month==5||month==7||month==8||
				month==10||month==12)&&day<=31){
		printf("YES\n");
	}
	else if((month==4||month==6||month==9||month==11)&&day<=30){
		printf("YES\n");
	}
        else if(month==2&&((year%100!=0&&year%4==0)||(year%400==0))&&day<=29){
		printf("YES\n");
	}
	else{
		printf("NO\n");
	}
	return 0;									    
}
这样就好了,相当于else里面镶嵌了else if,对判断过程的细化,注意判断结果的唯一性

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧