❶ 請問寫一個日歷的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;
}