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

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

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

C语言实现万年历源码

2021-08-05 11:55不是培训笔记了 C/C++

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

本文实例为大家分享了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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int GetWeek(int year,int month,int day);//求今天是周几。周四就返回 4 。周日 返回 0;非法返回 -1;
int GetDaysInMonth(int year,int month);/*求指定月总共多少天*/
int CreateMonthData(int MonthDay[6][7],int year,int month);
void PrintMonth(int MonthDay[6][7]);
 
int main()
{
 int MDate[6][7] = {{0}};
 
 int y = 0;
 int m = 0;
 int ret = 0;
 
 printf("Plear input year month:\n");
 scanf("%d%d",&y,&m);
 
 if(m <= 0 || m > 12)
 {
 printf("Your month is invalid\n");
 return 1;
 }
 
 ret = CreateMonthData(MDate,y,m);
 if(ret == 0)
 {
 PrintMonth(MDate);
 }
 return 0;
}
 
int CreateMonthData(int MonthDay[6][7],int year,int month)
{
 int week = GetWeek(year,month,1);//返回第几周。
 int day = 1;
 int i = 0;
 int j = 0;
 int daysInMonth = GetDaysInMonth(year,month);//当月天数。
 
 if(week < 0)
 {
 printf("GetWeek Failed\n");
 return -1;
 }
 
 /*给第0行赋值*/
 for(j = 0;j < 7;j++)
 {
 if(j < week)
 {
 MonthDay[0][j] = 0;
 }
 else
 {
 MonthDay[0][j] = day;
 day++;
 }
 }
 
 /*给第1~5赋值*/
 for(i = 1;i < 6;i++)
 {
 for(j = 0;j < 7;j++)
 {
 if(day > daysInMonth)
 {
 MonthDay[i][j] = 0;
 }
 else
 {
 MonthDay[i][j] = day;
 day++;
 }
 }
 }
 
 return 0;
}
 
/*将二维数组中本月日期按如下形式显示:以2017年1月为例*/
/*
 日 一 二 三 四 五 六
 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 0 0 0 0
 0 0 0 0 0 0 0
*/
void PrintMonth(int MonthDay[6][7])
{
 int i=0;
 int j=0;
 printf(" 日 一 二 三 四 五 六\n");
 for(i=0;i<6;i++)
 {
 for(j=0;j<7;j++)
 {
 printf("%2d ",MonthDay[i][j]);
 }
 printf("\n");
 }
}

第二个文件源码

?
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
//润年
int LeapDays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
//非闰年
int CommonDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
 
//闰年返回 1 ,否则返回 0;
int IsLeapYear(int year)
{
 if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
 {
 return 1;
 }
 else
 {
 return 0;
 }
}
 
// 输入非法 返回 0;否则返回 1;
int IsValidDate(int year,int month,int day)
{
 int ret = 1;
 if(month < 1 || month > 12 || day < 1 || year <= 0)
 {
  return 0;
 }
 
 if(IsLeapYear(year))
 {
  if(day > LeapDays[month - 1])
  {
   ret = 0;
  }
 }
 else
 {
  if(day > CommonDays[month - 1])
  {
   ret = 0;
  }
 }
 
 return ret;
}
 
/*求这一天是当年的第多少天*/
int GetDaysInYear(int year,int month,int day)
{
 int sum=0;//总天数
 int isrun=IsLeapYear(year);//闰年返回 1 ;否则返回 0;
 int i=0;
 int j=0;
 if(isrun)
 {
 for(i=0;i<month-1;i++)
 {
 sum=sum+LeapDays[i];
 }
 }
 else
 {
 for(i=0;i<month-1;i++)
 {
 sum=sum+CommonDays[i];
 }
 }
 sum=sum+day;
 return sum;
}
 
/*求指定月总共多少天*/
int GetDaysInMonth(int year,int month)
{
 int isrun=IsLeapYear(year);//闰年返回 1 ;否则返回 0;
 if(isrun)
 {
 return LeapDays[month-1];
 }
 else
 {
 return CommonDays[month-1];
 }
}
 
//求今天是周几。周四就返回 4 。周日 返回 0;非法返回 -1;
int GetWeek(int year,int month,int day)
{
 int sum = 0;
 if(0 == IsValidDate(year,month,day))
 {
 printf("Input date is invalid\n");
 return -1;
 }
 
 sum = year -1;
 sum = sum + sum / 4 - sum / 100 + sum / 400;
 sum = sum + GetDaysInYear(year,month,day);
 
 return sum % 7;
}

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

原文链接:https://blog.csdn.net/wang_huizhang/article/details/54137220

延伸 · 阅读

精彩推荐