年月日(c实现)

//模块化程序设计的思想
//设计一个年月日的程序,实现输入年月日,输出是这一年的第几天。
//首先考虑的是否为闰年,然后考虑这是哪一月的哪一天。
//最后考虑是总共的天数
#include"pch.h"
#include<stdio.h>
#include<iostream>
using namespace std;
/*bool Is_LeapYear(int year)//判断是否为闰年,用bool类型。返回True还是False
{
    if ((year % 4 == 0 & year % 100 == 0) ||( year % 400 == 0))
        return true;
    else
        return false;
}
*/
bool Is_LeapYear(int year)//另一种写法
{
    return ((year % 4 == 0 & year % 100 == 0) || (year % 400 == 0));
}
// 31 28 31 30 31 30 31 31 30 31 30 31 
// 1   2  3  4  5  6  7  8  9 10 11 12 
/*int GetMon_Day(int year, int mon)//希望返回这一年这一月有多少天
{
    int day = 0;
    switch (mon)
    {
    case 1:case 3:case 5:case 7:case 8:case 10:case 12:
        day = 31; break;
    case 4:case 6:case 9:case 11:
        day = 30; break;
    default:
        if (Is_LeapYear(year))
            day = 29;
        else
            day = 28;
    }
    return day;
}*/
int GetMon_Day(int year, int mon)//另一种写法:查表法
{
    int arr[13] = { 29,31,30,31,30,31,30,31,31,30,31,30,31 };
    if (mon == 2 && Is_LeapYear(year))
    {
         mon = 0;
    }
    return arr[mon];    
}
int GetYear_Mon_Day_Total(int year, int mon, int day)
{
    int m = 1;
    int total = day;
    for (; m < mon; m++)
    {
        total = total + GetMon_Day(year, m);
    }
    return total;
}

int main()
{
    int year, mon, day;
    int total = 0;
    cin>>year>>mon>>day;
    total = GetYear_Mon_Day_Total(year, mon, day);
    printf("%d年 %d月 %d日 => 总共%d", year, mon, day, total);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值