C语言题目:输入某年某月某日,判断这一天是这一年的第几天? 我哪里错了?

2024-11-15 12:07:45
推荐回答(5个)
回答1:

#include
void main()
{
int y,m,d;
int sum=0;
int flog;//标识是否是闰年
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&y,&m,&d);
if(( y%4==0 && y%100!=0 ) || y%400==0)
flog=1;
else
flog=0;
while((y<0)||(m>12||m<0)||(d<0||d>31)||((m%2==0)&&(d==31))||((flog==1)&&(m==2)&&(d>28)))
{
printf("input errro!\n");
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&y,&m,&d);
}
switch (m-1)
{
case 12: sum+=31;
case 11: sum+=30;
case 10: sum+=31;
case 9: sum+=30;
case 8: sum+=31;
case 7: sum+=31;
case 6: sum+=30;
case 5: sum+=31;
case 4: sum+=30;
case 3: sum+=31;
case 2:
if(flog==1) //这里改一下
sum+=28;
else
sum+=30;
case 1: sum+=31;break; //这里加上一个break语句
default:printf("非法月份\n");
break;
}
sum=sum+d;
if(flog==1)
printf("%d is leap year!\n",y);
printf("这一天是这年的第%d天",sum);
}
根据你的意思,修改了一下程序!你看看!

回答2:

  • #include
    void main()
    {
     int y,m,d;
     int sum=0;
     int flog;//标识是否是闰年
     int flog2=1;//标识是否是合法月份
     printf("请输入年月日(yyyy-mm-dd)\n");
     scanf("%4d-%2d-%2d",&y,&m,&d);
     switch (m-1)
    {
     case 11: sum+=30;//case12这句不要。因为swich是计算某月之前的月数有多少天,再加上该月的第几天
     case 10: sum+=31;
     case 9: sum+=30;
     case 8: sum+=31;
     case 7: sum+=31;
     case 6: sum+=30;
     case 5: sum+=31;
     case 4: sum+=30;
     case 3: sum+=31;
     case 2: sum+=29;
     case 1: sum+=31;

  • break;//这里要改
     default:
      {
      printf("非法月份\n");
            flog2=0;
      }
     break; 

     if(flog2==1)//这里改一下
     {
      sum=sum+d;
     if(( y%4==0 && y%100!=0 ) || y%400==0)
      flog=1;
     else
      flog=0;
     if (d>0&&d<=31)
     {
      if(flog==1)
      printf("这一天是这年的第%d天",sum-flog);
      if(flog==0)
      printf("这一天是这年的第%d天",sum);
     }
     else
      printf("非法日期\n");
     }
    }

回答3:

还有很多需要改进啊  

1.判非法日期不完善 距离 难道2013-4-31合法吗?

2.日期累加完全不是这样做的 ,switch语言机制你还没弄清楚啊

你可以试试无论你输入什么 都会输出非法月份

  我已经把你的代码改好了 你想不出来 我可以发给你

#include 
void main()
{
    int a[13]= {31,29,31,30,31,30,31,31,30,31,30,31};
    int y,m,d;
    int sum=0,i;
    int flag=0;//标识是否是闰年
    printf("请输入年月日(yyyy-mm-dd)\n");
    scanf("%4d-%2d-%2d",&y,&m,&d);
    if(m>=13||m<=0)
    {
        printf("非法月份\n");
        return 0;
    }
    if(( y%4==0 && y%100!=0 ) || y%400==0)
        flag=1;
    if(m==2)
    {
        if((flag==1&&(d<=0||d>=29))||(flag==0&&(d<=0||d>=28)))
        {
            printf("非法日期\n");
            return 0;
        }
    }
    else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
    {
        if(d<=0||d>=31)
        {
            printf("非法日期\n");
            return 0;
        }
    }
    else    if(d<=0||d>=31)
    {
        printf("非法日期\n");
        return 0;
    }
    for(i=1; i        sum+=a[i-1];
    sum+=d;
    if(m<3)
        flag=0;
        printf("这一天是这年的第%d天",sum-flag);
}

回答4:

要考虑的情况比较多,
#include
void main()
{
int y,m,d;
int sum=0;
int flog;//标识是否是闰年
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&y,&m,&d);
while((y<0)||(m>12||m<0)||(d<0||d>31)||((m==4||m==6||m==9||m==11)&&d>30)||(m==2&&d>29))
{
printf("input errro!\n");
printf("请输入年月日(yyyy-mm-dd)\n");
scanf("%4d-%2d-%2d",&y,&m,&d);
}
if(( y%4==0 && y%100!=0 ) || y%400==0)
flog=1;
else
flog=0;
switch (m-1)
{

case 11: sum+=30;
case 10: sum+=31;
case 9: sum+=30;
case 8: sum+=31;
case 7: sum+=31;
case 6: sum+=30;
case 5: sum+=31;
case 4: sum+=30;
case 3: sum+=31;
case 2:
if(flog==1)
sum+=29;
else
sum+=28;
case 1: sum+=31;break;
case 0: sum=0;break;
default:printf("非法月份\n");
break;
}
sum=sum+d;

printf("这一天是这年的第%d天\n",sum);
}

回答5:

#include
#include
void main()
{ int year,month,day,q,num,Jan, Feb, Mar, Apr, May,Jun,
Jul, Aug, Sept, Oct, Nov, Dec;
printf("input the day");
scanf("%d%d%d",&year,&month,&day);
if(year%4==0&&year%100!=0||year%400==0)
{q=1;}
else
{q=0;}
Jan=31;
Feb=28+q;
Mar=31;
Apr=30;
May=31;
Jun=30;
Jul=31;
Aug=31;
Sept=30;
Oct=31;
Nov=30;
Dec=31;
switch(month)
{
case 1 : printf("num=%d",day);break;
case 2:printf("num=%d",Jan+day);break;

case 3:printf("num=%d",Jan+Feb+day);break;

case 4:printf("num=%d",Jan+Feb+Mar+day);break;

case 5:printf("num=%d",Jan+Feb+Mar+Apr+day);break;

case 6 :printf("num=%d",Jan+Feb+Mar+Apr+May+day);break;

case 7 :printf("num=%d",Jan+Feb+Mar+Apr+May+Jun+day);break;
case 8 :printf("num=%d",Jan+Feb+Mar+Apr+May+Jun+Jul+day);break;

case 9 :printf("num=%d",Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+day);break;

case 10 :printf("num=%d",Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sept+day);break;

case 11 :printf("num=%d",Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sept+ Oct+day);break;

case 12 :printf("num=%d",Jan+Feb+Mar+Apr+May+Jun+Jul+Aug+Sept+ Oct+ Nov+day);break;
default :printf("error");
}
}