0012闰年计算的C++实现

来源:互联网 发布:围墙设计图纸含数据 编辑:程序博客网 时间:2024/05/01 21:18

闰年的计算规则:年份能被4整除,但是不能被100整除,或能被400整除。如2000能被400整除,是闰年;1992年能被4整除,不能被100整除,所以是闰年。

注意:博主一直被闰年和闰月两个概念打乱,这个计算是闰年,闰年,闰年(重要的话说三遍),与闰月无关。

先写一个头文件Date.h

#ifndef _DATE_H_#define _DATE_H_class Date{private:int day,month,year;public:void set(int,int,int);int getleap();    void print();int setyear(int);};#endif //_DATE_H_

实现文件:

#include"stdafx.h"#include"Date.h"#include"iostream"using namespace std;void Date::set(int x,int y,int z){    year=x;month=y;day=z;}int Date::setyear(int x) { year=x; return(year); }int Date::getleap(){    int k;k=(year%4==0&&year%100!=0)||(year%400==0);return(k);}void Date::print(){cout<<month<<":"<<day<<":"<<year<<endl;}

主函数文件:

#include"stdafx.h"#include"Date.h"#include"iostream"using namespace std;void main(){Date d;d.set(2000,1,1);d.print();int x;x=d.getleap();if(x)cout<<d.setyear(2000)<<"是闰年\n"<<endl;elsecout<<d.setyear(2000)<<"不是闰年\n"<<endl;}

                                                                                                                                                                              2015.8.13 西安

0 0