c语言编写万年历

编写一个万年历
主要功能:
(1)用户输入年份和月份,屏幕显示此月的月历。若年份为0,输出整年的月历。
(2)保存:用户可输入文件名,将此月历存入文件中。

第1个回答  2009-07-06
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
typedef struct //定义一个接受输入日期的结构体
{
int year;
int month;
int day;
}time2;
/*打印菜单*/
void menu()
{
system("cls");
printf("||===========================================||\n");
printf(" 1、输出系统所对应的本月月历。\n");
printf(" 2、查找某年某月。输出其对应的月历。\n");
printf(" 3、查找具体日期是星期几?\n");
printf(" 0、退出本程序。\n");
printf("||===========================================||\n");
}
int year_leap(int year)//判断输入的年是否是闰年?
{
int i=0;
if(year%4==0&&year%100!=0||year%400==0)
i=1; //是闰年 为真。
return i;
}
int month_day(int year,int month)
{
int i;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)//判断本月是否为大月?
return i=31;
if(month==4||month==6||month==9||month==11) //判断本月是否为小月?
return i=30;
if(month==2&&year_leap(year)) //判断本月是否为闰年的二月?
return i=29;
else
return i=28;
}
/*查找每一天所对应的是星期几?*/
int day_search(int year,int month,int day)
{
int c=0;
float s;
int m;
for(m=1;m<month;m++)
c=c+month_day(year,m);
c=c+day;
s=year-1+(float)(year-1)/4+(float)(year-1)/100+(float)(year-1)/400-40+c;
return ((int)s%7); //返回要查找的日期是星期几,
}
/*判断输入的年月是否符合习惯*/
time2 panduan_year_month(int year,int month)
{
time2 b;
b.year=year;
b.month=month;
if(year<0)
{
system("cls");
printf("你输入的年份有错!!!\n请重新输入年份:"); //判断输入的year是否符合习惯?
scanf("%d",&b.year);
}
if(month>12||month<1) //判断输入的month是否符合习惯?
{
system("cls");
printf("输入的月份有错!!!\n请重新输入月份:");
scanf("%d",&b.month);
}
return b;
}
//输出所查找的那天是星期几?
void print_weekday()
{
FILE *fp;
char string[7];
time2 time,b;
system("cls");
printf("请输入你要查找的年月日:");
scanf("%d%d%d",&time.year,&time.month,&time.day); //输入年月日
if((fp=fopen("weekday.txt","r"))==NULL) //判断能否打开文件
{
printf("cannot open this file\n");
exit(0);
}
b=panduan_year_month(time.year,time.month);
if(time.day<1||time.day>month_day(time.year,time.month)) //判断输入的day是否符习惯,若不,则重新输入。
{
system("cls");
printf("你输入的日期有错!!!\n请重新输入:");
scanf("%d",&b.day);
}
else
b.day=time.day;
time=b;
fseek(fp,6*day_search(time.year,time.month,time.day),0);
fread(string,6,1,fp);
string[6]='\0';
printf("%d/%d/%d/是%s\n",time.year,time.month,time.day,string);
fclose(fp);
getch();
}
/*打印一个月的月历。*/
void print_month(int a,int b)
{
int i,j=1,k=1;
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");
}
/*打印系统当月的月历*/
void print_system_month()
{
int a,b;
system("cls");
/*调用系统时间*/
time_t tval;
struct tm *now;
tval=time(NULL);
now=localtime(&tval);
printf("现在时间: %4d年 %d月 %02d日\n",now->tm_year+1900, now->tm_mon+1, now->tm_mday);//结束调用系统时间,并输出当前的系统月历。
b=month_day(now->tm_year+1900,now->tm_mon+1);
a=day_search(now->tm_year+1900,now->tm_mon+1,1);
print_month(a,b);
getch();
}

/*输出要查找的年月所对应的月历。*/
void print_any_month()
{
time2 time;
int a,b;
system("cls");
printf("请输入你要查找的年份:");
scanf("%d%d",&time.year,&time.month);
time=panduan_year_month(time.year,time.month); //判断输入的月份是否正确?若不正确,那就重新输入,并把修改后的结果赋给time
b=month_day(time.year,time.month);
a=day_search(time.year,time.month,1);
system("cls");
print_month(a,b); //输出月历
getch();
}

void print_xx() //输出欢迎用语!
{
system("cls");
printf("||================================欢迎下次光临!===============================||\n");
}

void choice() //选择功能选项
{
int i;
while(1)
{
menu();
printf("请输入你要选择的功能所对应的数:\n");
i=getch();
if(i=='0') break; //判断输入的数是否为零,若是,退出循环。
switch(i)
{
case '1': print_system_month();break;
case '2': print_any_month();break;
case '3': print_weekday();break;
default: printf("没有你要选择的功能选项!!!!!\n");getch();
}
}
print_xx(); //调用函数输出,致谢语。
}

/*在主函数中通过choice()函数调用其他功能函数。*/
void main()
{
choice();
printf("\n");
}
第2个回答  2009-07-05
我也要啊
第3个回答  2009-07-04
程序我有,给我发一封邮件:

[email protected]

但你所说的那个属于0就输出整年的月历没有实现。没有实现保存。

是我前几年自己写的万年历。
相似回答