求一个万年历C语言编程 急用啊
发布网友
发布时间:2024-09-30 09:28
我来回答
共2个回答
热心网友
时间:2024-12-15 06:11
#include<stdio.h>
#include<stdlib.h>
int isLeap(int year);
int maxDay(int year,int month);
int eachYearTotal(int year,int month,int day);
int week(int year,int month,int day);
char *chineseWeek(int numb$r);
void output(int year,int month,int day);
void display(int year,int month);
int check(int year,int month,int day);
void display1(int year);
void display2(int year,int month);
void display3(int year,int month,int day);
void getMenu();
void main()
{
int year,month,day,flag,select; /*flag是标志,select是选择*/
while(1)
{
getMenu();
printf("请输入你的选择: ");
fflush(stdin); /*刷新缓冲区的函数*/
scanf("%d",&select);
fflush(stdin); /*刷新缓冲区的函数*/
switch(select)
{
case 1:
do
{
printf("请输入年: ");
scanf("%d",&year);
flag=check(year,1,1);
if(flag==0)
{
printf("输入无效\n");
}
}while(flag==0);
display1(year);
break;
case 2:
do
{
printf("请输入年月: ");
scanf("%d%d",&year,&month);
flag=check(year,month,1);
if(flag==0)
{
printf("输入无效\n");
}
}while(flag==0);
display2(year,month);
break;
case 3:
do
{
printf("请输入年月日: ");
scanf("%d%d%d",&year,&month,&day);
flag=check(year,month,day);
if(flag==0)
{
printf("输入无效\n");
}
}while(flag==0);
display3(year,month,day);
break;
case 4:
exit(0);
default:
printf("输入有误!\n\n");
}
}
}
int isLeap(int year) /*判断是否是闰年*/
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else
return 0;
}
int maxDay(int year,int month) /*每月天数*/
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeap(year))
a[1]=29;
return a[month-1];
}
int eachYearTotal(int year,int month,int day) /**/
{
int i,sum=0;
for(i=1;i<month;i++)
{
sum+=maxDay(year,i);
}
sum+=day;
return sum;
}
int week(int year,int month,int day)
{
int number=(year-1)+(year-1)/4-(year-1)/100+(year-1)/400+eachYearTotal(year,month,day);
number%=7;
return number;
}
char *chineseWeek(int number)
{
switch(number)
{
case 0:
return "星期日";
case 1:
return "星期一";
case 2:
return "星期二";
case 3:
return "星期三";
case 4:
return "星期四";
case 5:
return "星期五";
case 6:
return "星期六";
default:
return "error";
}
}
void output(int year,int month,int day)
{
int number=week(year,month,day);
printf(" 公元%d年%d月%d日%10s\n\n",year,month,day,chineseWeek(number));
}
void display(int year,int month)
{
int max,number,i,j=1;
max=maxDay(year,month);
number=week(year,month,1);
printf("%4s%4s%4s%4s%4s%4s%4s\n","日","一","二","三","四","五","六");
for(i=0;i<number;i++)
{
printf(" ");
}
while(j<=max)
{
printf("%4d",j);
if(i%7==6)
{
printf("\n");
}
i++;
j++;
}
printf("\n\n");
}
int check(int year,int month,int day)
{
if(year<0||month<1||month>12||day<1||day>maxDay(year,month))
return 0;
else
return 1;
}
void display1(int year)
{
int i;
printf("\n");
for(i=1;i<=12;i++)
{
printf(" 公元%d年%d月\n\n",year,i);
display(year,i);
}
}
void display2(int year,int month)
{
printf("\n 公元%d年%d月\n\n",year,month);
display(year,month);
}
void display3(int year,int month,int day)
{
printf("\n");
output(year,month,day);
display(year,month);
}
void getMenu()
{
printf("1.年历\t2.月历\t3.日历\t4.退出\n");
}
热心网友
时间:2024-12-15 06:11
#include<stdio.h>
#include<time.h>
int leap (int year)//判断闰年
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else return 0;
}
int days_month (int month,int year)//判断月
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return 31;
if(month==4||month==6||month==9||month==11)
return 30;
if(month==2&&leap(year)==1) return 29;
else return 28;
}
int firstday(int month,int year)//判断年
{
int w;
w=(1+2*month+3*(month+1)/5+year+year/4+year/400-year/100)%7+1;
return w;
}
main()
{
//调用系统时间
time_t tval;
struct tm *now;
tval = time(NULL);
now = localtime(&tval);
printf("现在时间: %4d年 %d月 %02d日 %d:%02d:%02d\n", now->tm_year+1900, now->tm_mon+1, now->tm_mday,
now->tm_hour, now->tm_min, now->tm_sec);
//调用结束
int i,j=1,k=1,a,b,month,year;
b=days_month(now->tm_mon+1,now->tm_year+1900);
a=firstday (now->tm_mon+1,now->tm_year+1900);
printf(" Sun Mon Tue Wed Thu Fri Sat \n");
if(a==7)
{
for(i=1;i<=b;i++)
{
printf("%4d",i);
if(i%7==0)
{
printf("\n");
}
}
}
if(a!=7)
{
while (j<=4*a)
{
printf(" ");
j++;
}
for(i=1;i<=b;i++)
{
printf("%4d",i);
if(i==7*k-a)
{
printf("\n");
k++;
}
}
}
printf("\n");
}