计算天数(C++)_学习 简单

//计算两个日期之间的天数(C++)
//历法规定,四年一闰,四百年闰,例如2000年是闰年,2100年不闰年,
//公历年份是整百数的,必须是400的倍数的才是闰年,不是400的倍数的就是平年
//计算天数(C++)

#include <iostream>
#include <cmath>
using namespace std;
//判断是否为闰年
bool isLeap(int n)
{
	//400的倍数 或者四的倍数是  并且 不是100的倍数,也就是必须不为0
    if(n%400==0 || n%4==0 && n%100 !=0)
		return true;
	else
		return false;
}

int _count(int y, int m, int d)
{
    int c = 0;
	//循环月份
	for(int i=1; i<=m-1; i++){
		if(i==4 || i==6 || i==9 || i==11)
			c+=30;
		else if(i==2)
			c+= isLeap(y) ? 29 : 28; //闰年的二月29天,平年二月28天
		else
			c+=31;
	}
	c+=d;
	return c;
}
int main()
{
	int y=2011,m=3,d=22;
	cout<<_count(2011,3,22);
    system("pause");
	return 0;
}

  

原文地址:https://www.cnblogs.com/xiangxiaodong/p/2350221.html