第十一周 项目5 求一年中第几天

本博客提供了一个C++程序,用于接收用户输入的年月日,并计算出该日期是该年的第几天。程序通过判断闰年来确定二月的天数,并使用switch-case结构来计算不同月份的总天数。
#include <iostream>
using namespace std;
int days(int y,int m,int d);
int main()
{
    int year,month,day;
    cout<<"输入年 月 日"<<endl;
    cin>>year>>month>>day;
    cout<<"这是该年的第"<<days(year,month,day)<<"天"<<endl;
    return 0;
}
int days(int y,int m,int d)
{   int n;
if(y%400==0)
    switch(m-1)
    {
        case 0:n=d;break;
        case 1:n=31+d;break;
        case 2:n=29+31+d;break;
        case 3:n=31+29+31+d;break;
        case 4:n=31+29+31+30;break;
        case 5:n=31+29+31+30+31+d;break;
        case 6:n=31+29+31+30+31+30+d;break;
        case 7:n=31+29+31+30+31+30+31+d;break;
        case 8:n=31+29+31+30+31+30+31+31+d;break;
        case 9:n=31+29+31+30+31+30+31+31+30+d;break;
        case 10:n=31+29+31+30+31+30+31+31+30+31+d;break;
        case 11:n=31+29+31+30+31+30+31+31+30+31+30+d;break;
    }
    else
    switch(m-1)
    {
        case 0:n=d; break;
        case 1: n=31+d;break;
        case 2: n=28+31+d;break;
        case 3: n=31+28+31+d;break;
        case 4:n=31+28+31+30+d;break;
        case 5:n=31+28+31+30+31+d;break;
        case 6:n=31+28+31+30+31+30+d;break;
        case 7:n=31+28+31+30+31+30+31+d;break;
        case 8:n=31+28+31+30+31+30+31+31+d;break;
        case 9:n=31+28+31+30+31+30+31+31+30+d;break;
        case 10:n=31+28+31+30+31+30+31+31+30+31+d;break;
        case 11:n=31+28+31+30+31+30+31+31+30+31+30+d;break;
    }
    return(n);
}
<img src="https://img-blog.youkuaiyun.com/20141108212117953?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmd5b25nMDAw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值