我是靠谱客的博主 英俊冬天,这篇文章主要介绍创建一个Date类,现在分享给大家,希望可以做个参考。

复制代码
1
头文件:1 Data class.h
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部