打印日历c语言
‘壹’ 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 <string.h>
bool isLeapYear(int);
int getWeekDay(int, int, int);
int main(int argc, char const *argv[])
{
int year;
int month;
printf("输入日期(yyyy-mm):");
scanf("%d-%d-%d", &year, &month);//输入年和月
printf(" %d年%d月\n", year, month);//输出日历日历标题
printf("日 一 二 三 四 五 六\n");//输出星期
//本函数获取某个日期是星期几,这里获取每月的1号是星期几,返回是是[0-7]对应(星期天-星期六)
int week = getWeekDay(year, month, 1);
int i;
for (i = 0; i < week; i++)//每月第一周前几天用空格填充
printf(" ");
int endDayOfMonth;//每个月的最后一天
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
endDayOfMonth = 31;
else if (month == 2)
{
if (isLeapYear(year))
endDayOfMonth = 29;
else
endDayOfMonth = 28;
}
else
endDayOfMonth = 30;
int dayOfMonth = 1;
//从一号循环到本月末
while (dayOfMonth <= endDayOfMonth)
{
printf("%2d ", dayOfMonth);
dayOfMonth++;
//每输出完星期六的日期后就换行(对应周数起点是从week开始的,所以要在天数上加上week)
if ((dayOfMonth + week- 1) % 7 == 0)
printf("\n");
}
printf("\n");
return 0;
}
//判断闰年
bool isLeapYear(int y)
{
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return true;
return false;
}
//获取某天是星期几返回值(0-7)
int getWeekDay(int y, int m, int d)
{
//计算星期几的固定公式,Y是年D是这个日期在本年的天数
//W = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D
int days = d;
int i;
for (i = 1; i < m; i++)
{
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
days += 31;
else if (i == 2)
{
if (isLeapYear(y))
days += 29;
else
days += 28;
}
else
days += 30;
}
int temp = y - 1 + (int)((y - 1) / 4) - (int)((y - 1) / 100) + (int)((y - 1) / 400) + days;
return temp % 7;
}
‘叁’ 帮忙用C语言编一个“日历打印程序” 需要源代码
你好
#include "stdio.h"
void CaculateWeekDay(int y,int m, int d)
{
if(m==1||m==2) {
m+=12;
y--;
}
int iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
switch(iWeek)
{
case 0: printf("星期一\n"); break;
case 1: printf("星期二\n"); break;
case 2: printf("星期三\n"); break;
case 3: printf("星期四\n"); break;
case 4: printf("星期五\n"); break;
case 5: printf("星期六\n"); break;
case 6: printf("星期日\n"); break;
}
}
void main()
{
int year=0,month=0,day=0;
printf("请输入日期:\n格式为:1900,1,1\n");
char temp = '1';
while (temp != '0')
{
scanf("%d,%d,%d",&year,&month,&day);
scanf("%c",&temp);
CaculateWeekDay(year,month,day);
printf("输入0退出,其他继续:");
scanf("%c",&temp);
}
} 希望对你有帮助.....
‘肆’ 用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;
}