本文主要是介绍输入一个日期,判断这个日期在一年中是哪一天,是星期几,计算两个日期间的天数,使用字符串输出日期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
之前写了一个博文(http://blog.csdn.net/shiwazone/article/details/45053739)是用基本函数实现的,这次使用类的设计方法,也就是面向对象的方法改写一下,并加入了日期转换成字符串的实现。这里的程序也可以解决编程珠玑习题3.4的问题。
#include"calendar.h"
int main()
{Time t;t.initialTime();t.Show();t.StrShow();Time t1;t1.initialTime();t1.StrShow();Time t2;t2.initialTime();t2.StrShow();cout << "The days between t1 and t2 is: " << t1.countdays(t2)<<endl;cout << "The days between t2 and t1 is: " << t2.countdays(t1)<<endl;return 0;
}
#ifndef CANLENDAR_H
#define CANLENDAA_H
#include<iostream>
#include<string>
using namespace std;
string ntos(int t);//数字转换成字符
class Time
{
private:int year;int month;int day;unsigned int weekday;int Weekdaycount()const;//计算当日是星期几int Daycount()const;//计算当日是公元第多少天int Daysyearcount()const;//计算当日是该年的第多少天bool isleapyear()const;//判断该年是不是闰年bool check()const;//检查时间格式是否正确void numtostr(string &sy, string &sm, string &sd, string &sw)const;//数字日期转换成字符日期public:void initialTime();//输入初始化时间void Show()const;//显示时间信息void StrShow()const;//使用字符串的表达显示日期int countdays(const Time & t2)const;//计算两个日期间的天数
};
int Time::Weekdaycount()const//计算当日是星期几
{return Daycount() % 7;
}
int Time::Daycount()const//计算当日是公元第多少天
{int days = 0;days = (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100+ (year - 1) / 400 + Daysyearcount();return days;
}
int Time::Daysyearcount()const//计算当日是该年的第多少天
{int days = 0;int mtemp = month - 1;while (mtemp > 0){switch (mtemp){case(1) :case(3) :case(5) :case(7) :case(8) :case(10) :case(12) :days += 31; break;case(4) :case(6) :case(9) :case(11) :days += 30; break;case(2) :days += 28; break;default:break;}--mtemp;}if (isleapyear())++days;//如果是闰年,再加上一天return days + day;//返回计算的天数加上当月的天数
}
bool Time::isleapyear()const//判断该年是不是闰年
{if (year % 4 == 0 && year % 100 != 0) return true;//年是四的倍数而且不是100的倍数,是闰年if (year % 400 == 0)return true;else return false;
}
bool Time::check()const//检查时间格式是否正确
{if (year <= 0 || (month <= 0 || month>12) || day <= 0) return false;else{if (( month == 1 || month == 3 || month == 5 || month == 7|| month == 8 || month == 10 || month == 12) && day > 31)return false;else{if (( month == 4 || month == 6 || month == 9 || month == 11) && day > 30)return false;else{if ( month == 2) {if (isleapyear()) {if ( day > 29)return false; else return true;}else{if ( day > 28)return false; else return true;}}}}}
}
void Time::numtostr(string &sy, string &sm, string &sd, string &sw)const//数字日期转换成字符日期
{sy = ntos(year / 1000) + ntos(year / 100 - 10 * (year / 1000)) + ntos(year / 10 - 10 * (year / 100)) + ntos(year % 10);sd = ntos(day / 10) + ntos(day % 10);switch (month){case(1) : sm = "January"; break;case(2) : sm = "February"; break;case(3) : sm = "March"; break;case(4) : sm = "April"; break;case(5) : sm = "May"; break;case(6) : sm = "June"; break;case(7) : sm = "July"; break;case(8) : sm = "August"; break;case(9) : sm = "September"; break;case(10) : sm = "October"; break;case(11) : sm = "November"; break;case(12) : sm = "December"; break;default:break;}switch (weekday){case(1) : sw = "Monday"; break;case(2) : sw = "Tuesday"; break;case(3) : sw = "Wednesday"; break;case(4) : sw = "Thursday"; break;case(5) : sw = "Friday"; break;case(6) : sw = "Saturday"; break;case(7) : sw = "Sunday"; break;default:break;}
}void Time::initialTime()//输入初始化时间
{cout << "Enter the Time (year,month,day):\n";cin >> year;cin.get();cin >> month;cin.get();cin >> day;cin.get();if (!check()){ cout << "Try again:\n"; initialTime(); }elseweekday = Weekdaycount();
}
void Time::Show()const//显示时间信息
{cout << "Year: " << year << "\t";cout << "Month: " << month << "\t";cout << "Day: " << day << "\t";cout << "Weekday: " << weekday << endl;cout << "This is a ";if (isleapyear())cout << "leap"; else cout << "nonleap";cout << " year.\n";cout << "Today is the " << Daysyearcount() << " days of the year.\n";
}
void Time::StrShow()const//使用字符串的表达显示日期
{string st, sy, sm, sd, sw;numtostr( sy, sm, sd, sw);st = sy + " / " + sm + " / " + sd + " ," + "Today is " + sw + " .";string::iterator it = st.begin();for (; it != st.end(); ++it)cout << *it;cout << endl;cout << "This is a ";if (isleapyear())cout << "leap"; else cout << "nonleap";cout << " year.\n";cout << "Today is the " << Daysyearcount() << " days of the year.\n";
}
int Time::countdays(const Time & t2)const//计算两个日期间的天数
{int t = Daycount() - t2.Daycount();if (t < 0) return -t;return t;
}string ntos(int t)//数字转换成字符
{switch (t){case(0) : return "0";case(1) : return "1";case(2) : return "2";case(3) : return "3";case(4) : return "4";case(5) : return "5";case(6) : return "6";case(7) : return "7";case(8) : return "8";case(9) : return "9";default:break;}
}
#endif
如需转载,请注明出处。
这篇关于输入一个日期,判断这个日期在一年中是哪一天,是星期几,计算两个日期间的天数,使用字符串输出日期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!