万年历
用C语言写了一个万年历出来,我是新手,请大家多多指教指教。有什么不足或者语法毛病的可以指出来。
#include<stdio.h>
year_days(int date) /*计算由公元1年到本年的日数*/
{
int days,a,b;
a=(date-1)/4+(date-1)/400-(date-1)/100;
b=date*365;
days=a+b;
return days;
}
month_days(int date) /*计算由1月开始到本月的日数*/
{
if(date==1)return 0;
if(date==2)return 31;
if(date==3)return 59;
if(date==4)return 90;
if(date==5)return 120;
if(date==6)return 151;
if(date==7)return 181;
if(date==8)return 212;
if(date==9)return 243;
if(date==10)return 273;
if(date==11)return 304;
if(date==12)return 334;
}
get_days(int date) /*计算总日子数*/
{
int year_days(int);
int month_days(int);
int days;
days=year_days(date)+month_days(date)+date;
if(date%100!=0&&date%4==0||date%400==0)
if(date>2)days++;
return days;
}
get_mondays(int date) /*计算每月的日数*/
{
if(date==1)return 31;
if((date%100!=0&&date%4==0||date%400==0)&&date==2)return 29;
if(date==2)return 28;
if(date==3)return 31;
if(date==4)return 30;
if(date==5)return 31;
if(date==6)return 30;
if(date==7)return 31;
if(date==8)return 31;
if(date==9)return 30;
if(date==10)return 31;
if(date==11)return 30;
if(date==12)return 31;
}
main()
{
int get_days(int); /*声明函数*/
int get_mondays(int);
int date,days,mondays;
int i;
printf("please Please enter a date\n"); /*输入年月日*/
printf("year:");
scanf("%d",&date);
printf("month:");
scanf("%d",&date);
printf("day:");
scanf("%d",&date);
days=get_days(date);
mondays=get_mondays(date);
printf(" %d/%d/%d\n",date,date,date);
printf("=============================\n");
printf(" sun mon tue wed thu fri sat\n");
for(i=1;i<=(days-date)%7;i++) /*总日数减去日再除7求余,可以计算出每月开头空了的天数*/
{
printf(" ");
}
for(i=1;i<=mondays;i++)
{
if(i<10)printf(" ");
if(i==date)
printf("[%d]",i);
else
printf(" %d ",i);
if((days-date+i)%7==0)
printf("\n");
}
printf("\n");
system("pause");
} 回复 1# xyq038558
膜拜、源码最好设置回复可见! 挺厉害的 回复 3# yuyuhongss
呵呵,大家彼此交流! 嘻嘻,不错哦~最好再来个printf("I love xxx"); 回复 5# 小甲鱼
好的,下次就加上“printf("I Love fishc!")” 本帖最后由 nbceshi 于 2011-3-1 23:13 编辑
好像没有考虑闰年
附上我的,嘿嘿
#include <stdio.h>
void main()
{
int yy, mm, dd, y, i; //yy是年,mm是月,dd是日,y是周几,i是计数。
printf("请输入年份: ");
scanf("%d", &yy);
printf("请输入月份: ");
scanf("%d", &mm);
if (mm == 2)
{
if(yy%4 == 0 && yy%100 != 0 || yy%400 == 0 )
{
dd = 29;
}
else
{
dd = 28;
}
}
else
{
dd=30 + mm%2;
}
switch (mm)
{
case 1: i = 6; break;
case 2: i = 2; break;
case 3: i = 2; break;
case 4: i = 5; break;
case 5: i = 0; break;
case 6: i = 3; break;
case 7: i = 5; break;
case 8: i = 1; break;
case 9: i = 4; break;
case 10: i = 6; break;
case 11: i = 2; break;
case 12: i = 4; break;
}
if((yy%4 == 0 && yy%100 !=0 || yy%400 == 0) && mm <=2)
{
i -= 1;
}
y = (1.25 * (yy%100)) / 1 + 1 + i;
y = y%7;
i = y;
printf(" 日 一 二 三 四 五 六\n");
for( ;y > 0; y--)
{
printf(" ");
}
y = i;
for(i = 1 ; i <= dd; i++)
{
if((i + y - 1)%7 == 0)
{
printf("\n");
}
printf("%3d",i);
}
} 特地前来膜拜 不错不错,如果在家点颜色就漂亮了,颜色只需要几句哦!!!!!
页:
[1]