服务器之家

服务器之家 > 正文

C++实现万年历源代码

时间:2021-08-26 13:48     来源/作者:jxccy888

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

?
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
#include<time.h>
ofstream fout("日历.txt");
 
void Printtitle(int n);
int OrEndl(int n);
string Choose();    //选择功能
int Calculate(int mon,int day);
void Printday(int a);
void FirstDay(int wday);
void SomeYear(int p);
void ThisMonth();     //打印当月日历的主函数
void PrintFirstNum(int mon);
void Printyear(int year);
void ThisYear(int p);   //打印当年日历的主函数
void Month(int n);
void Printnum(int q,int mon);
int Firstwday[12];     //储存每个月1号的星期数
 struct tm *local;
string ch;
 int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //储存每月的天数
int main(void)
 long t;
  time(&t);
  local=localtime(&t);
  local->tm_year=local->tm_year+1900;
  local->tm_mon++;
  cout<<"今天是:"<<local->tm_year<<"年"<<local->tm_mon
    <<"月"<<local->tm_mday<<"日,";
  fout<<"今天是:"<<local->tm_year<<"年"<<local->tm_mon
    <<"月"<<local->tm_mday<<"日,";
  Month(local->tm_wday);
  cout<<endl;
  fout<<endl;
  cout<<"当前时间是:"<<local->tm_hour<<"时"<<local->tm_min<<"分"<<local->tm_sec
    <<"秒"<<local->tm_wday<<endl;
  fout<<"当前时间是:"<<local->tm_hour<<"时"<<local->tm_min<<"分"<<local->tm_sec
    <<"秒"<<local->tm_wday<<endl;
  string flag;
 int sum;
 if(((local->tm_year%4==0)&&(local->tm_year%100!=0))||(local->tm_year%400==0))
 a[1]=29;
 sum=Calculate(local->tm_mon,local->tm_mday);
 int p=sum-(local->tm_wday+1)-(sum/7)*7;
  do{
     flag=Choose();
     if(flag=="1")    //根据选择的数字确定调用哪个函数
      ThisMonth();
     else if(flag=="2")
      ThisYear(p);
     else if(flag=="3")
      SomeYear(p);
     else if(flag=="4")
      break;
  else
  {
  cout<<"输入错误"<<endl;
    fout<<"输入错误"<<endl;
  continue;
  }
 }while(1);
  return 0;
}
 
string Choose()   
 cout<<"请选择"<<"1、当月日历"<<endl<<"   2、当年日历"<<endl
 <<"   3、万年历"<<endl<<"   4、退出"<<endl;
 fout<<"请选择"<<"1、当月日历"<<endl<<"   2、当年日历"<<endl
 <<"   3、万年历"<<endl<<"   4、退出"<<endl;
 cin>>ch;
 fout<<ch;
 cout<<endl;
 fout<<endl;
 return ch;
 }
 
void ThisMonth() 
 int m=local->tm_mon%12;
 Printtitle(m);
 int p=local->tm_mday-(local->tm_wday+1)-(local->tm_mday/7)*7;
 Printnum(p,local->tm_mon);
}
 
void ThisYear(int p) 
 FirstDay(p);
 Printyear(local->tm_year);
 for(int a=1;a<12;a=a+2)
 
 Printtitle(a);
 PrintFirstNum(a);
 }
}
 
void SomeYear(int p)  //打印万年历的主函数
 int m;
 cout<<"Please enter a year number"<<endl;
 fout<<"Please enter a year number"<<endl;
 while(1) 
 
 
  scanf("%d",&m); 
  if(  m<0  )
  
  printf("\nInput error,Please enter a year number again:\n"); 
  fflush(stdin);    //没加这句话会死循环,加了就ok 
  
  else  break
 }
 fout<<m;
 cout<<endl;
  fout<<endl;
 Printyear(m);
  int n=m;
  if(n<local->tm_year)   //计算所输年份的1月1日星期几
  
  for(;n<local->tm_year;n++)
  {  
   if(((n%4==0)&&(n%100!=0))||(n%400==0))
   p=p+2;
    else
   p++;
  if(p>=7)
   p=p-7;
  }
 }
  else
  {
  for(;n>local->tm_year;n--)
  {
   if(((n%4==0)&&(n%100!=0))||(n%400==0))
   p=p-2;
   else
   p--;
  if(p<0)
   p=p+7;
  }
  }
FirstDay(p);
 for(int h=1;h<12;h=h+2)
 {
 Printtitle(h);
  if(((m%4==0)&&(m%100!=0))||(m%400==0))
  a[1]=29;
  else
  a[1]=28;
 PrintFirstNum(h);
 }
}
 
void Printtitle(int n)  //打印标题
{
 do{
   cout<<"     ";
   fout<<"     ";
 char str[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
 for(int h=0;h<10;h++)
 {
 cout<<str[n-1][h];
 fout<<str[n-1][h];
 }
 cout<<"     ";
   fout<<"     ";
 if(OrEndl(n))
 break;
n++;
 }while(!(n%2));
 do{
  cout<<"____________________________";
   fout<<"____________________________";
if(OrEndl(n))
 break;
n++;
}while(!(n%2));
  do{
cout<<" Sun Mon Tue Wed Thu Fri Sat ";
fout<<" Sun Mon Tue Wed Thu Fri Sat ";
if(OrEndl(n))
 break;
n++;
 }while(!(n%2));
}
 
 
int Calculate(int mon,int day)  //计算当天到当年1月1日的天数
{
 int sum=day;
for(mon--;mon!=0;mon--)
 sum=sum+a[mon-1];
 return sum;
}
void FirstDay(int wday)   //推算每个月1号的星期数
{
 if(wday<=0)
 wday=wday+7;
 Firstwday[0]=7-wday;
 for(int n=0;n<11;n++)
 {
 Firstwday[n+1]= Firstwday[n]+a[n]%7;
 if(Firstwday[n+1]>6)
      Firstwday[n+1]=Firstwday[n+1]-7;
 }
}
 
int OrEndl(int n)
{
   if(ch=="1")    //如果是打出当月日历就直接跳出循环
 cout<<endl;
 fout<<endl;
 return 1;
}
 if(n%2)        //判断单月输空格,双月回车
 {
 cout<<"    ";
 fout<<"    ";
 }
else
 cout<<endl;
 fout<<endl;
}
return 0;
}
 
void Printyear(int year) //打印年份
 int m=year/1000;
 int n=(year/100)%10;
 int p=(year/10)%10;
 int q=year%10;
 int num[4]={m,n,p,q};
 char str[5][10][7]={"***** ","  * ","***** ","***** ","*  * ","***** ","***** ","***** ","***** ","***** ",
         "*  * ","  * ","  * ","  * ","*  * ","*   ","*   ","  * ","*  * ","*  * ",
            "*  * ","  * ","***** ","***** ","***** ","***** ","***** ","  * ","***** ","***** ",
            "*  * ","  * ","*   ","  * ","  * ","  * ","*  * ","  * ","*  * ","  * ",
            "***** ","  * ","***** ","***** ","  * ","***** ","***** ","  * ","***** ","***** ",};
 for(int g=0;g<5;g++)
 
 cout<<"          ";
 fout<<"          ";
 for(int i=0;i<4;i++)
   for(int h=0;h<7;h++)
 
  cout<<str[g][num[i]][h];
  fout<<str[g][num[i]][h];
 }
  cout<<endl;
  fout<<endl;
 }
}
 
void PrintFirstNum(int mon)  //打印每两个月的日历
{
 int mday[2];       //储存每两个月当前打印的日期
 do{
 int k=0;
   for(;k<Firstwday[mon-1];k++)
 {   cout<<"  ";
  fout<<"  ";
 }
   k++;
 for(int d=1;k<8;d++,k++)      //输入每个月的第一行
 {
 cout<<" "<<d<<" ";
 fout<<" "<<d<<" ";
 }
 if(mon%2)             //判断单月输空格,双月回车
 {
  cout<<"    ";
  fout<<"    ";
  mday[mon%2-1]=d;
 }
 else
 {
  cout<<endl;
    fout<<endl;
    mday[mon%2+1]=d-1;
 }
 mon++;
 }while(!(mon%2));
 mon=mon-2;
  int i=0,k=1,m=mon-1;
  for(;mday[i]<a[m]+1;mday[i]++,k++)    
  {
  if(mday[i]<10)
 {
   cout<<" "<<mday[i]<<" ";
 fout<<" "<<mday[i]<<" ";
  }
  else
 {
 cout<<" "<<mday[i]<<" ";
 fout<<" "<<mday[i]<<" ";
 }
  if(k==7)
  {
 if(!i)
   {
  cout<<"    ";
  fout<<"    ";
   i=1;
   m++;
 }
   else
   {
  cout<<endl;
  fout<<endl;
   i=0;
   m--;
 }
   k=0;
 }
  }
  m=mon-1;
  if(mday[0]==a[m]+1&&mday[1]<a[m+1]+1)    //当双月未结束,单月输入结束跳出时最后一行的输出情况
  {
  for(;k<8;k++)
  {
   cout<<"  ";
  fout<<"  ";
  }
  cout<<"    ";
 fout<<"    ";
   k=1;
  for(mday[1]++;mday[1]<a[m+1]+1;mday[1]++,k++)
   {
 cout<<" "<<mday[1]<<" ";
  fout<<" "<<mday[1]<<" ";
   if(k==7)
  {
  cout<<endl;
   fout<<endl;
    cout<<"                  ";
   fout<<"                  ";
  }
   }
   cout<<endl;
 fout<<endl;
  }
  if(mday[0]<a[m]+1&&mday[1]==a[m+1]+1)  //当单月未结束,双月输入结束跳出时最后一行的输出情况
  {
  cout<<endl;
 fout<<endl;
   k=1;
  for(mday[0]++;mday[0]<a[m]+1;mday[0]++,k++)
 {
 cout<<" "<<mday[0]<<" ";
  fout<<" "<<mday[0]<<" ";
   if(k==7)
  {
  cout<<endl;
   fout<<endl;
    cout<<"                  ";
   fout<<"                  ";
  }
   }
   cout<<endl;
 fout<<endl;
  }
}
 
void Month(int n)
{
 char str[7][7]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
 for(int h=0;h<7;h++)
 {
 cout<<str[n][h];
 fout<<str[n][h];
 }
}
 
void Printnum(int q,int mon)  //打印当月日历
 if(q<0)
    q=q+7;
int k=0;
 if(q!=7&&q)
 {
 for(;k<7-q;k++)
 {
  cout<<"  ";
 fout<<"  ";
 }
 }
  k++;
  for(int d=1;d<a[mon-1]+1;d++,k++)
 {
 cout<<setw(4)<<d;
   if(k==7)
 {
  cout<<endl;
  fout<<endl;
  k=0;
 }
 }
  cout<<endl;
  fout<<endl;
}

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

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部