这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 岭南EE码农港 » C语言课程设计作业——万年历

共1条 1/1 1 跳转至

C语言课程设计作业——万年历

菜鸟
2014-11-19 22:54:56     打赏
/* Note:Your choice is C IDE */
#include "stdio.h"
void screen();
void whatisit();
void leapyear();
void printyearday();
int IsLeapYear(int);
int whatisita(int,int);
int everymonth(int,int);
void printmonth(int,int);
void printmonthday();
char*week[]={"Sunday","Monday","Tuesday","Wednesday",
	         "Thursday","Friday","Saturday"};
void main()
{
	screen();
}
void screen()
{
	int n;
	while(1)
	{ printf("  ID:2013914102\n");
      printf("  万年历查询程序:\n");
	  printf("1.查询某年某月某日是星期几\n");
	  printf("2.查询某年是否是闰年
");
	  printf("3.打印某年的全年日历
");
	  printf("4.查询某月的最大天数
");
	  printf("5.退出
");
	  printf("请输入要查询的选项:");
	  scanf("%d",&n);
	  switch(n)
	  {
	  	case 1:whatisit();break;
	  	case 2:leapyear();break;
	  	case 3:printyearday();break;
	  	case 4:printmonthday();break;
	  	case 5:return;
	  	default:printf("你输入错误,请按要求输入!!!\n");break;
	  }
	}
}
void whatisit()
{
	int y,m,d,s,k=1,sum,n,i;
	char yn;
	int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	while(k)
    {    printf("请依次输入年,月,日:\n");
    	scanf("%d,%d,%d",&y,&m,&d);
    	s=0;
    	for(i=1;i<m;i++)
    	  s=s+a[i];
    	  s=s+d;
    	  if(IsLeapYear(y)&&m>2)s=s+1;
    	  sum=s;
    	  n=(y-1+(y-1)/4-(y-1)/100+(y-1)/400+sum)%7;
    	  printf("这天是%s\n",week[n]);
    	  printf("是否继续查询(Y/N)?");
    	  scanf("%c",&yn);
    	  getchar();
    	  if(yn=='y'||yn=='Y')k=1;
    	  else k=0;
    }
}

int IsLeapYear(int year)
{
	
	return (((year%4)==0 && (year%100)!=0) || ((year%400)==0));

}

void leapyear()
{
	int y,k=1;char yn;
	while(k)
	{
		printf("请输入要查询的年份:\n");
		scanf("%d",&y);
		if(IsLeapYear(y))
		    printf("是闰年!\n");
		else
		printf("不是闰年!\n");
		printf("是否继续查询(Y/N)?");
		scanf("%c",&yn);
		getchar();
		if(yn=='y'||yn=='y')
		    k=1;
		else k=0;
	}
}

int whatisita(int y,int m)
{
	int n,i,sum;
	int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31},s=0;
	for(i=1;i<m;i++)
	    s=s+a[i];
	if(IsLeapYear(y) && m>2)
	    s=s+i;
	    n=(y-1+(y-1)/4-(y-1)/100+(y-1)/400+sum)&7;
	return n;
}

void printyearday()
{
	int y,value,sum,i,k=1;
	char yn;
	while(k)
	{
		printf("请输入查询日历的年份\n:");
		scanf("%d",&y);
		for(i=1;i<=12;i++)
		{
			printf("%d年%d月的信息:\n",y,i);
			value=whatisita(y,i);
			sum=everymonth(y,i);
			printmonth(value,sum);
			printf("\n");
		}
		printf("是否继续查询(Y/N)?");
		scanf("%c",&yn);
		getchar();
		if(yn=='y'||yn=='y')
		    k=1;
		else k=0;
	}
}

int everymonth(int year,int month)
{
	int s,i;
	static int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	s=a[month];
	if(IsLeapYear(year)&&month==2)
	    s=s+i;
	return s;
}

void printmonthday()
{
	int y,m,value,k=1;char yn;
	while(k)
	{
		printf("请输入查询的年份,月份\n:");
		scanf("%d,%d",&y,&m);
		value=everymonth(y,m);
		printf("%d年%d月的最大天数为%d:\n",y,m,value);
		printf("%c",&yn);getchar();
		if(yn=='y'||yn=='y')
		    k=1;
		else k=0;
	}
}

void printmonth(int value,int sum)
{
	int n,i=0,j;
	char*x[7]={"Sunday","Monday","Tuesday","Wenesday","Tursday",
		       "Friday","Saturday"};
	for(j=0;j<7;j++)
	{
		printf("%s",x[j]);
	}
	printf("\n");
	while(i<value)
	{
		printf(" ");
		i++;
	}
	for(n=1;n<=7-value;n++)
	printf("    %d",n);
	while(n<=sum)
	{
		if((n+value-1)%7==0)
		printf("\n");
		if(n/10>0)
		printf("    %d",n);
		else
		printf("    %d",n);
		n++;
	}
}
	

 

感想:

       输入的时候一定要仔细看清楚,不然很容易发生错误

       这个万年历可以很清楚知道年月日的具体情况

 




关键词: 万年历    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]