C语言编程(c++平台实现)万年历
发布网友
发布时间:2022-04-22 20:01
我来回答
共3个回答
热心网友
时间:2023-05-18 02:05
/*C语言编程万年历
要求输入年月,判断是否闰年;
输入年月日,判断星期几;
输入年份,打出12个月的月历;
输入年份,月份,打印出本月日历;
要求用多个函数实现。*/
#include<stdio.h>
#include<time.h>
#include<string.h>int calendar[12][6][7];/*月历*/
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Satarday"};
char* Monthname[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
int monthday[]={31,28,31,30,31,30,31,31,30,31,30,31};
char* menu[]={/*操作菜单*/
"1.Input a year number,check whether it's a leap year.\n",
"2.Input year,month,day,check the weekday.\n",
"3.Input year,month,output the calendar of that month.\n",
"4.Input year,output all the month calendar.\n",
"0.Exit.\n"
};/*判断参数year传递的年份是否是闰年*/
int IsLeapyear(int year)
{
if(!(year%4)&&year%100||!(year%400)) return 1;
else return 0;
}/*输入年月日,判断星期几,利用Zeller公式w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
w是结果星期数,y是年份的后两位,c是年份的前两位,m是月份,3≤m≤14,也就是当
m≤2时,要算到前一年的13月份和14月份,最后要将w对7取模
*/
int WeekDay(int year,int month,int day)
{
int w,y,c,m,d;
c=year/100;
if(month<3){
m=12+month;
y=year%100-1;
}
else{
m=month;
y=year%100;
}
d=day;
w=y+y/4+c/4-2*c+26*(m+1)/10+d-1;
return (w%7+7)%7;
}/*输入年份,月份,打印出本月的日历*/
void Monthly(int year,int month)
{
int weekday,i,j;
if(month==2)
if(IsLeapyear(year)) monthday[1]+=1;
weekday=WeekDay(year,month,1);
printf("%s\n",Monthname[month-1]);
printf("Sun. Mon. Tue. Wed. Thu. Fri. Sat.\n");
for(i=1,j=weekday;i<=monthday[month-1];i++,j++){
calendar[month-1][j/7][j%7]=i;
}
for(i=0;i<6;i++){
for(j=0;j<7;j++)
{
if(calendar[month-1][i][j]==0) printf("%5c",' ');
else printf("%-5d",calendar[month-1][i][j]);
}
printf("\n");
}
}void allMonth(int year) /*输入年份,打印出12个月的月历*/
{
int i;
for(i=1;i<=12;i++){
Monthly(year,i);
getch();/*按任意键继续执行*/
}
}void main(void)
{
int year,month,day,i,n,weekday;
memset(&calendar,sizeof(calendar),0); /*初始化月历*/
for(i=0;i<5;i++)
printf("%s",menu[i]);
while(1){
printf("Please choose the menu:");
scanf("%d",&n);
printf("\n");
switch(n){
case 1:
printf("Please input year:");
scanf("%d",&year);
if(IsLeapyear(year)) printf("\n%d is leap year.\n",year);
else printf("%d isn't leap year.\n",year);
break;
case 2:
printf("Please input year month day:");
scanf("%d%d%d",&year,&month,&day);
printf("\n");
weekday=WeekDay(year,month,day);
printf("That day is %s\n",week[weekday]);
break;
case 3:
printf("Please input year month,then it output a calendar of that month:");
scanf("%d%d",&year,&month);
printf("\n");
Monthly(year,month);
break;
case 4:
printf("Please input year,then it will output the calendar of that year:");
scanf("%d",&year);
printf("\n");
allMonth(year);
break;
case 0:
return;
default:
printf("The number you input is invalid.\n");
break;
}
}
getch(); /*按任意键,程序退出*/
}这个程序是可用的~~希望能帮到你~~~
热心网友
时间:2023-05-18 02:06
实现输入年月日输出星期用蔡勒公式,蔡勒(zeller)公式:是一个计算星期的公式。
随便给一个日期,就能用这个公式推算出是星期几。
蔡勒公式如下:
w = [c/4] - 2c + y + [y/4] + [13 * (m+1) / 5] + d - 1
公式中的符号含义如下:
w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六
c:世纪(前两位数)
y:年(后两位数)
m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d:日
[ ]代表取整,即只要整数部分。
下面以中华人民共和国成立100周年纪念日那天(2049年10月1日)来计算是星期几,过程如下:
w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
=49+[49/4]+[20/4]-2×20+[26×(10+1)/10]+1-1
=49+[12.25]+5-40+[28.6]
=49+12+5-40+28
=54 (除以7余5)
即2049年10月1日(100周年国庆)是星期五。
再比如计算2006年4月4日,过程如下:
w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
=6+[6/4]+[20/4]-2*20+[26*(4+1)/10]+4-1
=-12 (除以7余2,注意对负数的取模运算!) 其他的没怎么研究过
热心网友
时间:2023-05-18 02:06
#include<stdio.h>
#include<stdlib.h>
char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int IsLeapYear(int year) /*find out the year is leap year or not*/
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;}
int month_day(int year,int month)
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeapYear(year)&&month==2)
return 29;
else
return(mon_day[month-1]);}
int DaySearch(int year,int month,int day) /*search what day this day is*/
{
int c=0;
float s;
int m;
for(m=1;m<month;m++)
c=c+month_day(year,m);
c=c+day;
s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;
return ((int)s%7);
}int PrintAllYear(int year)/*print the all year*/
{
int temp;
int i,j;
printf("\n\n%d Calander\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month_str[i-1],i);
printf("0 1 2 3 4 5 6 \n");
printf("S M T W T F S \n\n");
temp=DaySearch(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf(" ");
else if(j-temp<10)
printf("%d ",j-temp);
else
printf("%d ",j-temp);if(j%7==0)
printf("\n");
}
}
return 0;
}int main()
{
int option,da;
char ch;
int year,month,day;
printf("Copyright @ 2005 TianQian All rights reserved!:):):)");
printf("\n\nWelcome to use the WanNianLi system!\n");while(1)
{
printf("\nPlease select the service you need:\n");
printf("\n1 Search what day the day is");
printf("\n2 Search whether the year is leap year or not");
printf("\n3 Print the calander of the whole year");
printf("\n4 Exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
while(1)
{
printf("\nPlease input the year,month and day(XXXX,XX,XX):");
scanf("%d,%d,%d,%c",&year,&month,&day);
da=DaySearch(year,month,day);
printf("\n%d-%d-%d is %s,do you want to continue?(Y/N)",year,month,day,week[da]);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 2:
while(1)
{
printf("\nPlease input the year which needs searched?(XXXX)");
scanf("%d",&year);
if(IsLeapYear(year))
printf("\n%d is Leap year,do you want to continue?(Y/N)",year);
else
printf("\n%d is not Leap year,do you want to continue(Y/N)?",year);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 3:
while(1)
{
printf("\nPlease input the year which needs printed(XXXX)");
scanf("%d",&year);
PrintAllYear(year);
printf("\nDo you want to continue to print(Y/N)?");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 4:
fflush(stdin);
printf("Are you sure?(Y/N)");
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
exit(0);
break;
default:
printf("\nError:Sorry,there is no this service now!\n");
break;
}}return 0;
}//网上看到的,功能差不多吧,你改一下吧,希望对你有用