如何制作万年历
课堂作业,毫无头绪,能否讲讲思路及相应的代码分析? #include <stdio.h>#include <stdlib.h>
#include <time.h>
#include<conio.h>
typedef struct today
{
int day;
int month;
int year;
} today;
int day_cankao={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
char *week[]=
{
"Sun","Mon","Tue","Wen","Thu","Fir","Sat"
};
struct tm *todayuse;//struct tm定义c语言中time的结构体
today today_current;
int getweekday(today today_usenow)
{
// w = y + + [ c/4 ] – 2c+ + d – 1
//其中,c是年份的前两位,y是年份的后两位,m是月份,d是日期,这里需要注意的是,如果是1月和2月,c和y需要按照上一年来取值。
int w=0;
int year=today_usenow.year;
int month=today_usenow.month;
if(today_usenow.month==1 || today_usenow.month==2)
{
month+=12;
year--;
}
int y=year%100;
int c=year/100;
int m=month;
w=y + y/4 +c/4- 2*c+ 26* (m+1) / 10 + today_usenow.day -1;
while(w<0)
{
w+=7;
}
return (w%7);
}
int is_leap(int year)
{
if( (year%4==0 && year%100!=0) || (year%400==0))
{
return 1;
}
else
return 0;
}
intgetmonthdays(int year,int month)
{
return day_cankao;
}
void print_calendar(today today_usenow)
{
printf("---------------------------\n");
printf("Sun Mon Tue Wen Thu Fir Sat\n");
int firstday=0;
today today_usehere=today_usenow;
today_usehere.day=1;
int day=getweekday(today_usehere);//日期前
//intdaysuseafter
int days=getmonthdays(today_usenow.year,today_usenow.month);//月总数
// 0 1 2 3 4 5 6// 6
int daysbefore=0;
if((today_usenow.month-1)==0)
{
//现在为1月,去年的十二月份
daysbefore=getmonthdays(today_usenow.year-1,12);
//printf("%d is",daysbefore);
}
else
{
daysbefore=getmonthdays(today_usenow.year,today_usenow.month-1);
}
int daysbefoeit=daysbefore-day+1;
printf("");
int count=1;
if(day==0)
{
daysbefoeit-=7;
for(int i=0;i<day+7;i++)
{
printf("%d", daysbefoeit);
daysbefoeit++;
}
printf("\n");
count=7;
}
else
{
for(int i=0;i<day;i++)
{
printf("%d", daysbefoeit);
daysbefoeit++;
}
count=day;
}
int m=1;
for(int i=0;i<=6-day;i++)
{
if(m<10)
{
printf(" %d",m);
}
else
{
printf("%d ",m);
}
m++;
}
printf("\n");
if(day==0)
{
count=14;
}
else
{
count=7;
}
int hang=0;
while(m<=days)
{
if(m<10)
{
printf(" %d",m);
}
else
{
printf("%d",m);
}
hang++;
if(hang==7)
{
printf("\n");
hang=0;
}
m++;
}
if(day==0)
{
count=days+7;
}
else
{
count=day+days;
}
int newmonth=1;
for(int j=hang;j<7;j++)
{
if(newmonth<10)
{
printf(" %d",newmonth);
}
else
{
printf("%d",newmonth);
}
newmonth++;
}
printf("\n");
count=count+7-hang;
for(int j=0;j< 42-count;j++)
{
if(newmonth<10)
{
printf(" %d",newmonth);
}
else
{
printf("%d",newmonth);
}
newmonth++;
}
去CSDN找代码参考{:10_256:} 路过看看,感觉应该不难实现,可惜不会C
页:
[1]