第十二周项目1-当年第几天

  1. /* 
  2.  * Copyright (c) 2013, 烟台大学计算机学院 
  3.  * All rights reserved. 
  4.  * 作    者:王英华 
  5.  * 完成日期:2013 年 11 月 14 日 
  6.  * 版 本 号:v1.0 
  7.  * 问题描述:输入年月日,输出 这一天为这一年的第几天。 
  8.  * 问题分析:定义一个函数用来判断为第几天,在main函数中输入年月日并调用函数求值,最后在main函数中输出结果。 
  9.                         要先判读该年份是否为闰年,然后根据月份算这是第几天。 
  10.  * 输入:2012 12 31 
  11.  * 输出:2012年12月31日为该年的第366天。
  12. #include <iostream>
    
    using namespace std;
    
    int time (int,int,int);
    
    int main()
    {
        int year,month,day,t;
        cout<<"请输入年份:"<<endl;
        cin>>year>>month>>day;
        t=time(year,month,day);
        cout<<month<<"月"<<day<<"日是该年第"<<t<<"天"<<endl;
        return 0;
    }
    int time(int year,int month,int day)
    {
        int t;
        if(year%4==0&&year%100!=0||year%400==0)
        {
        switch(month)
        {
            case 1:t=day;break;
            case 2:t=day+31;break;
            case 3:t=day+31+29;break;
            case 4:t=day+31+29+31;break;
            case 5:t=day+31+29+31+30;break;
            case 6:t=day+31+29+31+30+31;break;
            case 7:t=day+31+29+31+30+31+30;break;
            case 8:t=day+31+29+31+30+31+30+31;break;
            case 9:t=day+31+29+31+30+31+30+31+31;break;
            case 10:t=day+31+29+31+30+31+30+31+31+30;break;
            case 11:t=day+31+29+31+30+31+30+31+31+30+31;break;
            case 12:t=day+31+29+31+30+31+30+31+31+30+31+30;break;
        }
        return t;
        }
        else
        {
           switch(month)
        {
            case 1:t=day;break;
            case 2:t=day+31;break;
            case 3:t=day+31+28;break;
            case 4:t=day+31+28+31;break;
            case 5:t=day+31+28+31+30;break;
            case 6:t=day+31+28+31+30+31;break;
            case 7:t=day+31+28+31+30+31+30;break;
            case 8:t=day+31+28+31+30+31+30+31;break;
            case 9:t=day+31+28+31+30+31+30+31+31;break;
            case 10:t=day+31+28+31+30+31+30+31+31+30;break;
            case 11:t=day+31+28+31+30+31+30+31+31+30+31;break;
            case 12:t=day+31+28+31+30+31+30+31+31+30+31+30;break;
        }
        return t;
        }
    
    } 
    

    运行结果:第十二周项目1-当年第几天_第1张图片

你可能感兴趣的:(第十二周项目1-当年第几天)