- #include
- /*自定义函数说明*/
- 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(); /*打印某年的某月最大天数*/
- void printyearmonthday(); /*查询某年某月日历*/
- /*定义一个判断星期几的指针数组*/
- char *week[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
- int main()
- {
- system("color 2F"); /*设置背景颜色为浅绿色*/
- printf("制作人ID:2013914215.\n\n"); /*个人信息,姓名:庞世聪,学号:2013914215*/
- screen(); /*万年历的入口*/
- getch();
- }
- /*万年历查询主界面(含部分功能)*/
- void screen()
- {
- int n;
- while (1)
- {
- /*万年历的功能*/
- printf(" 万年历查询程序:\n");
- printf("1:查询某年某月某日是星期几\n");
- printf("2:查询某年是否是闰年");
- printf("3:打印某年的全年日历");
- printf("4:打印某年某月的日历");
- printf("5:查询某年某月的最大天数");
- printf("6:退出");
- printf("请输入要查询的选项: ");
- scanf("%d",&n);
- switch (n)
- {
- /*实现相应功能的函数*/
- case 1:whatisit();break;
- case 2:leapyear();break;
- case 3:printyearday();break;
- case 4:printyearmonthday();break;
- case 5:printmonthday();break;
- case 6:return;
- default:printf("你输入错误,请按要求输入!!!");break;
- }/*end of switch (n)*/
- }/*end of while (1)*/
- }/*end of void screen ()*/
- /*查询某年某月某日是星期几*/
- 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("请依次输入年,月,日:");
- 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("%d 年 %d 月 %d 号是: %s.\n",y,m,d,week[n]);
- printf("是否要继续查询(Y/N)?");
- scanf("%c",&yn);
- getchar();
- if (yn == 'y' || yn == 'Y')
- {
- k = 1;
- }
- else
- {
- k = 0;
- }/*end of if (yn == 'y' || yn == 'Y')*/
- }/*end of while (k)*/
- }/*end of void whatisit()*/
- /*判断某年是否是闰年*/
- int IsLeapYear(int year)
- {
- return(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); /*返回一个整型值0或1*/
- } /*end of int IsLeapYear(int year)*/
- /*查询某年是否是闰年*/
- void leapyear()
- {
- int y, k = 1;
- char yn;
- while (k)
- {
- printf("请输入要查询的年份: ");
- scanf("%d",&y);
- if (IsLeapYear(y)) /*y是判断标志*/
- {
- printf("%d 年是闰年!",y);
- }
- else
- {
- printf("%d 年不是闰年!\n",y);
- }
- printf("是否继续查询(Y/N)?");
- scanf("%c",&yn); getch();
- if (yn == 'y' || yn == 'Y') /*是否继续查询入口*/
- {
- k = 1; /*k是是否继续查询的标志*/
- }
- else
- {
- k = 0;
- } /*end of if (yn == 'y' || yn == 'Y')*/
- } /*end of while (k)*/
- } /*end of void leapyear()*/
- /*计算当天是星期几*/
- int whatisita(int y,int m) /*whatisita 是有参函数,返回值是整型,y表示年,m表示月*/
- {
- int n, i;
- 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) /*判断是否闰年,如果是闰年就总天数加1天*/
- {
- s = s + 1;
- }
- n = (y-1+(y-1)/4-(y-1)/100+(y-1)/400+s+1)%7;
- return n; /*返回值是n*/
- }/*end of int whatisita(int y,int m)*/
- /*打印某年的全年日历*/
- void printyearday()
- {
- int y, value, sum, i, j, k = 1;
- char yn;
- while (k)
- {
- printf("请输入查询的日历的年份: ");
- scanf("%d",&y);
- for (i = 1; i <= 12; i++)
- {
- printf(" %d年%d月
- ",y,i);
- value = whatisita(y, i);
- sum = everymonth(y, i);
- printmonth(value, sum);
- printf("\n\n");
- }
- printf("是否继续查询(Y/N)?");
- scanf("%c",&yn); getchar();
- if (yn == 'Y' || yn == 'y')
- {
- k = 1;
- }
- else
- {
- k = 0;
- }
- } /*end of while (k)*/
- } /*end of void printyearday()*/
- /*查询某月最大天数*/
- int everymonth(int year, int month) /*everymonth是有参函数,year是年份,month是月份,返回值是整型*/
- {
- int s;
- 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 + 1;
- }
- return s; /*s是返回值*/
- } /*end of int everymonth(int year, int month)*/
- /*打印某年的某月最大天数函数*/
- void printmonthday()
- {
- int y, m, value, k = 1;
- char yn;
- while (k)
- {
- printf("请输入查询的年份,月份: ");
- scanf("%d,%d",&y,&m);
- value = everymonth(y,m);
- printf("%d年%d月的最大天数为%d.\n",y,m,value);
- printf("是否继续查询(Y/N)?");
- scanf("%c",&yn); getchar();
- if (yn == 'y' || yn == 'Y')
- {
- k = 1;
- }
- else
- {
- k = 0;
- }
- } /*end of while (k)*/
- } /*end of void printmonthday()*/
- /*打印月历*/
- void printmonth(int value,int sum) /*printmonth是有参函数,value和sum是参数,无返回值*/
- {
- int n, i = 0, j;
- char *x[7] = {"Sunday", "Monday", "Tuesday", "Wenesday", "Thursday", "Friday", "Saturday"};
- for (j = 0; j < 7; j++)
- {
- printf("%-10s",x[j]);
- }
- printf("\n");
- while (i < value)
- {
- printf(" ");
- i++;
- }
- for (n = 1; n < 7-value; n++)
- {
- printf("%-10d",n);
- }
- while (n <= sum)
- {
- if ((n + value - 1) % 7 == 0)
- {
- printf("\n");
- }
- if (n/10 > 0)
- {
- printf("%-10d",n);
- }
- else
- {
- printf("%-10d",n);
- }
- n++;
- }/*end of while (n <= sum)*/
- } /*end of void printmonth(int value,int sum)*/
- /*查询某年某月日历*/
- void printyearmonthday()
- {
- int y, m, value, sum, k = 1;
- char yn;
- while (k)
- {
- printf("请输入要查询日历的年份,月份: ");
- scanf("%d,%d",&y,&m);
- printf(" %d年%d月的日历\n",y,m);
- value = whatisita(y,m); /*whatisit 函数返回一个值,返回值是本月的第一天是星期几*/
- sum = everymonth(y,m); /*everymonth 函数返回一个值,返回值是本月的最大天数*/
- printmonth(value,sum);
- printf("\n");
- printf("是否继续查询(Y/N)?");
- scanf("%c",&yn); getchar();
- if (yn == 'y' || yn == 'Y')
- {
- k = 1;
- }
- else
- {
- k = 0;
- }
- }/*end of while (k)*/
- <p>
- } /*end of void printyearmonthday()*/
- </p>
- <p>
- </p>
- <p>
- </p>
- <img src="http://uphotos.eepw.com.cn/1415793597/pics/f574da662596daafce1d321d4ca9f843.PNG" width="361" height="294">
总结:写程序需要耐心和细心,需要反复检查,防止遗漏。