好贷网好贷款

日期类

发布时间:2016-12-4 7:52:30 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"日期类",主要涉及到日期类方面的内容,对于日期类感兴趣的同学可以参考一下。

这是前段时间学习类时写的一个日期类,基本上一些基本的想要的算大都有了。#ifndef DATE_H #define DATE_H class Date { public: Date(int year=1970,int month=1,int day=1); virtual ~Date(); bool isLeapYear(const int); void show(); void go(); void input(); int weekDay(); int todayIsWhichDay(); int getYear()const{return year;} int getMonth()const{return month;} int getDay()const{return day;} int differenceDate(Date&); void printMonth(); void printWeekDay(); void printYear(); private: bool checkDate(); int year; int month; int day; }; 这是头文件   #include "Date.h" #include<iostream> #include<iomanip> #include<cstdlib> using namespace std; Date::Date(int year/*=1970*/,int month/*=1*/,int day/*=1*/): year(year),month(month),day(day) { if(!checkDate()){ cout<<"The date is illegal!"<<endl; exit(1); } //ctor } Date::~Date() { //dtor } bool Date::isLeapYear(const int year) { bool flag = false; if((year%4==0&&year%100!=0)||(year%400==0)) { flag = true; } return flag; } bool Date::checkDate() { bool flag = true; if(year<1970){ flag = false; } switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(day<1||day>31){ flag = false; } break; case 4: case 6: case 9: case 11: if(day<1||day>30){ flag = false; } break; case 2: if(isLeapYear(year)){ if(day>29||day<1){ flag = false; } }else{ if(day>28||day<1){ flag = false; } } break; default: flag = false; break; } return flag; } void Date::show() { cout<<year<<"-"<<setw(2)<<setfill('0')<< month<<"-"<<setw(2)<<setfill('0')<< day<<endl; } void Date::go() { ++day; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if(day>31){ day = 1; ++month; } break; case 4: case 6: case 9: case 11: if(day>30){ day = 1; ++month; } break; case 2: if(isLeapYear(year)){ if(day>29){ day = 1; ++month; } }else{ if(day>28){ day = 1; ++month; } } break; } if(month>12){ ++year; month=1; } } void Date::input() { cout<<"Please input year,month,day"<<endl; cin>>year>>month>>day; if(!checkDate()){ cout<<"The date is illegal!"<<endl; exit(1); } } int Date::weekDay() { int yearTemp = year; int countDay = 0; while((yearTemp--)>1) { if(isLeapYear(yearTemp)) { countDay+=366; }else{ countDay+=365; } } countDay+=todayIsWhichDay(); return countDay; } int Date::todayIsWhichDay() { int count = 0; int monthTemp = month; while(monthTemp--){ switch(monthTemp) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: count+=31; break; case 4: case 6: case 9: case 11: count+=30; break; case 2: if(isLeapYear(year)){ count+=29; }else{ count+=28; } break; } } count+=day; return count; } int Date::differenceDate(Date& date) { int countDay = 0; int maxYear = date.getYear(); int minYear = year; if(maxYear<=year) { maxYear = year; minYear = date.getYear(); } if(isLeapYear(minYear)) { if(minYear==date.getYear()){ countDay+=366-date.todayIsWhichDay(); }else{ countDay+=366-todayIsWhichDay(); } }else{ if(minYear==date.getYear()){ countDay+=365-date.todayIsWhichDay(); }else{ countDay+=365-todayIsWhichDay(); } } while(++minYear<maxYear) { if(isLeapYear(minYear)){ countDay+=366; }else{ countDay+=365; } } if(maxYear==date.getYear()){ countDay+=date.todayIsWhichDay(); }else{ countDay+=todayIsWhichDay(); } return countDay; } void Date::printMonth() { int count = weekDay()-day; int tempMonth = 0; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: tempMonth=31; break; case 4: case 6: case 9: case 11: tempMonth=30; break; case 2: if(isLeapYear(year)){ tempMonth=29; }else{ tempMonth=28; } break; } cout<<"The "<<year<<" year's "<<month<<" month map!"<<endl; cout<<"日\t"<<"一\t"<<"二\t"<<"三\t"<< "四\t"<<"五\t"<<"六"<<"\n"; int countDay = 0; int countDays = 0; int m = count+1; //cout<<m<<endl; int tempm = m%7; //cout<<temp<<endl; //cout<<tempm<<endl; for(int n = 0;n<tempm;++n) { cout<<"\t"; } for(int i = count+1;i<(tempMonth+count+1);++i) { countDay++; countDays++; while((i%7)>=0) { cout<<countDay<<"\t"; break; } if(i%7==6) { //cout<<(i%7)<<endl; cout<<"\n"; countDays = 0; } } cout<<"\n"; } void Date::printWeekDay() { switch(weekDay()%7) { case 1: cout<<"Today is Monday!"<<endl; break; case 2: cout<<"Today is Tuesday!"<<endl; break; case 3: cout<<"Today is Wednesday!"<<endl; break; case 4: cout<<"Today is Thursday!"<<endl; break; case 5: cout<<"Today is Friday!"<<endl; break; case 6: cout<<"Today is Saturday!"<<endl; break; case 7: cout<<"Today is Sunday!"<<endl; break; } } void Date::printYear() { int temp = month; for(int i = 1;i<=12;i++){ month = i; printMonth(); } month = temp; }   上面是头文件的实现代码。

上一篇:UIView+ViewController响应者链
下一篇:vi编辑器常用的命令及快捷键

相关文章

关键词: 日期类

相关评论