#include <stdioh>
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; //判断二月是29天还是29天
else return 28;
}
int firstday(int month,int year)
{int w;
w=(1+2month+3(month+1)/5+year+year/4+year/400-year/100)%7+1; //判断每个月开始的第一天是星期几
return w;
}
main()
{int i,j=1,k=1,a,b,month,year;
printf("\n input month and year:\n");
scanf("%d%d",&month,&year); //输入月和年
b=days_month(month,year);
a=firstday (month,year);
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<=4a)
{printf(" ");
j++;
}
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7k-a)
{printf("\n");
k++;
}
}
}
printf("\n");
}
#include"stdioh"
void print(int);//函数声明
int dobb(int);
int date(int,int,int);
void main()
{
int year=0;
printf("输入打印的年份(XXXX)\n");
scanf("%d",&year);
print(year);
}
int dobb(int year)//闰年判断
{
if((year%4==0 && year%100!=0)|| (year%400==0))
return(1);
else return(0);
}
int date(int year,int month,int day)//判断该年某月某日是星期几的计算
{
int leap,cn=0,sum;//cn计算的是从该年1月1日起,到达这一天的天数总和
leap=dobb(year);
switch(month-1)
{
case 11:cn+=30;
case 10:cn+=31;
case 9:cn+=30;
case 8:cn+=31;
case 7:cn+=31;
case 6:cn+=30;
case 5:cn+=31;
case 4:cn+=30;
case 3:cn+=31;
case 2:if(leap) cn+=29;
else cn+=28;
case 1:cn+=31;
default:cn+=day;
}
sum=year-1+(year-1)/4-(year-1)/100+(year-1)/400+cn;//这是核心的一条公式,返回值是星期几(没有为什么)
return(sum%7);
}
void print(int year)//打印的核心算法
{
int i,j,n,leap,days,k,count;
leap=dobb(year);//判断是否闰年
days=date(year,1,1);//计算该年第一天是星期几
printf("%d年\n",year);
for(i=1;i<=12;i++)
{
printf("%3d月份\n",i);
printf("\n");
printf("\t");
printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8s\n","Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
if(days==7)
{
days=0;n=days;
}
else n=days;
printf("\t");
for(j=1;j<=8days;j++)
printf(" ");
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:count=31;break;
case 4:
case 6:
case 9:
case 11:count=30;break;
case 2:if(leap) count=29;
else count=28; break;
}
for(k=1;k<=count;k++)//以下代码是控制界面的对齐
{
printf("%-8d",k);
n++;
if(n==7)
{printf("\n");printf("\t");n=0;}
}
printf("\n\n");
days=n;
}
}
#include<stdioh>
#include<timeh>
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+2month+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<=4a)
{
printf(" ");
j++;
}
for(i=1;i<=b;i++)
{
printf("%4d",i);
if(i==7k-a)
{
printf("\n");
k++;
}
}
}
printf("\n");
}
# include <stdioh>
# include <mathh>
void printmonth(int m);
void printhead(int m);
int daysofmonth(int m);
int firstday(int y);
int year,weekday;
void main()
{
int i;
printf("请输入年份:");
scanf("%d",& year);
weekday=firstday(year);
printf("\n\n");
printf(" %d年\n",year);
for(i=1;i<=12;i++)
{
printmonth(i);
printf("\n");
}
printf("\n\n");
}
void printmonth(int m) //打印每月日历
{
int i,days;
printhead(m);
days=daysofmonth(m);
for(i=1;i<=days;i++)
{
printf("%5d",i);
weekday=(weekday+1)%7;
if (weekday==0) printf("\n ");
}
}
void printhead(int m) //打印每月的日历头(判定起始位置)
{
int i;
printf("\n%d月 日 一 二 三 四 五 六\n",m);
printf(" ");
for(i=0;i<weekday;i++)
printf(" ");
}
int daysofmonth(int m) //每月的天数
{
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:if (((year%4==0 && year%100!=0)||year%400==0))
return 29;
else
return 28;
default: return 0;
}
}
int firstday(int y) //判断某年元旦是星期几
{
double s ;
s=floor(year-1+(year-1)/40-(year-1)/1000+(year-1)/4000+1);
return (int)s%7;
}
以上就是关于C语言程序设计万年历全部的内容,包括:C语言程序设计万年历、C语言编写万年历、万年历(C语言编程)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)