如何用C语言编写一个万年历系统?
要求: 模仿现实生活中的挂历.当前页以系统当前日期的月份为准显示当前月的每一天(显示出日及对应的星期几).当系统...
要求: 模仿现实生活中的挂历. 当前页以系统当前日期的月份为准显示当前月的每一天(显示出日及对应的星期几).当系统日期变到下一月时,系统自动翻页到下一月.
展开
1个回答
展开全部
#include <stdio.h>
int day,year,month;
int isLeap()
{int isleap;
if(((year%4==0)&&!(year%100==0))||(year%400==0))isleap=1;
else isleap=0;
return isleap;}
int lastdayofmonth()
{int lastday,isleap;
switch (month)
{case 1: case 3: case 5: case 7:case 8: case 10: case 12: lastday=31;break;
case 4: case 6: case 9: case 11: lastday=30;break;
case 2: {isleap=isLeap();
if (isleap==1) lastday=29;
else lastday=28;}}
return lastday;}
int ValidDate()
{int valid;
int yearOK,monthOK,dayOK;
if ((year>1811)&&(year<=2012)) yearOK=1;
else yearOK=0;
if ((month>0)&&(month<=12)) monthOK=1;
else monthOK=0;
if ((day>0)&&(day<=lastdayofmonth())) dayOK=1;
else dayOK=0;
if(yearOK && monthOK && dayOK) valid=1;
else valid=0;
return valid;}
int getday(){printf("Enter a month/a day/a year");scanf("%d %d %d",&month,&day,&year);
return ValidDate();}
void incrementdate()
{if (day<lastdayofmonth()) day+=1;
else
{day=1;
if (month==12)
{month=1;year+=1;}
else month+=1;}}
void print()
if (ValidDate())printf("nextday is %d/%d/%d",month,day,year);elseprintf("Date error");}
void main(){getday();incrementdate();print();}
补充一下,又发现一个问题。输入之后虽然你做了输入检查,但是现在的写法,这个检查起不到任何作用。可以把main里的getday();改写成if(getday()==0){printf("Date error");return;}意思是如果输入的日期不正常的话打出出错信息并结束程序
主要功能包括
1.查询某年某月每一天对应星期几
2.可以查询某年某月的上个月或是下个月的日历情报
3.查询某年某月某日是这一年的第几天,并查询改天是星期几
4.判断该年是闰年还是平年,判断这一年的生肖
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void rili(int,int);
int runniansub(int);
int week(int,int,int);
int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{ struct date d;
getdate(&d);
printf("今天是:%d年 %d月 %d日\n",d.da_year,d.da_mon,d.da_day);
rili(d.da_year,d.da_mon-1);
system("pause");
return 0; }
void rili(int year,int month)
{ int d,w,i,s,c,p;
char x;
while(month>11) {
month-=12;
year++; }
while(month<0) { year--;
month+=12; }
if(runniansub(year)) monthday[1]=29;
printf("%d年%d月\n",year,month+1);
printf("S M T W T F S\n");
s=week(year,month+1,1);
for(i=0; i<3*s; i++ ) printf(" ");
c=s;
for(d=1;d<=monthday[month];d++) {
if(c>6) {
printf("\n");
c=0; }
c++;
printf("%d",d);
printf("%s",(d<10)?" ":" "); }
printf("\n");
printf("下月N,上月P,退出Q");
x=getch();
printf("%c\n",x);
if(x=='N' || x=='n') { rili(year,month+1); }
else if(x=='P' || x=='p') { rili(year,month-1); }
else { printf("感谢使用!88\n"); return;} }
int runniansub(int year)
{ return ((year%4==0 && year%100!=0) || (year%4==0 && year%400==0)); }
int week(int y,int m,int d)
{ if((m==1)||(m==2)) {
m+=12;
y--; }
return ((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)+1)%7;
#include <stdio.h>
int IsLeapYear(int); /*定义子函数*/
main()
{int i,day,year,temp,temp_i; /*定义变量*/
long int Year_days=0;
int Year_Start=1;
int Per_Year_Days;
int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};/*定义
月份*/
printf("We know that 2000.01.01 is saturday!\n");
printf("Please enter the year and month:"); /*提示信息*/
while(1)
{scanf("%d %d",&year,&temp);
if(getchar()=='q')
exit(0);
while(Year_Start < year)
{ if(IsLeapYear(Year_Start)) /*调用IsLeapYear函数判断是否为
闰年*/
Per_Year_Days=366; /*闰年一年366天*/
else
Per_Year_Days=365; /*平年一年365天*/
Year_days=Year_days + Per_Year_Days;
Year_Start++;
} /*输出每个月的日历*/
// for(temp=1;temp<=12;temp++)
{switch(temp)
{ case 1: printf("January (%d) \n",year);break;
case 2: printf("February (%d) \n",year); break;
case 3: printf("March (%d) \n",year); break;
case 4: printf("April (%d) \n",year); break;
case 5: printf("May (%d) \n",year); break;
case 6: printf("June (%d) \n",year);break;
case 7: printf("July (%d) \n",year);break;
case 8: printf("Auguest (%d) \n",year); break;
case 9: printf("September (%d) \n",year);break;
case 10:printf("October (%d) \n",year);break;
case 11:printf("Novermber (%d) \n",year); break;
case 12:printf("December (%d) \n",year); break;
default:printf("error\n");
}
i=Year_days%7;
printf("Mon Tue Wed Thu Fri Sat Sun\n"); /*星期提示信息*/
if(i!=0)
for(temp_i=0;temp_i<i;temp_i++)
printf(" ");
day=1;
if(IsLeapYear(year)&&temp==2)
while(day<=month_day[12])
{if(day>1)
if(Year_days%7==0)
printf("\n");
if(day>=10)
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;}
else
while(day<=month_day[temp-1])
{ if(day>1)
if(Year_days%7==0)
printf("\n");
if(day>=10)
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++; } /*依次输入日期*/
printf("\n") ;}}}
int IsLeapYear(int year) /*判断是否是闰年*/
{ if((year%4==0)&&(year%100!=0)||(year%400==0))
return 1;
else
return 0;}}
#include <stdio.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; //判断二月是29天还是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()
{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<=4*a)
{printf(" ");
j++; }
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7*k-a)
{printf("\n");
k++; } } }
printf("\n"); }
int day,year,month;
int isLeap()
{int isleap;
if(((year%4==0)&&!(year%100==0))||(year%400==0))isleap=1;
else isleap=0;
return isleap;}
int lastdayofmonth()
{int lastday,isleap;
switch (month)
{case 1: case 3: case 5: case 7:case 8: case 10: case 12: lastday=31;break;
case 4: case 6: case 9: case 11: lastday=30;break;
case 2: {isleap=isLeap();
if (isleap==1) lastday=29;
else lastday=28;}}
return lastday;}
int ValidDate()
{int valid;
int yearOK,monthOK,dayOK;
if ((year>1811)&&(year<=2012)) yearOK=1;
else yearOK=0;
if ((month>0)&&(month<=12)) monthOK=1;
else monthOK=0;
if ((day>0)&&(day<=lastdayofmonth())) dayOK=1;
else dayOK=0;
if(yearOK && monthOK && dayOK) valid=1;
else valid=0;
return valid;}
int getday(){printf("Enter a month/a day/a year");scanf("%d %d %d",&month,&day,&year);
return ValidDate();}
void incrementdate()
{if (day<lastdayofmonth()) day+=1;
else
{day=1;
if (month==12)
{month=1;year+=1;}
else month+=1;}}
void print()
if (ValidDate())printf("nextday is %d/%d/%d",month,day,year);elseprintf("Date error");}
void main(){getday();incrementdate();print();}
补充一下,又发现一个问题。输入之后虽然你做了输入检查,但是现在的写法,这个检查起不到任何作用。可以把main里的getday();改写成if(getday()==0){printf("Date error");return;}意思是如果输入的日期不正常的话打出出错信息并结束程序
主要功能包括
1.查询某年某月每一天对应星期几
2.可以查询某年某月的上个月或是下个月的日历情报
3.查询某年某月某日是这一年的第几天,并查询改天是星期几
4.判断该年是闰年还是平年,判断这一年的生肖
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void rili(int,int);
int runniansub(int);
int week(int,int,int);
int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{ struct date d;
getdate(&d);
printf("今天是:%d年 %d月 %d日\n",d.da_year,d.da_mon,d.da_day);
rili(d.da_year,d.da_mon-1);
system("pause");
return 0; }
void rili(int year,int month)
{ int d,w,i,s,c,p;
char x;
while(month>11) {
month-=12;
year++; }
while(month<0) { year--;
month+=12; }
if(runniansub(year)) monthday[1]=29;
printf("%d年%d月\n",year,month+1);
printf("S M T W T F S\n");
s=week(year,month+1,1);
for(i=0; i<3*s; i++ ) printf(" ");
c=s;
for(d=1;d<=monthday[month];d++) {
if(c>6) {
printf("\n");
c=0; }
c++;
printf("%d",d);
printf("%s",(d<10)?" ":" "); }
printf("\n");
printf("下月N,上月P,退出Q");
x=getch();
printf("%c\n",x);
if(x=='N' || x=='n') { rili(year,month+1); }
else if(x=='P' || x=='p') { rili(year,month-1); }
else { printf("感谢使用!88\n"); return;} }
int runniansub(int year)
{ return ((year%4==0 && year%100!=0) || (year%4==0 && year%400==0)); }
int week(int y,int m,int d)
{ if((m==1)||(m==2)) {
m+=12;
y--; }
return ((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)+1)%7;
#include <stdio.h>
int IsLeapYear(int); /*定义子函数*/
main()
{int i,day,year,temp,temp_i; /*定义变量*/
long int Year_days=0;
int Year_Start=1;
int Per_Year_Days;
int month_day[]={31,28,31,30,31,30,31,31,30,31,30,31,29};/*定义
月份*/
printf("We know that 2000.01.01 is saturday!\n");
printf("Please enter the year and month:"); /*提示信息*/
while(1)
{scanf("%d %d",&year,&temp);
if(getchar()=='q')
exit(0);
while(Year_Start < year)
{ if(IsLeapYear(Year_Start)) /*调用IsLeapYear函数判断是否为
闰年*/
Per_Year_Days=366; /*闰年一年366天*/
else
Per_Year_Days=365; /*平年一年365天*/
Year_days=Year_days + Per_Year_Days;
Year_Start++;
} /*输出每个月的日历*/
// for(temp=1;temp<=12;temp++)
{switch(temp)
{ case 1: printf("January (%d) \n",year);break;
case 2: printf("February (%d) \n",year); break;
case 3: printf("March (%d) \n",year); break;
case 4: printf("April (%d) \n",year); break;
case 5: printf("May (%d) \n",year); break;
case 6: printf("June (%d) \n",year);break;
case 7: printf("July (%d) \n",year);break;
case 8: printf("Auguest (%d) \n",year); break;
case 9: printf("September (%d) \n",year);break;
case 10:printf("October (%d) \n",year);break;
case 11:printf("Novermber (%d) \n",year); break;
case 12:printf("December (%d) \n",year); break;
default:printf("error\n");
}
i=Year_days%7;
printf("Mon Tue Wed Thu Fri Sat Sun\n"); /*星期提示信息*/
if(i!=0)
for(temp_i=0;temp_i<i;temp_i++)
printf(" ");
day=1;
if(IsLeapYear(year)&&temp==2)
while(day<=month_day[12])
{if(day>1)
if(Year_days%7==0)
printf("\n");
if(day>=10)
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++;}
else
while(day<=month_day[temp-1])
{ if(day>1)
if(Year_days%7==0)
printf("\n");
if(day>=10)
printf("%d ",day);
else
printf("%d ",day);
Year_days++;
day++; } /*依次输入日期*/
printf("\n") ;}}}
int IsLeapYear(int year) /*判断是否是闰年*/
{ if((year%4==0)&&(year%100!=0)||(year%400==0))
return 1;
else
return 0;}}
#include <stdio.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; //判断二月是29天还是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()
{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<=4*a)
{printf(" ");
j++; }
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7*k-a)
{printf("\n");
k++; } } }
printf("\n"); }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询