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

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

服务器之家 - 编程语言 - C/C++ - C++实现万年历源代码

C++实现万年历源代码

2021-08-26 13:48jxccy888 C/C++

这篇文章主要介绍了C++实现万年历源代码,可以直接在VC6.0编译运行,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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;
}

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

延伸 · 阅读

精彩推荐
  • C/C++详解c语言中的 strcpy和strncpy字符串函数使用

    详解c语言中的 strcpy和strncpy字符串函数使用

    strcpy 和strcnpy函数是字符串复制函数。接下来通过本文给大家介绍c语言中的strcpy和strncpy字符串函数使用,感兴趣的朋友跟随小编要求看看吧...

    spring-go5642021-07-02
  • C/C++C语言实现电脑关机程序

    C语言实现电脑关机程序

    这篇文章主要为大家详细介绍了C语言实现电脑关机程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    xiaocaidayong8482021-08-20
  • C/C++C语言中炫酷的文件操作实例详解

    C语言中炫酷的文件操作实例详解

    内存中的数据都是暂时的,当程序结束时,它们都将丢失,为了永久性的保存大量的数据,C语言提供了对文件的操作,这篇文章主要给大家介绍了关于C语言中文件...

    针眼_6702022-01-24
  • C/C++学习C++编程的必备软件

    学习C++编程的必备软件

    本文给大家分享的是作者在学习使用C++进行编程的时候所用到的一些常用的软件,这里推荐给大家...

    谢恩铭10102021-05-08
  • C/C++深入理解goto语句的替代实现方式分析

    深入理解goto语句的替代实现方式分析

    本篇文章是对goto语句的替代实现方式进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网7342020-12-03
  • C/C++C/C++经典实例之模拟计算器示例代码

    C/C++经典实例之模拟计算器示例代码

    最近在看到的一个需求,本以为比较简单,但花了不少时间,所以下面这篇文章主要给大家介绍了关于C/C++经典实例之模拟计算器的相关资料,文中通过示...

    jia150610152021-06-07
  • C/C++C++之重载 重定义与重写用法详解

    C++之重载 重定义与重写用法详解

    这篇文章主要介绍了C++之重载 重定义与重写用法详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    青山的青6062022-01-04
  • C/C++c++ 单线程实现同时监听多个端口

    c++ 单线程实现同时监听多个端口

    这篇文章主要介绍了c++ 单线程实现同时监听多个端口的方法,帮助大家更好的理解和学习使用c++,感兴趣的朋友可以了解下...

    源之缘11542021-10-27