Ⅰ 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;
}