概述
头文件:1 Data class.h
#ifndef I_DATE_ED
#define I_DATE_ED
#include <iostream>
#include <string>
using namespace std;
// year应当是1800到2200之间的整数;
// month必须是1到12之间的整数;
// day必须是1到给定、月的天数之间的整数;
class Date
{
int year, month, day;
public:
Date() {month = 1; day = 1; year = 1800;}
Date(int, int, int);
bool isvalid();
// 判断Date是否合法
void readinto(); // 用读入的month、day和year设置这个Date的值
Date next();
// 返回Date之后的Date
Date previous(); // 返回Date之前的Date
string dayofweek(); // 返回这个Date是星期几
friend ostream& operator<<(ostream&, Date&);
};
Date::Date(int monthln, int dayln, int yearln)
{
month = monthln;
day = dayln;
year = yearln;
}
bool Date::isvalid()
{
if (year < 1800 || year > 2200)
return false;
else
switch(month){
case 1: case 3: case 5: case 7:
case 8: case 10: case 12:
if (day < 1 || day > 31) return false;
case 4: case 6: case 9: case 11:
if (day < 1 || day > 30) return false;
case 2:
if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
if (day < 1 || day >29) return false;
else ;
else if (day < 1 || day > 28)
return false;
default:
return true;
}
}
void Date::readinto()
{
cout << "Please input "month day, year":n";
cin >> month >> day >> year;
}
Date Date::next()
{
switch(month){
case 1: case 3: case 5: case 7:
case 8: case 10:
if (day != 31) {day++; break;}
else {day = 1; month++; break;}
case 4: case 6: case 9: case 11:
if (day != 30) {day++; break;}
else {day = 1; month++; break;}
case 2:
if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
if (day != 29) {day++; break;}
else {day = 1; month++; break;}
else if (day != 28) {day++; break;}
else {day = 1; month++; break;}
case 12:
if (day != 31) {day++; break;}
else if (year <= 2200 && year >= 1800)
{day = 1; month = 1; year++; break;}
else
cout << "Out of the Date!";
}
return *this;
}
Date Date::previous()
{
switch(month){
case 2: case 4: case 6: case 8:
case 9: case 11:
if (day != 1) {day--; break;}
else {day = 31; month--; break;}
case 5: case 7: case 10: case 12:
if (day != 1) {day--; break;}
else {day = 30; month--; break;}
case 3:
if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
if (day != 1) {day--; break;}
else {day = 29; month--; break;}
else if (day != 1) {day--; break;}
else {day = 28; month--; break;}
case 1:
if (day != 1) {day--; break;}
else if (year <= 2200 && year >= 1800)
{day = 31; month = 12; year--; break;}
else
cout << "Out of the Date!";
}
return *this;
}
// 算法公式: Week=(Day + 2*Month + 3*(Month+1)/5 + Year + Year/4 - Year/100 + Year/400) % 7
//
(其中的Year是4位数的,如2009。“%”号是等式除7取余数)
// 该式对应的与蔡勒公式有点区别:“0”为星期1,……,“6”为星期日!
// 某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算
string Date::dayofweek()
{
int myweek;
if (month == 1 || month == 2){
year--; month += 12;
}
myweek = (day + 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400) % 7;
if (month == 13 || month == 14){
year++; month -= 12;
}
switch(myweek){
case 0: return "Monday";
case 1: return "Tuesday";
case 2: return "Wednesday";
case 3: return "Thursday";
case 4: return "Friday";
case 5: return "Saturday";
case 6: return "Sunday";
}
}
ostream& operator<<(ostream& out, Date& ate)
{
out << ate.month << ' ' << ate.day << ", "<< ate.year;
return out;
}
#endif
驱动程序:data.cpp
#include "1 Date class.h"
int main()
{
Date dat1;
Date dat2(2, 28, 1900);
// 不是闰年
Date dat3(12, 31, 2008);
if (dat1.isvalid())
cout << "dat1 = " << dat1 << endl;
else
return 0;
if (dat2.isvalid())
cout << "dat2 = " << dat2 << endl;
else
return 0;
cout << "dat3 = " << dat3 << endl << endl;
dat1.readinto();
cout << "dat1 = " << dat1 << endl << endl;
cout << "dat1.next() = " << dat1.next() << endl
<< "dat2.next() = " << dat2.next() << endl
<< "dat3.next() = " << dat3.next() << endl;
cout << "dat1.previous() = " << dat1.previous() << endl
<< "dat2.previous() = " << dat2.previous() << endl
<< "dat3.previous() = " << dat3.previous() << endl << endl;
cout << dat1 << " " << dat1.dayofweek() << endl
<< dat2 << " " << dat2.dayofweek() << endl
<< dat3 << " " << dat3.dayofweek() << endl;
return 0;
}
这个类简单,所以把Date类的实现也放在头文件了,修改类实现时,vs检测不到头文件中的改动,必须改变.cpp的内容,才会重新编译……
只创建string对象可以 不包含头文件<string>, 但这样不能执行cout << string;的操作;
最后
以上就是英俊冬天为你收集整理的创建一个Date类的全部内容,希望文章能够帮你解决创建一个Date类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复