c语言实现万年历程序

一个下午加一个晚上完成这个万年历程序,这是我目前为止写的最长的程序了,完成后运行成功的那一瞬间还是有一些小成就感的。

这个程序目前有五个功能:

1.查看当前年历或月历;

2.搜索某年年历或某月月历;

3.查询某天是星期几;

4.判断某年是否为闰年

5.查询历史上某年是该年的第几天

c语言实现万年历程序_第1张图片


代码如下:

-----------------------------------------------------------------------------------------------------------------------------------------------------

#include 
#include
#include "time.h" 

int head()			//显示欢迎界面,返回功能值 
{int select;
 printf("\n\n");
 printf("\t\t\t<--欢迎使用万年历-->\n\n");
 printf("\t\t****************************************\n");
 printf("\t\t请选择要使用的功能:\n\n");
 printf("\t\t\t1.查看当年年历或当月月历\n");
 printf("\t\t\t2.搜素某年的年历或某月的日历\n");
 printf("\t\t\t3.查询历史上某日为星期几\n");
 printf("\t\t\t4.判断历史上某年是否为闰年\n");
 printf("\t\t\t5.查询历史上某天是该年的第几天\n");
  printf("\t\t\t6.退出\n");
 printf("\t\t****************************************\n");
 printf("\t\t\t请选择<1,2,3,4,5,6>:"); 
 scanf("%d",&select) ;
 printf("\n");
 return(select);
}


void searchhead(int year,int month)			//月历的表头 
	{printf("\t%d年%d月\n",year,month);
	 printf("\t***************************************************\n")	;
	 printf("\t日\t一\t二\t三\t四\t五\t六\n"); 
	}

int monthnum(int year,int month)			//返回月份多少天 
{int a1[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 int a2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
 if(leapyear(year)==1)
 	{
 		return(a2[month]);
 	}
 else
 	{
 		return(a1[month]);
 	}
}


int leapyear(int year)			//判断是否为闰年 
{
	if(year%4==0&&year%100!=0||year%4==0&&year%400==0) 
		{
			return(1);
		}         
     else
	 	{
	 		return(0);
	 	} 
}  

int  week(int year,int month,int day)			//根据蔡勒公式计算星期几 
{int w ,c,y;
 if(month==1) 
 	{
 		month=13;
 		year=year-1;
 	}
  if(month==2) 
 	{
 		month=14;
 		year=year-1;
 	}
 y=year%100;
 c=(year/1000)*10+(year/100%10);
 if(y==0)
 	{
 		c=c-1;
 	}
 w=y+floor(y/4)+floor(c/4)-2*c+floor(26*(month+1)/10.0)+day-1;
while(w<0)
	{
		w=w+7;
	}
 if(w%7==0)
 	{
 		return(7);
 	}
return(w%7);
}


int function3()			//第三个功能查询历史上某日为星期几的模块 
{int year=0,month=1,day=1;
 printf("-----查询历史上某日为星期几-----\n");
 printf("\n");
 do
 {if(year<0)
 	{
 		printf("\t系统提示:您输入了错误的年份请从新输入...\n");
 	}
 printf("\t请输入要查询的年份:");
 scanf("%d",&year);
 printf("\n");
 } while(year<0);
 
 do
 {if(month<1||month>12)
 	{
 		printf("\t系统提示:您输入了错误的月份请从新输入...\n");
 	}
printf("\t请输入%d年的第几月:",year);
 scanf("%d",&month);
 printf("\n");
 } while(month<1||month>12);
 
 do
 {if(day<1||day>31)
 	{
 		printf("\t系统提示:您输入了错误的日期请从新输入...\n");
 	}
 printf("\t请输入%d年%d月的第几天:",year,month);
 scanf("%d",&day);
 printf("\n");
 } while(day<1||day>31);
 printf("\t您查询的%d年%d月%d号是星期%d\n\n",year,month,day,week(year,month,day));
}


void function4()			//功能四判断历史上某年是否为闰年模块 
{int year=0;
 printf("-----判断历史上某年是否为闰年-----\n");
 do
{if(year<0)
 	{
 		printf("\t系统提示:您输入了错误的年份请从新输入...\n");
 	}
 printf("\t请输入要查询的年份:");
 scanf("%d",&year);
 printf("\n");
 } while(year<0);
 if(leapyear(year)==1)
 	{
 		printf("\t您查询的%d年是闰年\n\n",year);	
 	}
 else
 	{
 		printf("\t您查询的%d年不是闰年\n\n",year);	
 	}

}

int function2()			//模块二:搜素某年的年历或某月的日历模块 
{int year=0,month=1,i,j,spacenum,count;
  printf("-----搜素某年的年历或某月的日历-----\n\n");
 do
 {if(year<0)
 	{
 		printf("\t系统提示:您输入了错误的年份请从新输入...\n");
 	}
 printf("\t请输入要查看的年份:");
 scanf("%d",&year);
 printf("\n");
 } while(year<0);
 
 do
 {if(month<0||month>12)
 	{
 		printf("\t系统提示:您输入了错误的月份请从新输入...\n");
 	}
 printf("\t请输入查看%d年的第几月,若想查看%d年年历请输入零:",year,year);
 scanf("%d",&month);
 printf("\n");
 } while(month<0||month>12);
 
 
 if(month!=0)
 	{
 	 searchhead(year,month)	;
	 spacenum=week(year,month,1);
	 if(spacenum!=7)			//输出空格 
	 	{
	 	for(i=1;i<=spacenum;i++)
	 		{
	 		printf("\t ");
	 		}	
	 	}
	 count=spacenum;
	 for(i=1;i<=monthnum(year,month);i++)	
	 	 	{
	 	 		printf("\t%d ",i);
	 	 		count++;
	 	 		if(count%7==0)
	 	 			{
	 	 				printf("\n");
	 	 			}	
	 		}
 	}
  if(month==0)
  	{
  		for(j=1;j<=12;j++)
  			{
  			 searchhead(year,j)	;
	 		 spacenum=week(year,j,1);
	 		 if(spacenum!=7)			//输出空格 
	 			{
	 			for(i=1;i<=spacenum;i++)
	 			{
	 			printf("\t ");
	 			}	
	 			}
	 		count=spacenum;
	    	for(i=1;i<=monthnum(year,j);i++)	
	 	 		{
	 	 		printf("\t%d ",i);
	 	 		count++;
	 	 		if(count%7==0)
	 	 			{
	 	 				printf("\n");
	 	 			}	
	 			}
			   printf("\n\n");	
  			}
  	}
	 	
}

int nowyear()    //获取系统的年份     
	{ 
	time_t rawtime; 
	struct tm * timeinfo; 
	time ( &rawtime ); 
	timeinfo = localtime ( &rawtime );
	return( timeinfo->tm_year+1900) ;  

	}	

int nowmonth()    //获取系统的月份
	{
	time_t rawtime; 
	struct tm * timeinfo; 
	time ( &rawtime ); 
	timeinfo = localtime ( &rawtime );
	return(timeinfo->tm_mon+1  ) ;	
	}

int function1()			//模块一:查看当年年历或当月月历 
{int k,i,j,spacenum,count;
  printf("-----查看当年年历或当月月历-----\n\n");
 printf("\t您想查看今年的年历还是当月的月历,请输入1【年历】或0【月历】:");
 scanf("%d",&k);
 printf("\n");
 if(k==0)
 	{
 	 searchhead(nowyear(),nowmonth())	;
	 spacenum=week(nowyear(),nowmonth(),1);
	 if(spacenum!=7)			//输出空格 
	 	{
	 	for(i=1;i<=spacenum;i++)
	 		{
	 		printf("\t ");
	 		}	
	 	}
	 count=spacenum;
	 for(i=1;i<=monthnum(nowyear(),nowmonth());i++)	
	 	 	{
	 	 		printf("\t%d ",i);
	 	 		count++;
	 	 		if(count%7==0)
	 	 			{
	 	 				printf("\n");
	 	 			}	
	 		}
 	}
  if(k==1)
  	{
  		for(j=1;j<=12;j++)
  			{
  			 searchhead(nowyear(),j)	;
	 		 spacenum=week(nowyear(),j,1);
	 		 if(spacenum!=7)			//输出空格 
	 			{
	 			for(i=1;i<=spacenum;i++)
	 			{
	 			printf("\t ");
	 			}	
	 			}
	 		count=spacenum;
	    	for(i=1;i<=monthnum(nowyear(),j);i++)	
	 	 		{
	 	 		printf("\t%d ",i);
	 	 		count++;
	 	 		if(count%7==0)
	 	 			{
	 	 				printf("\n");
	 	 			}	
	 			}
			   printf("\n\n");	
  			}
  	}
	 	
}



void function5()			//功能五:查询历史上某天是该年的第几天 
{int year=0,month=1,day=1,i,s=0;
 int a[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
 int b[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 printf("-----查询历史上某天是该年的第几天-----\n");
 printf("\n");
 do
 {if(year<0)
 	{
 		printf("\t系统提示:您输入了错误的年份请从新输入...\n");
 	}
 printf("\t请输入要查询的年份:");
 scanf("%d",&year);
 printf("\n");
 } while(year<0);
 
 do
 {if(month<1||month>12)
 	{
 		printf("\t系统提示:您输入了错误的月份请从新输入...\n");
 	}
printf("\t请输入%d年的第几月:",year);
 scanf("%d",&month);
 printf("\n");
 } while(month<1||month>12);
 
 do
 {if(day<1||day>31)
 	{
 		printf("\t系统提示:您输入了错误的日期请从新输入...\n");
 	}
 printf("\t请输入%d年%d月的第几天:",year,month);
 scanf("%d",&day);
 printf("\n");
 } while(day<1||day>31);
 s=s+day;
 if(leapyear(year)==1)
 	{
 		for(i=1;i



你可能感兴趣的:(c语言实现万年历程序)