电子万年历: 用C语言设计一个年历系统
发布网友
发布时间:2022-05-04 20:20
我来回答
共3个回答
热心网友
时间:2022-06-25 14:59
代码如下,完全按照题目要求:
#include <stdio.h>
#include <conio.h>
long int f(int year,int month)
{/*f(年,月)=年-1,如月<3;否则,f(年,月)=年*/
if(month<3) return year-1;
else return year;
}
long int g(int month)
{/*g(月)=月+13,如月<3;否则,g(月)=月+1*/
if(month<3) return month+13;
else return month+1;
}
long int n(int year,int month,int day)
{
/*N=1461*f(年、月)/4+153*g(月)/5+日*/
return 1461L*f(year,month)/4+153L*g(month)/5+day;
}
int w(int year,int month,int day)
{
/*w=(N-621049)%7(0<=w<7)*/
return(int)((n(year,month,day)%7-621049L%7+7)%7);
}
int date[12][6][7];
int day_tbl[ ][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
main()
{int sw,leap,i,j,k,wd,day;
int year;/*年*/
char title[]="Sun Mon Tue Wed Thu Fri Sat ";
const char *month[]={"*","January","February","March","April","May","June","July","August","September","October","November","December"};
printf("Input the year: ");/*输入年*/
scanf("%d%*c",&year);/*输入年份值和掠过值后的回车*/
sw=w(year,1,1);
leap=year%4==0&&year%100||year%400==0;/*判闰年*/
for(i=0;i<12;i++)
for(j=0;j<6;j++)
for(k=0;k<7;k++)
date[i][j][k]=0;/*日期表置0*/
for(i=0;i<12;i++)/*一年十二个月*/
for(wd=0,day=1;day<=day_tbl[leap][i];day++)
{/*将第i+1月的日期填入日期表*/
date[i][wd][sw]=day;
sw=++sw%7;/*每星期七天,以0至6计数*/
if(sw==0) wd++;/*日期表每七天一行,星期天开始新的一行*/
}
printf("\nThe calendar of the year %d\n\n",year);
for(i=0;i<12;i+=2)
{/*先测算第i+1月和第i+2月的最大星期数*/
for(wd=0,k=0;k<7;k++)/*日期表的第六行有日期,则wd!=0*/
wd+=date[i][5][k]+date[i+1][5][k];
wd=wd?6:5;
printf(" %-9s %d\t\t\t%-9s %d\n",month[i+1],i+1,month[i+2],i+2);
printf(" %s\t%s\n",title,title);
for(j=0;j<wd;j++)
{
printf(" ");/*输出四个空白符*/
/*左栏为第i+1月,右栏为第i+2月*/
for(k=0;k<7;k++)
if(date[i][j][k])
printf("%-4d",date[i][j][k]);
else printf("%4s","");
printf("%3s","");/*输出四个空白符*/
for(k=0;k<7;k++)
if(date[i+1][j][k])
printf("%-4d",date[i+1][j][k]);
else printf("%4s","");
printf("%4s\n","");
}
printf("===========================\t===========================\n");
/*键入回车输出下一个月的日历*/
/*scanf("%*c");*/
}
puts("\n");
getch();
}
运行结果如下:
Input the year: 2009
The calendar of the year 2009
January 1 February 2
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 2 3 1 2 3 4 5 6 7
4 5 6 7 8 9 10 8 9 10 11 12 13 14
11 12 13 14 15 16 17 15 16 17 18 19 20 21
18 19 20 21 22 23 24 22 23 24 25 26 27 28
25 26 27 28 29 30 31
=========================== ===========================
March 3 April 4
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11
15 16 17 18 19 20 21 12 13 14 15 16 17 18
22 23 24 25 26 27 28 19 20 21 22 23 24 25
29 30 31 26 27 28 29 30
=========================== ===========================
May 5 June 6
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 2 1 2 3 4 5 6
3 4 5 6 7 8 9 7 8 9 10 11 12 13
10 11 12 13 14 15 16 14 15 16 17 18 19 20
17 18 19 20 21 22 23 21 22 23 24 25 26 27
24 25 26 27 28 29 30 28 29 30
31
=========================== ===========================
July 7 August 8
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 1
5 6 7 8 9 10 11 2 3 4 5 6 7 8
12 13 14 15 16 17 18 9 10 11 12 13 14 15
19 20 21 22 23 24 25 16 17 18 19 20 21 22
26 27 28 29 30 31 23 24 25 26 27 28 29
30 31
=========================== ===========================
September 9 October 10
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 1 2 3
6 7 8 9 10 11 12 4 5 6 7 8 9 10
13 14 15 16 17 18 19 11 12 13 14 15 16 17
20 21 22 23 24 25 26 18 19 20 21 22 23 24
27 28 29 30 25 26 27 28 29 30 31
=========================== ===========================
November 11 December 12
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7 1 2 3 4 5
8 9 10 11 12 13 14 6 7 8 9 10 11 12
15 16 17 18 19 20 21 13 14 15 16 17 18 19
22 23 24 25 26 27 28 20 21 22 23 24 25 26
29 30 27 28 29 30 31
=========================== ===========================
热心网友
时间:2022-06-25 14:59
两个月并排显示也太*了,
我做了一个各个月连续显示的:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int leap(int year )
{
if ((year %4 == 0) && (year % 100 != 0)
|| (year % 400 == 0))
{
return 1;
}
return 0;
}
void show(int year,int month)
{
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,wdays,mdays,days;
int count=0;
for(i=1,days=0;i<year;i++)
{
if(leap(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && leap(year))
{
days+=29;
}
else
{
days+=month_day[i];
}
}
printf("\n----------------------------\n");
printf(" %s %d\n",month_str[month],month);
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
wdays = days % 7;
for( j = 0; j <= wdays; j++)
{
if(wdays==6) break;
for( i = 0; i < 4; i++)
{
printf(" ");
}
count++;
}
if(month == 2 && leap(year))
{
mdays=29;
}
else
{
mdays= month_day[month];
}
for(i=1;i<=mdays;i++)
{
if( i > 1 && count % 7 == 0 )
{
printf("\n");
}
printf("%4d",i);
count++;
}
printf("\n----------------------------\n");
}
main()
{
int year,month;
printf("Input the year:");
scanf("%d",&year);
printf("The calendar of the year %d.",year);
printf("\n----------------------------\n");
for(month=1;month<=12;month++)
show(year,month);
system("pause");
}
运行:
Input the year:2004
The calendar of the year 2004.
----------------------------
----------------------------
Jan 1
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Feb 2
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Mar 3
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Apl 4
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
May 5
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Jun 6
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Jul 7
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Aug 8
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Sep 9
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Oct 10
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Nov 11
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Dec 12
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
热心网友
时间:2022-06-25 14:59
我做了一个各个月连续显示的:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int leap(int year )
{
if ((year %4 == 0) && (year % 100 != 0)
|| (year % 400 == 0))
{
return 1;
}
return 0;
}
void show(int year,int month)
{
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,wdays,mdays,days;
int count=0;
for(i=1,days=0;i<year;i++)
{
if(leap(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && leap(year))
{
days+=29;
}
else
{
days+=month_day[i];
}
}
printf("\n----------------------------\n");
printf(" %s %d\n",month_str[month],month);
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
wdays = days % 7;
for( j = 0; j <= wdays; j++)
{
if(wdays==6) break;
for( i = 0; i < 4; i++)
{
printf(" ");
}
count++;
}
if(month == 2 && leap(year))
{
mdays=29;
}
else
{
mdays= month_day[month];
}
for(i=1;i<=mdays;i++)
{
if( i > 1 && count % 7 == 0 )
{
printf("\n");
}
printf("%4d",i);
count++;
}
printf("\n----------------------------\n");
}
main()
{
int year,month;
printf("Input the year:");
scanf("%d",&year);
printf("The calendar of the year %d.",year);
printf("\n----------------------------\n");
for(month=1;month<=12;month++)
show(year,month);
system("pause");
}
运行:
Input the year:2004
The calendar of the year 2004.
----------------------------
----------------------------
Jan 1
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Feb 2
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Mar 3
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Apl 4
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
May 5
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Jun 6
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Jul 7
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Aug 8
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Sep 9
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Oct 10
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Nov 11
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------
----------------------------
Dec 12
Sun Mon Tue Wed Thu Fri Sat
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
----------------------------