#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define str " SUN MON TUE WED THU FRI SAT "
void menu(); //操作菜单
int leap(int year); //判断某年是不是闰年
int days_month(int year,int month); //返回某月的天数
int someday(int year,int month,int day); //计算某天是星期几
void search_month(int year,int month); //输出某月的日历
void search_someday(int year,int month,int day); //查询某日是星期几
int main()
{
int i,year,month,day;
int k1,k2,k3,k4; //用于判断输入的数据和操作是否正确,1代表正确,0代表错误
int a = 1;
menu();
do
{
printf("请输入你要进行的操作(1-4):");
scanf("%d",&i);
getchar();
switch(i)
{
case 1:
printf("请输入你要查询的年份:");scanf("%d",&year);
for(a;a<=12;a++)
{
printf(" ---------******* %d *******------------ ",a);
search_month(year,a);
}
printf("---------*********************------------ ");
break;
case 2:
printf("请输入您所要查询的相关信息. ");
printf("年份:");scanf("%d",&year);
printf("月份:");scanf("%d",&month);
k1=1;
while(k1)
{
if(month>12||month<1)
{
printf("你输入的月份有误,请重新输入! ");
printf("月份:");
scanf("%d",&month);
if(month<1||month>12) k1=1;
}
else k1=0;
}
search_month(year,month);
printf("---------*********************------------ ");
break;
case 3:
printf("请输入你要查询的日期 ");
printf("年份:");scanf("%d",&year);
printf("月份:");scanf("%d",&month);
k2=1;
while(k2)
{
if(month>12||month<1)
{
printf("你输入的月份有误,请重新输入! ");
printf("月份:");
scanf("%d",&month);
if(month<1||month>12) k2=1;
}
else k2=0;
}
// printf("%d年%d月有%d天 ",year,month,days_month(year,month)); 调试观察输入的日期是否有误,可忽略
printf("日期:");scanf("%d",&day);
k3=1;
while(k3)
{
if(day>days_month(year,month)||day<1)
{
printf("你输入的日期有误,请重新输入! ");
printf("日期:");
scanf("%d",&day);
if(day>days_month(year,month)||day<1) k3=1;
}
else k3=0;
}
search_someday(year,month,day);
printf("---------*********************------------ ");break;
case 4:exit(0);
printf("---------*********************------------ ");break;
default:
k4=1;
while(k4)
{
printf("你输入的数据有误,请重新输入你要进行的操作(1-4):");
scanf("%d",&i);
if(i<1||i>4) k4=1;
else k4=0;
}
break;
}
}while(i>0&&i<5);
return 0;
}
void menu()
{
int i=1;
printf(" ---------------------------------------- ");
printf(" |--------------------------------------| ");
printf(" | %d. 输出某年的日历 | ",i);
printf(" | %d. 输出某月的日历 | ",i+1);
printf(" | %d. 查询某日是星期几 | ",i+2);
printf(" | %d. 退出系统 | ",i+3);
printf(" |--------------------------------------| ");
printf(" ---------------------------------------- ");
}
int leap(int year)
{
if((year%4==0&&year%100!=0)||year%400==0) return 1;
else return 0;
}
int days_month(int year,int month)
{
int days_month;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days_month = 31;break;
case 4:
case 6:
case 9:
case 11:days_month = 30;break;
case 2:
if(leap(year)==1)
{
days_month = 29;break;
}
else days_month = 28;break;
}
return days_month;
}
int someday(int year,int month,int day)
{
//该函数运用了特殊的计算公式,详细可参见 http://blog.163.com/hexin_mars_blog/blog/static/248215040201571351115699/
int someday,m,n,i; //someday表示查询的日子是星期几
m=year-year/1000*1000;
n=m-m/100*100-1; //用于获得年份的后两位数
int days=0;
for(i=1;i<month;i++) days = days + days_month(year,i);
days =days + day;
someday = (n + n/4 - n/100 + n/400 + days)%7;
return someday;
}
void search_someday(int year,int month,int day)
{
char weekname[20];
switch(someday(year,month,day))
{
case 0:strcpy(weekname,"星期天") ;break;
case 1:strcpy(weekname,"星期一") ;break;
case 2:strcpy(weekname,"星期二") ;break;
case 3:strcpy(weekname,"星期三") ;break;
case 4:strcpy(weekname,"星期四") ;break;
case 5:strcpy(weekname,"星期五") ;break;
case 6:strcpy(weekname,"星期六") ;break;
}
printf("你所要查询的日期是%s ",weekname);
}
void search_month(int year,int month)
{
int i=0,j,k=0,m; //j用于记录某月的第一天是星期几 m用于记录某月的天数 k用于换行提示
char x[10]=" ";
m=days_month(year,month);
j=someday(year,month,1);
//输出某月的日历
printf("------------------------------------------ ");
printf("%s ",str);
for(;i<j;i++)
{
printf("%s",x);k++;
}
for(i=1;i<=m;i++)
{
k++;
if(k%7==0)
{
if(i<10)
{
printf(" %d ",i);
printf(" ");
}
else if(i>=10)
{
printf(" %d ",i);
printf(" ");
}
}
else
{
if(i<10)
{
printf(" %d ",i);
}
else if(i>=10)
{
printf(" %d ",i);
}
}
}
printf(" ------------------------------------------ ");
}
void exit()
{
exit(0);
}
#include <stdlib.h>
#include <string.h>
#define str " SUN MON TUE WED THU FRI SAT "
void menu(); //操作菜单
int leap(int year); //判断某年是不是闰年
int days_month(int year,int month); //返回某月的天数
int someday(int year,int month,int day); //计算某天是星期几
void search_month(int year,int month); //输出某月的日历
void search_someday(int year,int month,int day); //查询某日是星期几
int main()
{
int i,year,month,day;
int k1,k2,k3,k4; //用于判断输入的数据和操作是否正确,1代表正确,0代表错误
int a = 1;
menu();
do
{
printf("请输入你要进行的操作(1-4):");
scanf("%d",&i);
getchar();
switch(i)
{
case 1:
printf("请输入你要查询的年份:");scanf("%d",&year);
for(a;a<=12;a++)
{
printf(" ---------******* %d *******------------ ",a);
search_month(year,a);
}
printf("---------*********************------------ ");
break;
case 2:
printf("请输入您所要查询的相关信息. ");
printf("年份:");scanf("%d",&year);
printf("月份:");scanf("%d",&month);
k1=1;
while(k1)
{
if(month>12||month<1)
{
printf("你输入的月份有误,请重新输入! ");
printf("月份:");
scanf("%d",&month);
if(month<1||month>12) k1=1;
}
else k1=0;
}
search_month(year,month);
printf("---------*********************------------ ");
break;
case 3:
printf("请输入你要查询的日期 ");
printf("年份:");scanf("%d",&year);
printf("月份:");scanf("%d",&month);
k2=1;
while(k2)
{
if(month>12||month<1)
{
printf("你输入的月份有误,请重新输入! ");
printf("月份:");
scanf("%d",&month);
if(month<1||month>12) k2=1;
}
else k2=0;
}
// printf("%d年%d月有%d天 ",year,month,days_month(year,month)); 调试观察输入的日期是否有误,可忽略
printf("日期:");scanf("%d",&day);
k3=1;
while(k3)
{
if(day>days_month(year,month)||day<1)
{
printf("你输入的日期有误,请重新输入! ");
printf("日期:");
scanf("%d",&day);
if(day>days_month(year,month)||day<1) k3=1;
}
else k3=0;
}
search_someday(year,month,day);
printf("---------*********************------------ ");break;
case 4:exit(0);
printf("---------*********************------------ ");break;
default:
k4=1;
while(k4)
{
printf("你输入的数据有误,请重新输入你要进行的操作(1-4):");
scanf("%d",&i);
if(i<1||i>4) k4=1;
else k4=0;
}
break;
}
}while(i>0&&i<5);
return 0;
}
void menu()
{
int i=1;
printf(" ---------------------------------------- ");
printf(" |--------------------------------------| ");
printf(" | %d. 输出某年的日历 | ",i);
printf(" | %d. 输出某月的日历 | ",i+1);
printf(" | %d. 查询某日是星期几 | ",i+2);
printf(" | %d. 退出系统 | ",i+3);
printf(" |--------------------------------------| ");
printf(" ---------------------------------------- ");
}
int leap(int year)
{
if((year%4==0&&year%100!=0)||year%400==0) return 1;
else return 0;
}
int days_month(int year,int month)
{
int days_month;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: days_month = 31;break;
case 4:
case 6:
case 9:
case 11:days_month = 30;break;
case 2:
if(leap(year)==1)
{
days_month = 29;break;
}
else days_month = 28;break;
}
return days_month;
}
int someday(int year,int month,int day)
{
//该函数运用了特殊的计算公式,详细可参见 http://blog.163.com/hexin_mars_blog/blog/static/248215040201571351115699/
int someday,m,n,i; //someday表示查询的日子是星期几
m=year-year/1000*1000;
n=m-m/100*100-1; //用于获得年份的后两位数
int days=0;
for(i=1;i<month;i++) days = days + days_month(year,i);
days =days + day;
someday = (n + n/4 - n/100 + n/400 + days)%7;
return someday;
}
void search_someday(int year,int month,int day)
{
char weekname[20];
switch(someday(year,month,day))
{
case 0:strcpy(weekname,"星期天") ;break;
case 1:strcpy(weekname,"星期一") ;break;
case 2:strcpy(weekname,"星期二") ;break;
case 3:strcpy(weekname,"星期三") ;break;
case 4:strcpy(weekname,"星期四") ;break;
case 5:strcpy(weekname,"星期五") ;break;
case 6:strcpy(weekname,"星期六") ;break;
}
printf("你所要查询的日期是%s ",weekname);
}
void search_month(int year,int month)
{
int i=0,j,k=0,m; //j用于记录某月的第一天是星期几 m用于记录某月的天数 k用于换行提示
char x[10]=" ";
m=days_month(year,month);
j=someday(year,month,1);
//输出某月的日历
printf("------------------------------------------ ");
printf("%s ",str);
for(;i<j;i++)
{
printf("%s",x);k++;
}
for(i=1;i<=m;i++)
{
k++;
if(k%7==0)
{
if(i<10)
{
printf(" %d ",i);
printf(" ");
}
else if(i>=10)
{
printf(" %d ",i);
printf(" ");
}
}
else
{
if(i<10)
{
printf(" %d ",i);
}
else if(i>=10)
{
printf(" %d ",i);
}
}
}
printf(" ------------------------------------------ ");
}
void exit()
{
exit(0);
}