c语言设计万年历 输入任意年份,输出该年12个月份 输入任意年份月份,输出该年月日历
展开全部
程序直接输入年份和月份,打印对应日历。
一年12个月是固定的,我就不写单独输入年份打印月份了。你想就把我月份数组打印一遍好了。
#include<stdio.h>
#include<time.h>
#include<malloc.h>
enum MONTH{January=1,February,March,April,May,June,July,August,September,October,November,December};
struct tm * getDate(int year,int mon);
void printTime(time_t theTime,int year);
int main()
{
enum MONTH mon;
time_t theTime;
struct tm *date;
int year,m;
while(1)
{
printf("输入任意年份及月份:\n");
scanf("%d%d",&year,&m);
if(m<1 || m>12)
{
printf("月份超出范围,重新输入!\n");
continue;
}
mon=m;
break;
}
date=getDate(year,mon);
theTime=mktime(date);//从1900年至此的秒数
printTime(theTime,year);
return 0;
}
struct tm * getDate(int year,int mon)
{
struct tm *date=(struct tm *)malloc(sizeof(struct tm));
date->tm_sec=0;
date->tm_min=0;
date->tm_hour=0;
date->tm_year=year-1900;
date->tm_mon=mon-1;
date->tm_mday=1;
date->tm_yday=0;
return date;
};
void printTime(time_t theTime,int year)
{
int y,m,wday,i,j;
char wd[7][7]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
struct tm *date;
date =localtime(&theTime);
y=date->tm_year+1900;
m=date->tm_mon+1;
wday=date->tm_wday;
printf("\n------------------开始生成日历----------------------\n\n");
printf(" %d年%02d月\n",y,m);
i=0;
for(j=0;j<7;j++)
{
printf("%s ",wd[wday++]);
if(wday>6)
wday=0;
}
printf("\n");
while(1)
{
if(date->tm_mon+1>m)
{
printf("\n");
break;
}
printf(" %02d日 ",date->tm_mday);
if(++i==7)
{
printf("\n");
i=0;
}
theTime+=24*60*60;
date =localtime(&theTime);
}
}
追问
为啥我复制你的显示有错误呢?
为啥我复制你的显示有错误呢?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询