Iareu 2016-11-29 11:53 采纳率: 100%
浏览 1010
已采纳

这是一道简短函数题:输入年月日要求输出这是该年第几天?我写的中间代码为什么不执行,求大手指点迷津。

#include
#include
void main()
{int calculatedDays(int year,int month,int day);
int year,month,day;
scanf("%4d,%4d,%4d\n",&year,&month,&day);
calculatedDays(year,month,day);
system("pause");
}

int calculatedDays(int year,int month,int day)
{int leap,totaldays;
if((year%4==0&&year%100!=0)||(year%400==0))
leap=1;
else
leap=0;
while(leap==1)
{if(month==1)
totaldays=day;
else if(month==2)
totaldays=31+day;
else if(month==3)
totaldays=31+29+day;
else if(month==4)
totaldays=31+29+31+day;
else if(month==5)
totaldays=31+29+31+30+day;
else if(month==6)
totaldays=31+29+31+30+31+day;
else if(month==7)
totaldays=31+29+31+30+31+30+day;
else if(month==8)
totaldays=31+29+31+30+31+30+31+day;
else if(month==9)
totaldays=31+29+31+30+31+30+31+31+day;
else if(month==10)
totaldays=31+29+31+30+31+30+31+31+30+day;
else if(month==11)
totaldays=31+29+31+30+31+30+31+31+30+31+day;
else
totaldays=31+29+31+30+31+30+31+31+30+31+30+day;
break;
}
while(leap==0)
{if(month==1)
totaldays=day;
else if(month==2)
totaldays=31+day;
else if(month==3)
totaldays=31+28+day;
else if(month==4)
totaldays=31+28+31+day;
else if(month==5)
totaldays=31+28+31+30+day;
else if(month==6)
totaldays=31+28+31+30+31+day;
else if(month==7)
totaldays=31+28+31+30+31+30+day;
else if(month==8)
totaldays=31+28+31+30+31+30+31+day;
else if(month==9)
totaldays=31+28+31+30+31+30+31+31+day;
else if(month==10)
totaldays=31+28+31+30+31+30+31+31+30+day;
else if(month==11)
totaldays=31+28+31+30+31+30+31+31+30+31+day;
else
totaldays=31+28+31+30+31+30+31+31+30+31+30+day;
break;
}
return(printf("该日是该年的第:%4d天\n",totaldays));
}

  • 写回答

7条回答

  • AlbertS 博客专家认证 2016-11-29 12:50
    关注
     #include <stdio.h>
    #include <stdlib.h>
    
    int calculatedDays(int year,int month,int day);
    void main()
    {
        int year,month,day;
        scanf("%d %d %d",&year,&month,&day);
        calculatedDays(year,month,day);
        system("pause");
    }
    
    int calculatedDays(int year,int month,int day)
    {
        int leap,totaldays;
        if((year%4==0&&year%100!=0)||(year%400==0))
            leap=1;
        else
            leap=0;
    
        if(leap==1)
        {
            if(month==1)
                totaldays=day;
            else if(month==2)
                totaldays=31+day;
            else if(month==3)
                totaldays=31+29+day;
            else if(month==4)
                totaldays=31+29+31+day;
            else if(month==5)
                totaldays=31+29+31+30+day;
            else if(month==6)
                totaldays=31+29+31+30+31+day;
            else if(month==7)
                totaldays=31+29+31+30+31+30+day;
            else if(month==8)
                totaldays=31+29+31+30+31+30+31+day;
            else if(month==9)
                totaldays=31+29+31+30+31+30+31+31+day;
            else if(month==10)
                totaldays=31+29+31+30+31+30+31+31+30+day;
            else if(month==11)
                totaldays=31+29+31+30+31+30+31+31+30+31+day;
            else
                totaldays=31+29+31+30+31+30+31+31+30+31+30+day;
        }
        else
        {
            if(month==1)
                totaldays=day;
            else if(month==2)
                totaldays=31+day;
            else if(month==3)
                totaldays=31+28+day;
            else if(month==4)
                totaldays=31+28+31+day;
            else if(month==5)
                totaldays=31+28+31+30+day;
            else if(month==6)
                totaldays=31+28+31+30+31+day;
            else if(month==7)
                totaldays=31+28+31+30+31+30+day;
            else if(month==8)
                totaldays=31+28+31+30+31+30+31+day;
            else if(month==9)
                totaldays=31+28+31+30+31+30+31+31+day;
            else if(month==10)
                totaldays=31+28+31+30+31+30+31+31+30+day;
            else if(month==11)
                totaldays=31+28+31+30+31+30+31+31+30+31+day;
            else
                totaldays=31+28+31+30+31+30+31+31+30+31+30+day;
        }
        return(printf("该日是该年的第:%4d天\n",totaldays));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型