c++谭浩强(第四版),课后习题7-2,输入年月日判断是该年的第几天

这篇博客展示了如何使用C++编程语言,通过struct定义的Date结构,输入年月日,计算并输出该日期是一年中的第几天。代码中包含闰年判断和不同月份天数的处理,适用于基础编程学习和日期计算实践。

#include<iostream>

using namespace std;

struct Date

{

       int year ;

       int month ;

       int day ;

 } ;

 int main()

 {

      Date date ;

      int a, b, c, sum=0 ;

      cout << "year: " ;

      cin >> date.year ;

      cout << endl << "month: " ;

      cin >> date.month ;

      cout << endl << "day: " ;

      cin >> date.day ;

      cout<<endl ;

      a=date.year ;

      b=date.month ;

      c=date.day ;

      if(b<3)

             sum =(b-1)*31+c ;

      else{

             for(int i=3; i<=b; i++){

                    switch(i){

                           case 3: sum=59 ; break ;

                           case 4: sum+=31 ; break ;

                           case 5: sum+=30 ; break ;

                           case 6: sum+=31 ; break ;

                           case 7: sum+=30 ; break ;

                           case 8: sum+=31 ; break ;

                           case 9: sum+=31 ; break ;

                           case 10: sum+=30 ; break ;

                           case 11: sum+=31 ; break ;

                           case 12: sum+=30 ; break ;

                           default: break ;     

                     }

              }

              sum+=c ;

              if(a%400==0 || (a%4==0&&a%100!=0))

                     sum+=1 ;     

        }

        cout<<"it's the "<<sum<<"th day" ;

        return 0 ;

 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值