當前位置:首頁 » 編程語言 » 用c語言做一個教學日歷表
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

用c語言做一個教學日歷表

發布時間: 2023-08-21 04:05:24

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語言 高手們 幫幫我吧 做一個日歷 輸入 年月 輸出 該年該月的 日歷

#include<stdio.h>
#include<time.h>
main(){

//定義變數
struct tm t,*date;

time_t t_day;

int year,month;
int month_day;
int i,j;

struct tm *local;
time_t t_mow;
t_mow=time(NULL);
local=gmtime(&t_mow);

//輸入年份和月份
printf("請輸入年份:\n");
scanf("%d",&year);
printf("請輸入月份:\n");
scanf("%d",&month );

if(month==2 ){
if(year%4==0 && year%100!=0)
month_day=29;
else
month_day=28;

}
else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
month_day=31;
}
else
month_day=30;

printf("當前時間是: ");
printf(asctime(local));
printf("\n\n");

printf("當月天數是:%d \n\n 日歷\n",month_day); //輸出當前天數

printf("一\t二\t三\t四\t五\t六\t日\t\n"); //日歷的第一行,顯示一二三四五六日

for (i=1;i<=month_day;i++)
{
t.tm_year=year-1900;
t.tm_mon=month-1;
t.tm_mday=i;
t.tm_hour=0;
t.tm_min=0;
t.tm_sec=0;
t.tm_isdst=0;
t_day=mktime(&t);

date=gmtime(&t_day);

if(i==1)
{
for(j=0;j<date->tm_wday;j++)
printf("\t");
}

if(date->tm_wday==6)
{
printf("%d\n",i);
}
else
printf("%d\t",i);

}
printf("\n\n",i);
}

Ⅲ 請問寫一個日歷的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語言編寫日歷程序

這個程序基本上給你寫出來了
由於操作比較機械化
所以一些重復的機械操作我(兩個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語言怎麼編寫萬年歷

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;

}

(5)用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>
# 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;
}