本文实例为大家分享了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
|
#include <stdio.h> #include<stdlib.h> int year, month; int run[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; int ping[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; char week[7][10] = { "SUN" , "MON" , "TUE" , "WED" , "THU" , "FRI" , "SAT" }; bool IsLeap( int year) { if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) //判断闰年 { return true ; } else { return false ; } } void PrintStar() { printf ( "\t\t\t万年历\t\t\t\n" ); printf ( "****************************************************\n" ); for ( int i = 0; i < 7; i++) { printf ( "%s\t" , week[i]); } printf ( "\n" ); } int DaySum( int year, int month) { int sum = 0; for ( int i = 1990; i < year; i++) { if (IsLeap(i)) { sum += 366; } else { sum += 365; } } for ( int i = 0; i < month - 1; i++) { if (IsLeap(year)) { sum += run[i]; } else { sum += ping[i]; } } return sum; } void PrintCal( int sum, int year, int month) { int nRes, nTmp; nRes = sum % 7+1; //前面空几格 nTmp = 7 - nRes; //从第几格开始打印 for ( int i = 0; i < nRes; i++) { printf ( "\t" ); } if (IsLeap(year)) { for ( int i = 1; i < run[month - 1]+1; i++) { printf ( "%d\t" , i); if (nTmp == i || (i - nTmp) % 7 == 0) { printf ( "\n" ); } } printf ( "\n" ); } else { for ( int i = 1; i < ping[month - 1]+1; i++) { printf ( "%d\t" , i); if (nTmp == i || (i - nTmp) % 7 == 0) { printf ( "\n" ); } } printf ( "\n" ); } } int main( void ) { while (1) { scanf_s( "%d %d" , &year, &month); PrintStar(); PrintCal(DaySum(year, month), year, month); printf ( "****************************************************\n" ); } return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/plokday/article/details/96367151