给出年、月、日,计算该日是该年的第几天

给出年、月、日,计算该日是该年的第几天。

解:程序:

#include<iostream>

using namespace std;


int main()

{

int sum_day(int , int);

int leap(int year);

int year,month,day,days;

cout <<"input date(year,month,day):";

cin>>year>>month>>day;

cout << year<<"/"<<month<<"/"<<day;

days = sum_day(month, day);

if (leap(year) && month >= 3)

{

days += 1;

}

cout <<" is the " <<days<< "th day in this year." <<endl;

system("pause");

return 0;

}


int sum_day(int month, int day)

{

int i;

int day_tab[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

for (i = 0; i < month - 1; i++)

//输入的时候已经知道所求最后一个月有多少天,故i < month - 1

{

day += day_tab[i];

}

return (day);

}


int leap(int year)//判断是否为闰年

{

int leap;

leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;

return(leap);

}

结果:

input date(year,month,day):2016 3 24

2016/3/24 is the 84th day in this year.

请按任意键继续. . .


本文出自 “岩枭” 博客,请务必保留此出处http://yaoyaolx.blog.51cto.com/10732111/1754973

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值