ArexM 2020-11-19 07:03:36
能不能帮我看看这个怎么出错了
#include<stdio.h>
int isLeap(int year) //判断是否为闰年
{
if(year%100==0||year%4==0)
return 1;
else
return 0;
}

int daysInMonth(int year,int month) //判断某月天数
{
int i;
i=isLeap(year);
if(i==1)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("31天\n");break;
case 2:printf("29天\n");break;
case 4:
case 6:
case 9:
case 11:printf("30天\n");break;
}
}
if(i==0)
{
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("31天\n");break;
case 2:printf("28天\n");break;
case 4:
case 6:
case 9:
case 11:printf("30天\n");break;
}
}
}

int isLegal(int year,int month,int day) //判断日期是否合法
{
int i,j=1,k;
i=isLeap(year);
k=daysInMonth(year,month);
if((month<=0||month>=13)||(day<=0||day>31))
j=0;
else
{
if(i==1)
{
if(month<1||month>12)
j=0;
if(month==4||month==6||month==9||month==11)
{
if(day>30||day<=0)
j=0;
}
if(month==2)
{
if(day<=0||day>29)
j=0;
}
}
if(i==0)
{
if(month<1||month>12)
j=0;
if(month==4||month==6||month==9||month==11)
{
if(day<=0||day>31)
j=0;
}
if(month==2)
{
if(day<=0||day>28)
j=0;
}
}
}
return j;
}

int Num(int year,int month,int day)
{
int i,sum;
i=isLeap(year);
switch (month)
{
case 1:sum = 0;break;
case 2:sum = 31;break;
case 3:sum = 59;break;
case 4:sum = 90;break;
case 5:sum = 120;break;
case 6:sum = 151;break;
case 7:sum = 181;break;
case 8:sum = 212;break;
case 9:sum = 243;break;
case 10:sum = 273;break;
case 11:sum = 304;break;
case 12:sum = 334;break;
}
sum=sum+day;
if(i==1&&month>2)
{
sum++;
}
return sum;
}

int main()
{
int year,month,day;
int a,b,c;
a=isLeap(year);
b=isLegal(year,month,day);
if (b==0)
{
do
{
printf("请输入正确格式的日期\n");
scanf("%d-%d-%d",&year,&month,&day);
} while(b==1);
}
c=Num(year,month,day);
printf("该日期是该年的第%d天",c);
printf("%d",b);
}
...全文
58 点赞 收藏 1
写回复
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qzjhjxj 2020-11-20
闰年判断有问题,写的太复杂了,依照你的修改如下,供参考:
#include <stdio.h>
int s[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
int f[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

bool isLeap(int year) //判断是否为闰年
{
   if((year%4==0 && year%100!=0)||(year%400==0))
       return 1;
   else
       return 0;
}


int  isLegal(int year,int month,int day) //判断日期是否合法
{
  int j=0;
  if(month>0 && month<=12 && day>0 && day<=31)
    {
       if(isLeap(year))
          {
              if(day <= s[month-1])
                 {   j=1; }
          }
       else
          {
              if(day <= f[month-1])
                 {   j=1; }
          }
    }
   return j;
}

int Num(int year, int month, int day) //统计天数
{
    int d = day;
    if(isLeap(year))
     {
        for(int i=0;i<month-1;i++)
           d+=s[i];
     }
    else
     {
        for(int i=0;i<month-1;i++)
           d+=f[i];
     }
    return d;
}


int main(int argc, char* argv[])
{
    int year,month,day;

    do
     {
       printf("请输入正确格式的日期(yyyy-mm-dd):");
       scanf("%4d-%2d-%2d",&year,&month,&day);
     }while(!isLegal(year,month,day));

   printf("该日期是该年的第%d天\n",Num(year,month,day));
   system("pause");
   return 0;
}
回复
相关推荐
发帖
其它技术问题
创建于2007-09-28

3845

社区成员

C/C++ 其它技术问题
申请成为版主
帖子事件
创建了帖子
2020-11-19 07:03
社区公告
暂无公告