要求:请问要用C++编制一个万年历,应该怎么编写程序,具体要求如下:
程序运行后,能
(1) 查询某年某月某日是星几
(2) 查询某年是不是闰年
(3) 打印某年的全年日历
(4) 退出
//做一个小型万年历
/*
要求:请问要用C++编制一个万年历,应该怎么编写程序,具体要求如下:
程序运行后,能
(1) 查询某年某月某日是星几
(2) 查询某年是不是闰年
(3) 打印某年的全年日历
(4) 退出
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char *week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int test_leap_year(int year) //测试某年是不是闰年
{
if(year%4==0)
if(year%100!=0)
return 1; //闰年
else if(year%400==0)
return 1; //闰年
else return 0; //非闰年
else return 0;
}
//计算某年1月1日是从1900年开始的第多少天
int calc_day(int year)
{
int i=0;
long int sum=0;
while(i<year-1900)
{
sum=sum+365+test_leap_year(1900+i);
i++;
}
return sum;
}
int query(int year,int month,int day) //返回星期几,0表示星期日
{
char leap_year=0; //闰年标志
char month12[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //记录12个月的天数,默认平年
char i=0;
long int sum=0; //sum记录这天是这年的第几天
leap_year=test_leap_year(year);
month12[1]+=leap_year;
while(i<month-1)
{
sum+=month12[i];
i++;
}
sum+=day;
sum+=calc_day(year);
sum=sum%7;
return sum;
}
void print_all_year(int year)
{
int weekday=0,month=0,day=1,leap_year=0,i=1,j=0;
char month12[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //记录12个月的天数,默认平年
char *month_name[]={"January","Febrary","March","April","May","June","July","August",\
"September","October","November","December"};
leap_year=test_leap_year(year);
month12[1]+=leap_year;
while(month<12)
{
day=1;
i=1;
j=0;
printf(" %s\n",*(month_name+month));
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
for(day=1;day<=month12[month];day++)
{
weekday=query(year,month+1,day);
if(day==1)
while(j<weekday)
{
printf(" ");
j++;
}
printf("%4d",day);
if(weekday==6)
printf("\n");
}
printf("\n");
month++;
}
}
void mesg()
{
printf("**************************************************\n");
printf("********输入一个数字,程序执行相应功能 ********\n");
printf("********1.查询某年某月某日是星几 ********\n");
printf("********2.查询某年是不是闰年 ********\n");
printf("********3.打印某年的全年日历 ********\n");
printf("********4.退出 ********\n");
printf("**************************************************\n");
}
void fun1()
{
int year=0,month=0,day=0,weekday=0;
printf("Input the year, month and day:\n");
scanf("%d%d%d",&year,&month,&day);
weekday=query(year,month,day);
printf("that day is %s\n",*(week+weekday));
}
void fun2()
{
int leap_year=0,year=0;
printf("Input the year you want to see: ");
scanf("%d",&year);
leap_year=test_leap_year(year);
if(leap_year)
printf("Yes, it is a leap year.\n");
else printf("No, it is not a leap year.\n");
}
void fun3()
{
int year=0;
printf("Input the year you want to see: ");
scanf("%d",&year);
print_all_year(year);
}
int main()
{
int year=0,month=0,day=0,weekday=0,function=0;
mesg();
while(function!=4)
{
printf(">>");
scanf("%d",&function);
getchar();
switch(function)
{
case 1:{fun1();} break;
case 2:{fun2();} break;
case 3:{fun3();} break;
case 4:{exit(0);} break;
default: break;
}
}
return 0;
}
vc6.0下通过测试。