列印日歷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;
}