服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现万年历功能

C++实现万年历功能

2021-08-04 12:08pointer_y C/C++

这篇文章主要为大家详细介绍了C++实现万年历功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下

1.此万年历功能

1>日期加减天数

2>日期与日期之间的差值

3>输入年月显示当月日历

2.代码实现

?
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include<iostream>
#include<iomanip>
using namespace std;
 
class Date
{
public:
 Date(int year = 1990, int month = 1, int day = 1) //构造函数
 :_year(year), _month(month), _day(day)
 {
 if (JudgeRightDate())   //判断传入的值是否是合法的,不合法则置成1990年1月1日
 {
  _year = 1990;
  _month = 1;
  _day = 1;
 }
 }
 
 bool JudgeRightDate()  //判断值是否合法函数
 {
 if (_year < 1 || ((_month< 1)||_month>12) ||
  (_day<1)||_day>GetMonthDay(_year,_month))
 {
  return true;
 }
 else
 {
  return false;
 }
 }
 
 int JudgeYear(int year)  //判断是否是闰年的函数
 {
 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
 {
  return 1;
 }
 else
  return 0;
 }
 
 int GetMonthDay(int year, int month) //通过年和月得到对应的天数
 {
 int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 int days = arr[month];
 if (month == 2)
 {
  days += JudgeYear(year);  //如果是闰年的二月则天数加1
 }
 return days;
 }
 
 
 Date operator +(int days)  //日期加天数函数,重载“+”实现
 {
 _day += days;      //先将天数全部加到所给日期的“天”上
 GetRightDate(_year, _month, _day); //再通过计算得到正确的日期。
 return *this;
 }
 
 void GetRightDate(int &year, int &month, int &day)  //计算出正确的日期
 {
 if (day <= 0)
 {
  while (day <= 0)
  {
  month--;
  day += GetMonthDay(year, month);
  if (month < 1)
  {
   year--;
   month = 13;
  }
  }
 }
 else
 {
  while (day>GetMonthDay(year, month))
  {
  day -= GetMonthDay(year, month);
  month++;
  if (month > 12)
  {
   year++;
   month = 1;
  }
  }
 }
 }
 
 Date operator -(int days)  //重载“-”实现日期减天数
 {
 _day -= days;
 GetRightDate(_year, _month, _day);
 return *this;
 }
 
 bool operator >(const Date &d)  //判断两个日期的大小
 {
 if (_year > d._year)
 {
  return true;
 }
 else if (_year == d._year)
 {
  if (_month > d._month)
  {
  return true;
  }
  else if (_month==d._month)
  {
  if (_day > d._day)
  {
   return true;
  }
  }
 }
 return false;
 }
 
 bool operator ==(const Date &d)  //判断两个日期是否相等
 {
 if (_year == d._year && _month == d._month && _day == d._day)
 {
  return true;
 }
 else
  return false;
 }
 
 int operator -( Date &d) //计算日期差函数,重载“-”实现
 {
 int count = 0;
 Date tmp(*this);
 if (*this > d)
 {
  tmp = d;
  d = *this;
  *this = tmp;
 }
 while (!(*this==d))
 {
  count++;
  *this =*this+1;
 }
 return count;
 }
 
 
 void print()    //打印函数
 {
 cout << _year << "-" << _month << "-" << _day;
 }
 
 
 int week()  //求出日期对应的星期函数
 {
 int w = 0;
 int y = _year;
 int m = _month;
 if (m == 1 || m == 2)
 {
  m = _month + 12;
  y = _year - 1;
 }
 w = _day + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400;
 w = w % 7 + 1;
 return w;
 }
 
 void print_week()
 {
 cout << "星期日 星期一 星期二 星期三 星期四 星期五 星期六" << endl;
 }
 
 void print_day() //根据日期和星期,正确的输出日历
 {
 int line = 1;
 int days = GetMonthDay(_year,_day);
 int w = week();
 if (w != 7)
 {
  for (int blank = w - 1; blank; --blank, ++line)
  {
  cout << setw(7) << "";
  }
 }
 for (int d = 1; d <= days; ++d, ++line)
 {
  cout << setw(7) << d;
  if (line % 7 == 0)
  {
  cout << endl;
  }
 }
 cout << endl;
 }
 
private:
 int _year;
 int _month;
 int _day;
};
void menu()
{
 cout << setw(40) <<"万年历"<< endl;
 cout << "1.日期加减天数" << endl;
 cout << "2.日期减日期" << endl;
 cout << "3.输入年月显示当月日历" << endl;
}
void choice()
{
 int num = 0;
 int year, month, day, days;
 char ch = '+';
 cin >> num;
 if (num == 1)
 {
 cout << "请输入日期:" << endl;
 cin >> year >> month >> day;
 cout << "请输入天数:" << endl;
 cin >> days;
 cout << "请输入'+'或者'-':" << endl;
 cin >> ch;
 Date d1(year, month, day);
 Date d2;
 if (ch == '+')
 {
  d2 = d1 + days;
 }
 else if (ch == '-')
 {
  d2 = d1 - days;
 }
 else
 {
  cout << "无效的输入!" << endl;
 }
 cout << "计算后的日期为:";
 d2.print();
 cout << endl;
 }
 else if (num==2)
 {
 cout << "请输入日期:" << endl;
 cin >> year >> month >> day;
 Date d3(year, month, day);
 cout << "请输入日期:" << endl;
 cin >> year >> month >> day;
 Date d4(year, month, day);
 int ret = d3 - d4;
 cout << "期间相差:" << ret << endl;
 }
 else if (num == 3)
 {
 cout << "请输入年月:" << endl;
 cin >> year >> month;
 Date d5(year, month);
 d5.print_week();
 d5.print_day();
 }
}
int main()
{
 menu();
 choice();
 system("pause");
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/pointer_y/article/details/52244245

延伸 · 阅读

精彩推荐