C/C++学习:判断某日是当年第几天

问题

学期末两周实训,老师布置若干题目。其中,第一题为:判断某日是当年第几天

问题描述:用结构体表示日期,输入一个日期(年、月、日),计算从输入这年的1月1日到输入的日期的总天数days并输出。

解题

1. 设计结构体

结构体中包含三个成员,分别为整型的年、月、日。

 struct date{
    int year;//年
    int month;//月
    int day;//日
};
2.日期输入设计

这里按照常规日期格式:年用四位数字表示,月、日用两位数字表示。故使用语句”%4d%2d%2d”来截取相应数量的数字赋值给结构体成员。

scanf("%4d%2d%2d",&data.year, &data.month, &data.day);
3.天数计算

从输入的日期可知(如20141222),12月只过了22日,不足一个月。所以,我们可以先对1-11月的天数进行求和。 注:下述代码中数组m记录平年每个月的天数,方便起见从1号下标开始使用。

int total_day = 0//初始化变量
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//除当月之外的月份天数加和,直至一月 
for(i = data.month-1;i >= 1;i--)
    total_day += m[i]; 

1-11月的天数求和完毕后,就可将12月的天数加到总天数中,该步骤可与在1-11月求和前进行。

//当月天数加和 
total_day += data.day;

上述操作都是以平年的天数进行求和的。若输入的年份为闰年且求解的日期在3月或以上,则需要补多一天(即2月29日)。注:闰年判断见第四点。

if( (data.year % 4 == 0 && data.year % 100 != 0) || (data.year % 400 == 0) ){
    if(data.month > 2){
        total_day += 1;                 
    }
}
4.闰年判断

闰年的条件有两个,满足任意一个即为闰年。
-1.年份能被4整除,且不能被100整除
-2.年份能被400整除

if((data.year<1 or data.year>9999) && (data.month<1 or data.month>12)&& (data.day<1 or data.day>12)){
    //该年为闰年时的执行语句
}
5.合法性检查
 //输入合法性检查 
if(data.year<1 && data.year>9999 && data.month<1 && data.month>12 && data.day<1 && data.day>12){
    //不合法时的执行语句
}
else{
    //合法时的执行语句
}
附完整代码:
/*========================================
Module Name:判断某日是当年第几天 
Module Date:20141222
Module Auth:CLyoko
Description:用结构体表示日期,输入一个日期(年、月、日),计算从输入这年的1月1日到输入的日期的总天数days并输出。

Other:
    Revision History:
    Date        Rel Ver.    Notes
    20141222    1.0         创建程序 

==========================================*/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct date{
    int year;
    int month;
    int day;
}data; 

int main(int argc, char *argv[])
{
    int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int total_day = 0;
    int i=0;
    while(1)
    {
        system("cls");
        //初始化总天数以开始新的计算 
        total_day = 0;  
        cout<<"    计算总天数\n";
        cout<<"请输入日期(例20140523):"; 
        //按格式获取年月日信息 
        scanf("%4d%2d%2d",&data.year, &data.month, &data.day);
        //输入合法性检查 
        if((data.year<1 or data.year>9999) && (data.month<1 or data.month>12)&& (data.day<1 or data.day>12)){
            cout<<"输入不合法\n";
            system("pause"); 
        }
        else{
            //除当月之外的月份天数加和,直至一月 
            for(i = data.month-1;i >= 1;i--)
                total_day += m[i]; 
            //当月天数加和 
            total_day += data.day;
            //当年若为闰年则总天数+1 
            if( (data.year % 4 == 0 && data.year % 100 != 0) || (data.year % 400 == 0) ){
                if(data.month > 2){
                    total_day += 1;                 
                }
            }
            cout<<"该日期到这年1月1日的总天数为:"<<total_day<<endl;
            system("pause");                
        }
    }
    return 0;
}

转载请保留作者信息。
作者: CLyoko
文章网址: http://blog.csdn.net/clyoko/article/details/43908647

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值