#include<stdioh>
#include<timeh>
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;
else return 28;
}
int firstday(int month,int year)//判断年
{
int w;
w=(1+2month+3(month+1)/5+year+year/4+year/400-year/100)%7+1;
return w;
}
main()
{
//调用系统时间
time_t tval;
struct tm now;
tval = time(NULL);
now = localtime(&tval);
printf("现在时间: %4d年 %d月 %02d日 %d:%02d:%02d\n", now->tm_year+1900, now->tm_mon+1, now->tm_mday,
now->tm_hour, now->tm_min, now->tm_sec);
//调用结束
int i,j=1,k=1,a,b,month,year;
b=days_month(now->tm_mon+1,now->tm_year+1900);
a=firstday (now->tm_mon+1,now->tm_year+1900);
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<=4a)
{
printf(" ");
j++;
}
for(i=1;i<=b;i++)
{
printf("%4d",i);
if(i==7k-a)
{
printf("\n");
k++;
}
}
}
printf("\n");
}
#include <stdioh>
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+2month+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<=4a)
{printf(" ");
j++;
}
for(i=1;i<=b;i++)
{printf("%4d",i);
if(i==7k-a)
{printf("\n");
k++;
}
}
}
printf("\n");
}
程序直接输入年份和月份,打印对应日历。
一年12个月是固定的,我就不写单独输入年份打印月份了。你想就把我月份数组打印一遍好了。
#include<stdioh>#include<timeh>
#include<malloch>
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+=246060;
date =localtime(&theTime);
}
}
//我写的代码,我自己改吧。当年水平有限,忘了考虑闰年,楼主莫怪
没有纯c环境,不知道还有没有错
#include <stdioh>
#include <timeh>
int days[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
char weekday[7][7]={"一","二","三","四","五","六","七"};
char weeks[][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
//判闰年
inline int leap(int year)
{
return (year%4==0&&year%100!=0)||year%400==0;
}
//判合法性
inline int legal(int y,int m,int d){
if(m<0||m>12)
return 0;
if(m==2)
return d>0 && d<=28+leap(y);
return d>0 && d<=month[m-1];
}
int week(int y,int m,int d)
{
int day = days[m-1]+d-1+(m>2leap(y):0);
return (y-1+(y-1)/4-(y-1)/100+(y-1)/400+day)%7;
}
void print()
{
puts("1Week(查询某天是星期几)");
puts("2Month(打印某月日历)");
puts("3Calendar(打印某年日历)");
puts("4Exit(退出)");
}
void printmonth(int y,int m)
{
int i,j,w;
w=(week(y,m,1)+1)%7;
puts(" SUN MON TUE WED THU FRI SAT");
for(i=1;i<=w;i++)
printf("%5c",' ');
for(j=i;j<i+month[m-1]+(m==2&&leap(y));j++)
{
printf("%5d",j-i+1);
if(j%7==0)
puts("");
}
if((j-1)%7)
puts("");
}
int main()
{
int i,y,m,d,c;
char op[5],s[105];
time_t current = time(NULL);
tm date = localtime(¤t);
print();
while(scanf("%d",&c)==1)
{
if(c==1)
{
strftime(s,100,"%Y %m %d",date);
do
{
printf("请输入年月日(例如:%s)\n",s);
scanf("%d%d%d",&y,&m,&d);
printf("%44d年%22d月%22d日是星期%s,是否继续查询(Y/N)?\n",y,m,d,weekday[week(y,m,d)]);
scanf("%s",op);
}while(op[0]=='Y'||op[0]=='y');
}
if(c==2)
{
strftime(s,100,"%Y %m",date);
do
{
printf("请输入要打印的月份(例如:%s)\n",s);
scanf("%d%d",&y,&m);
printf(" %44d年%22d月\n",y,m);
printmonth(y,m);
puts("是否继续打印(Y/N)?");
scanf("%s",op);
}while(op[0]=='Y'||op[0]=='y');
}
if(c==3)
{
strftime(s,100,"%Y",date);
do
{
printf("请输入要打印的年份(例如:%s)\n",s);
scanf("%d",&y);
printf(" %44d\n",y);
for(i=1;i<=12;i++)
{
printf(" %22d月\n",i);
printmonth(y,i);
}
puts("是否继续打印(Y/N)?");
scanf("%s",op);
}while(op[0]=='Y'||op[0]=='y');
}
if(c==4)
{
puts("是否要真的退出(Y/N)?");
scanf("%s",op);
if(op[0]=='Y'||op[0]=='y')
return 0;
}
print();
}
}
以上就是关于万年历(C语言编程)全部的内容,包括:万年历(C语言编程)、C语言程序设计万年历、c语言设计万年历 输入任意年份,输出该年12个月份 输入任意年份月份,输出该年月日历等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)