❶ 请问写一个日历的c语言程序,代码怎么写
#include<conio.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
constmonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
intisLeap(intyear)
{
if(year%4)return0;
if(year%400)return1;
if(year%100)return0;
return1;
}
intgetWeek(intyear,intmonth,intday)
{
intc,y,week;
if(month==1||month==2)//判断month是否为1或2
{
year--;
month+=12;
}
c=year/100;
y=year-c*100;
week=(c/4)-2*c+(y+y/4)+(13*(month+1)/5)+day-1;
while(week<0){week+=7;}
week%=7;
returnweek;
}
voiddisplay(intyear,intmonth)
{
intmonthDays,weekFirst,i;
monthDays=monthDay[month]+(month==2?isLeap(year):0);
weekFirst=getWeek(year,month,1);
system("cls");
printf("-------%4d年----%2d月------- ",year,month);
printf("星期日星期一星期二星期三星期四星期五星期六 ");
for(i=0;i<weekFirst;i++)printf("");
for(i=1;i<=monthDays;i++)
{
printf("%8d",i);
weekFirst++;
if(weekFirst>=7){printf(" ");weekFirst=0;}
}
}
voidmain()
{
intyear,month,chr;
time_ttimer;
structtm*tblock;
timer=time(NULL);
tblock=localtime(&timer);
year=tblock->tm_year+1900;
month=tblock->tm_mon+1;
while(1)
{
display(year,month);
chr=getch();
if(chr==0xe0)
{
chr=getch();
if(chr==0x4b)/*方向键(←)*/
{
month--;
if(month<1){month=12;year--;}
}
elseif(chr==0x4d)/*方向键(→)*/
{
month++;
if(month>12){month=1;year++;}
}
}
elseif(chr=='q'||chr=='Q')break;
}
}
❷ C语言编写日历
#include <stdio.h>
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void print_calendar(int y);
void main()
{
int y;
printf("Calendar system :\n");
printf("Please input a year between 1600 & 2500:");
while(scanf("%d",&y)!=EOF)//输入年份
{
if (y<1600 || y>2500)
{
printf("Invalid input. Please try again!\n");
printf("Please input a year between 1600 & 2500:");
continue;
}
print_calendar(y);
}
}
void print_calendar(int y) //打印日历
{
int i,sum,days,k,m;
char monthname[12][20] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
if((0==y%4&&y%100!=0)||0==y%400 ) //闰年判断
month[2]=29;
for(m=1;m<13;m++)
{
days = 0;
for (i=1;i<m;i++) //计算m月1号是第几天
days+=month[i];
days++;
sum=y-1+(y-1)/4-(y-1)/100+(y-1)/400+days;
k=sum%7; //k是星期几,k=0星期日
printf("%s %d\n",monthname[m-1],y);
printf(" S M Tu W Th F S\n");
printf("%*d",3*(k+1)-1,1);//先输出3*(k+1)-1个空格,在输出1
k++;
for(i=2;i<=month[m];i++)
{
if(k == 7)
{
printf("\n");
k=0;
}
if (k == 0)
printf("%2d",i);
else
printf("%3d",i);
k++;
}
printf("\n\n");
}
}
❸ 用C语言怎么编写万年历
C语言编写万年历的源代码如下:
#include <stdio.h>
int week(int y,int m,int d);
int main()
{
char wk[7][3] = {{"天"},{"一"},{"二"},{"三"},{"四"},{"五"},{"六"}};
int year, month, day, w;
scanf("%d %d %d", &year, &month, &day);
w = week(year, month, day);
printf("%d年%d月%d日 星期%s", year, month, day, wk[w]);
}
int week(int y,int m,int d)
{
static int r[13]={0,0,3,3,6,1,4,6,2,5,0,3,5};
int c,w;
y %= 400;
if ((y==0||y%4==0&&y%100!=0)&&m<3)
c = 5;
else
c = 6;
w = (y+y/4-y/100+r[m]+d+c)%7;
return w;
}
(3)c语言编写日历扩展阅读
电子时钟C语言版的源代码如下:
#include<stdio.h>
#include<time.h>
#include<string.h>
int main()
{
char str[50];
time_t t;
struct tm *nt;
while(1)
{
system("cls");//清屏函数
t=time(NULL);
nt=localtime(&t);
strftime(str,50,"%Y-%m-%d %H:%M:%S “,nt);
printf("现在时刻:%s ",str);
}
getch();
return 0;
}
❹ C语言编写年历
只找到这个打印的,
你自己看看,
#include <stdio.h>
void main()
{
int i,j,k,year,week;
int day[]={31,28,31,30,31,30,31,31,30,31,30,31};
char month[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};
printf("Input year you want to print:");
scanf("%d",&year);
if((year%4==0&&year%100!=0)||(year%400==0)) day[1]++; //判断是否闰年
week=(35+year+year/4-year/100+year/400)%7; //计算当年的1月1日是周几的公式
for(i=0;i<12;i++)
{
printf("\n%s\n",month[i]); //输出月份的英文
printf("Sun Mon Tue Wed Thu Fri Sat\n"); //输出表头
for(k=0;k<week;k++) printf(" "); //每月一号对齐它的星期数
for(j=1;j<=day[i];j++)
{
printf("%3d ",j);
if(++week>=7)
{
printf("\n");
week=week%7;
}
}
printf("\n");
}
}
❺ 求C语言编日历源代码的详细说明
/*
稍微改了下对齐格式,加了注释
*/
/*
1、闰年的算法:
如果某年能被4整除但不能被100整除,
或者能被400整除,
则该年是闰年.
用表达式表示就是
(year
%4
==
0
&&
year%100
!=
0)
||
(year%400
==
0)
2、计算某一天是星期几:
已知1900年的1月1号为星期一,
然后就可以用某一天和1900年的1月1号相差的天数对7取余来求星期,
本题是用的公元1年的1月1号作为基准
*/
#include
<stdio.h>
#include<conio.h>
#include<stdlib.h>
int
IsLeapYear(int);
//函数定义
void
main()
{
int
i;
int
day;
int
year;
int
temp;
int
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("Please
enter
the
year:
");
scanf("%d",&year);
//输入年份
while(Year_Start
<
year)
//从公元1年开始执行while循环,
该年的一月一号为星期一
{
if(
IsLeapYear(
Year_Start
)
)
Per_Year_Days
=
366;
//如果是闰年,
则一年有366天
else
Per_Year_Days
=
365;
//如果不是闰年,
则一年有365天
Year_days
=
Year_days
+
Per_Year_Days;
//Year_days为从公元1年到输入年份的前一年的天数的总和
Year_Start++;
}
for(
temp
=
1;
temp
<=12;
temp++
)
//temp从1到12,
对应一年内12个月
{
switch(
temp
)
//用switch语句将temp和12个月对应起来
{
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("
August(%d)\n",year);
//八月
break;
case
9:
printf("
September(%d)\n",year);
//九月
break;
case
10:
printf("
October(%d)\n",year);
//十月
break;
case
11:
printf("
November(%d)\n",year);
//十一月
break;
case
12:
printf("
December(%d)\n",year);
//十二月
break;
}
i
=
Year_days
%
7;
//每个星期有7天,
故用每年的天数对7取余
printf("Mon\tTue\tWed\tThu\tFri\tSat\tSun\n");
if(
i
!=
0
)
//如果余数不为零
for(
temp_i
=
0;
temp_i
<
i;
temp_i++)
printf("\t");
//则打印空格(这里用\t代替空格,
更加美观),
空格数为i
day
=
1;
//初始化day为1,
为下面的while循环做准备
if(
IsLeapYear(year)
&&
temp
==
2)
//如果输入的年份是闰年,
并且月份为2
while(
day
<=
month_day[12]
)
//day为一循环变量,
取值为1-365(闰年的话为1-366)
{
if(
day
>1
)
//如果天数大于一
if(
Year_days
%
7
==
0
)
//如果是星期日,
则换行
printf("\n");
if(
day
>=
10
)
printf("%d\t",day);
//打印天数+空格
else
printf("%d\t",day);
Year_days++;
day++;
}
else
//如果不满足"输入的年份是闰年,
并且月份为2"
while
(day
<=
month_day[temp-1])
{
if(
day
>
1
)
if(
Year_days
%
7
==
0
)
printf("\n");
if(
day
>=10
)
printf("%d\t",day);
else
printf("%d\t",day);
Year_days++;
day++;
}
printf("\n");
if(
getch()
==
'q'
)
//如果输入为q,
则退出程序
exit(0);
}
getch();
//每按一次键,
打印一个月份
}
int
IsLeapYear(
int
year
)
{
//判断是否是闰年,
是则返回1,
否则返回0
if
((year
%4
==
0)
&&
(year
%
100
!=
0)
||
(year
%
400
==
0)
)
return
1;
else
return
0;
}
❻ 用c语言编写日历,这段有哪位大神能详细解释一下吗😭
# include <stdio.h>
# include <math.h>
void printmonth(int m);
void printhead(int m);
int daysofmonth(int m);
int firstday(int y);
int year,weekday;
void main()
{
int i;
printf("请输入年份:");
scanf("%d",& year);
weekday=firstday(year);
printf("\n\n");
printf(" %d年\n",year);
for(i=1;i<=12;i++)
{
printmonth(i);
printf("\n");
}
printf("\n\n");
}
void printmonth(int m) //打印每月日历
{
int i,days;
printhead(m);
days=daysofmonth(m);
for(i=1;i<=days;i++)
{
printf("%5d",i);
weekday=(weekday+1)%7;
if (weekday==0) printf("\n ");
}
}
void printhead(int m) //打印每月的日历头(判定起始位置)
{
int i;
printf("\n%d月 日 一 二 三 四 五 六\n",m);
printf(" ");
for(i=0;i<weekday;i++)
printf(" ");
}
int daysofmonth(int m) //每月的天数
{
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:if (((year%4==0 && year%100!=0)||year%400==0))
return 29;
else
return 28;
default: return 0;
}
}
int firstday(int y) //判断某年元旦是星期几
{
double s ;
s=floor(year-1+(year-1)/4.0-(year-1)/100.0+(year-1)/400.0+1);
return (int)s%7;
}
❼ C语言程序编写日历
/*你好,这个问题我来回答,希望能被采纳:*//*函数功能输出year的日历*/#include<stdio.h>void
main(){int
a,b,c,d,e,f,i,j,k,n,m,year;printf("input
the
year:\n");
scanf("%d",&year);
//输入数据的初始化;b=year;c=year%4;d=year/4;e=d*1461+c*365;f=e%7;j=f;
//初始化Year年的第一天的星期;m=j;printf("***%d年的日历***\n",year);for(a=1;a<=12;a++)
//函数的主要部分,主要实现日历的打印部分
{
if(a==1||a==3||a==5||a==7||a==8||a==10||a==12)k=31;
else
if(a==4||a==6||a==9||a==11)k=30;
else
if((year%4==0&&year%100!=0)||(year%400==0))k=29;
else
k=28;
printf("%d月份\n
STAT
SUN
MON
TUE
WED
THUR
FRI\n",a);
m=j;
if(m<=5)m=m+1;
else
m=m-6;
for(n=1;n<=m;n++)printf("
");
for(i=1;i<=k;i++,j++)
{if(j==7)j=0;
if(i<10)printf("
%d",i);
else
printf("
%d",i);
if(j==5)printf("\n");
}
printf("\n\n\n");
}}
❽ C语言编写万年历
#include"stdio.h"
void print(int);//函数声明
int dobb(int);
int date(int,int,int);
void main()
{
int year=0;
printf("输入打印的年份(XXXX)\n");
scanf("%d",&year);
print(year);
}
int dobb(int year)//闰年判断
{
if((year%4==0 && year%100!=0)|| (year%400==0))
return(1);
else return(0);
}
int date(int year,int month,int day)//判断该年某月某日是星期几的计算
{
int leap,cn=0,sum;//cn计算的是从该年1月1日起,到达这一天的天数总和
leap=dobb(year);
switch(month-1)
{
case 11:cn+=30;
case 10:cn+=31;
case 9:cn+=30;
case 8:cn+=31;
case 7:cn+=31;
case 6:cn+=30;
case 5:cn+=31;
case 4:cn+=30;
case 3:cn+=31;
case 2:if(leap) cn+=29;
else cn+=28;
case 1:cn+=31;
default:cn+=day;
}
sum=year-1+(year-1)/4-(year-1)/100+(year-1)/400+cn;//这是核心的一条公式,返回值是星期几(没有为什么)
return(sum%7);
}
void print(int year)//打印的核心算法
{
int i,j,n,leap,days,k,count;
leap=dobb(year);//判断是否闰年
days=date(year,1,1);//计算该年第一天是星期几
printf("%d年\n",year);
for(i=1;i<=12;i++)
{
printf("%3d月份\n",i);
printf("\n");
printf("\t");
printf("%-8s%-8s%-8s%-8s%-8s%-8s%-8s\n","Sun","Mon","Tues","Wed","Thurs","Fri","Sat");
if(days==7)
{
days=0;n=days;
}
else n=days;
printf("\t");
for(j=1;j<=8*days;j++)
printf(" ");
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:count=31;break;
case 4:
case 6:
case 9:
case 11:count=30;break;
case 2:if(leap) count=29;
else count=28; break;
}
for(k=1;k<=count;k++)//以下代码是控制界面的对齐
{
printf("%-8d",k);
n++;
if(n==7)
{printf("\n");printf("\t");n=0;}
}
printf("\n\n");
days=n;
}
}
❾ 如何用C语言编写日历程序
这个程序基本上给你写出来了
由于操作比较机械化
所以一些重复的机械操作我(两个switch中的省略了一些)
我是以2008年1月1日为基准的
那天是星期二
可以查找从那天开始以后的89年中的任意一天
有于 int型的取值范围所限定的
你自己看看程序把 很简单
#include<stdio.h>
void lab()
int xinqi()
//计算星期
xinqi(int y,int m,int d)
{
int a,b,star;
int a[12]={31,59,90,120,151,181,212,243,273,304,334,365};
switch(m)
{
case 1: a=m;
case 2: a=m+31;
case 3: a=m+59;
//后面以此类推
}
b=(y-2008)/4;
a=365*(y-2008)+b;
star=a%7+2;
switch(star)
{
case 1: printf("Mon");
case 2: printf("Tue");
//下面以此类推
}
}
//计算天数并输出相应日期
lab(int y,int m,int d)
{
int a[12]={31,59,90,120,151,181,212,243,273,304,334,365};
int x,run,flag;
if((x-y)%4==0) run=1;
else run=0;
switch(m)
{
case 1:{ flag=a[m]; printf("Jan,%dth,%d",d,y); xinqi(y,m,d);}
case 2:{ flag=a[m]; printf("Feb,%dth,%d",d,y); xinqi(y,m,d);}
case 3:{ flag=a[m]+run; printf("Mar,%dth,%d",d,y); xinqi(y,m,d);}
//后面依次类推
}
}
}
void main()
{
int y,m,d;
printf("输入:");
scanf("%d %d %d ",&y,&m,&d);
lab(y,m,d);
}
❿ 用C语言编写程序打印任意年份的日历
#include <stdio.h>
int JgYr(int yr)
{
if (yr%4==0&&yr%100!=0||yr%100==0&&yr%400==0)
return 1;
else
return 0;
}
int CalWkd(int yr,int mth)
{
int ds=0,i,rst,wkd;
int mthd[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if (JgYr(yr))
mthd[2]=29;
for (i=1;i<mth;i++)
ds+=mthd[i];
ds+=1;
rst=yr-1+(int)((yr-1)/4)-(int)((yr-1)/100)+(int)((yr-1)/400)+ds;
wkd=rst%7;
return wkd;
}
showcld(int wkd,int mtd)
{
int dt=1,i,j=0;
printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
for (i=0;i<wkd;i++)
{
printf("\t");
j++;
}
while (j!=7)
{
printf("%d",dt);
if (j<6)
printf("\t");
else
printf("\n");
dt++;
j++;
}
while (1)
{
for (j=0;j<7;j++)
{
printf("%d",dt);
if (j!=6)
printf("\t");
else
printf("\n");
if (dt!=mtd)
dt++;
else
return 0;
}
}
}
int main()
{
int yr,mth,mtd,wkd,mthd[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("Input the Year and Month(Year-Month):");
scanf("%d-%d",&yr,&mth);
if (yr<0||mth<1||mth>12)
return 0;
if (JgYr(yr))
mthd[2]=29;
mtd=mthd[mth];
wkd=CalWkd(yr,mth);
showcld(wkd,mtd);
return 0;
}