程序可以實現如下三種功能:
求某個日期對應的星期
求某年某月有的天數
輸出某年的日歷.
例如,打印2006年日歷如下:
--------------------------------------------------------------------------
2006 年
--------------------------------------------------------------------------
一 月 二 月
周日 周一 周二 周三 周四 周五 周六 周日 周一 周二 周三 周四 周五 周六
1 3 5 7 9 11 13 1 3 5 7
14 15 16 17 18 19 20 8 9 10 11 12 13 14
21 22 23 24 25 26 27 15 16 17 18 19 20 21
28 29 30 31 22 23 24 25 26 27 28
三 月 四 月
周日 周一 周二 周三 周四 周五 周六 周日 周一 周二 周三 周四 周五 周六
1 3 5 7 1
8 9 10 11 12 13 14 2 3 4 5 6 7 8
15 16 17 18 19 20 21 9 10 11 12 13 14 15
22 23 24 25 26 27 28 16 17 18 19 20 21 22
29 30 31 23 24 25 26 27 28 29
30
五 月 六 月
周日 周一 周二 周三 周四 周五 周六 周日 周一 周二 周三 周四 周五 周六
1 3 5 7 9 11 1 3 5
12 13 14 15 16 17 18 6 7 8 9 10 11 12
19 20 21 22 23 24 25 13 14 15 16 17 18 19
26 27 28 29 30 31 20 21 22 23 24 25 26
27 28 29 30
七 月 八 月
周日 周一 周二 周三 周四 周五 周六 周日 周一 周二 周三 周四 周五 周六
1 1 3 5 7 9
2 3 4 5 6 7 8 10 11 12 13 14 15 16
9 10 11 12 13 14 15 17 18 19 20 21 22 23
16 17 18 19 20 21 22 24 25 26 27 28 29 30
23 24 25 26 27 28 29 31
30 31
九 月 十 月
周日 周一 周二 周三 周四 周五 周六 周日 周一 周二 周三 周四 周五 周六
1 3 1 3 5 7 9 11 13
4 5 6 7 8 9 10 14 15 16 17 18 19 20
11 12 13 14 15 16 17 21 22 23 24 25 26 27
18 19 20 21 22 23 24 28 29 30 31
25 26 27 28 29 30
十一 月 十二 月
周日 周一 周二 周三 周四 周五 周六 周日 周一 周二 周三 周四 周五 周六
1 3 5 7 1 3
8 9 10 11 12 13 14 4 5 6 7 8 9 10
15 16 17 18 19 20 21 11 12 13 14 15 16 17
22 23 24 25 26 27 28 18 19 20 21 22 23 24
29 30 25 26 27 28 29 30 31
原代碼如下:
一,非打印版(:即只能輸出到屏幕,不可輸出到文件)
/*萬年歷全集:程序可以實現如下三種功能:
求某個日期對應的星期
求某年某月有的天數
輸出某年的日歷
*/
/*2006-1-2 梁見斌*/
#include
#include
struct mon
{
int maxdata;
int data;
};
void SeekWeekDay(void); //求某個日期對應的星期函數
int WeekDay(int year, int month, int day); //根據輸入的日期,返回對應的星期
void HowManyDays(void);//求某年某月有的天數函數
int MonthDays(int year, int month);//根據輸入的年號和月份,返回該月的天數
void PrintWeek(int weekday);//打印星期幾
void PrintMonth(int month); //打印月份
void PrintData(); //打印日歷
int main(void)
{
int choice;
while(1)
{
puts("------------------------------------------");
puts("請輸入您的選擇:");
puts("輸入1求某個日期對應的星期");
puts("輸入2求某年某月有的天數");
puts("輸入3輸出某年的日歷");
puts("輸入4結束程序");
puts("------------------------------------------");
scanf("%d", &choice); fflush(stdin);
switch(choice)
{
case 1: SeekWeekDay(); break;
case 2: HowManyDays(); break;
case 3: PrintData(); break;
case 4: return 0;
default: puts("輸入錯誤,請重新輸入"); break;
}
printf("\n"); printf("\n");
}
system("pause");
return 0;
}
void HowManyDays(void) //求某年某月有的天數函數
{
int year, month, days;
puts("請輸入年號和月份:");
scanf("%d%d", &year, &month); fflush(stdin);
printf("你的輸入為 %d年%d月,", year, month);
days = MonthDays(year, month); //根據輸入的年號和月份,返回該月的天數
printf(" %d年%d月有%d天\n", year, month, days);
}
void SeekWeekDay(void) //求某個日期對應的星期函數
{
int year, month, day, weekday;
puts("請輸入年,月, 日:");
scanf("%d%d%d", &year, &month, &day); fflush(stdin);
printf("你的輸入為 %d年%d月%d日\n", year, month, day);
weekday = WeekDay(year, month, day); //根據輸入的日期,返回對應的星期
printf("這天是 ");
PrintWeek(weekday); //打印星期幾
}
void PrintWeek(int weekday)//打印星期幾
{
switch(weekday)
{
case 0 : printf("%s","周日 "); break;
case 1 : printf("%s","周一 "); break;
case 2 : printf("%s","周二 "); break;
case 3 : printf("%s","周三 "); break;
case 4 : printf("%s","周四 "); break;
case 5 : printf("%s","周五 "); break;
case 6 : printf("%s","周六 "); break;
}
}
void PrintMonth(int month) //打印月份
{
switch(month)
{
case 1 : printf("%s","一 月 "); break;
case 2 : printf("%s","二 月 "); break;
case 3 : printf("%s","三 月 "); break;
case 4 : printf("%s","四 月 "); break;
case 5 : printf("%s","五 月 "); break;
case 6 : printf("%s","六 月 "); break;
case 7 : printf("%s","七 月 "); break;
case 8 : printf("%s","八 月 "); break;
case 9 : printf("%s","九 月 "); break;
case 10: printf("%s","十 月 "); break;
case 11: printf("%s","十一 月 "); break;
case 12: printf("%s","十二 月 "); break;
}
}
int WeekDay(int year, int month, int day) //根據輸入的日期,返回對應的星期
{
int i;
int run="0", ping="0";
long sum;
for(i="1"; i {
if(i%4==0 && i%100!=0 || i%400==0)
run++;
else
ping++;
}
sum = 366*run + 365*ping;
for(i="1"; i sum += MonthDays(year, i);
sum += day; //計算總天數
return (int)sum%7;
}
int MonthDays(int year, int month)//根據輸入的年號和月份,返回該月的天數
{
switch(month)
{
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: puts("這是一個錯誤的月份!"); system("pause"); return 0;
}
}
void PrintData(void)//打印日歷,對輸出格式的控制較復雜
{
struct mon month[13];
int i, j, k;
int year, mon, week;
puts("請輸入年號:");
scanf("%d", &year);
for(i="1"; i<13; i++) //存儲該年每個月的總天數和初始日期
{
month[i].data = 1;
month[i].maxdata = MonthDays(year, i);
}
for(i="0"; i<6; i++) //總共輸出6排
{
for(j="1"; j<=2; j++) //每排輸出2個月
{
mon = 2*i + j;
printf("%15s", " ");
PrintMonth(mon); //第一行打印月份
printf("%15s", " ");
if(j==1)
printf("\t");
}
printf("\n"); printf("\n");
for(j="1"; j<=2; j++)
{
for(k="0"; k<7; k++)
{
PrintWeek(k); //第2行打印星期
}
printf("\t");
}
printf("\n");
for(j="1"; j<=2; j++)
{
mon = 2*i + j;
week = WeekDay(year, mon, 1);//根據輸入的日期,返回對應的星期
printf("%*d ", week*5+2, month[mon].data++); //控制輸出格式,把每月的1日打印在對應星期的下面
week++;
while(week < 7) //接著在該行打印該周剩余的日期
{
printf("%2d ", month[mon].data++);
week++;
}
if(j==1)
printf("\t");
}
printf("\n"); printf("\n");//從第4行起打印該月剩余的日期,每7個一行;直至該月日期打印完畢
while(month[2*i+1].data<=month[2*i+1].maxdata || month[2*i+2].data<=month[2*i+2].maxdata)
{
for(j="1"; j<=2; j++)
{
mon = 2*i + j;
for(k="0"; k<7; k++)
{ //如果該月日期未打印完,打印該日期
if(month[mon].data<=month[mon].maxdata)
printf("%2d ", month[mon].data++);
else //否則輸出空格
printf(" ");
}
if(j==1)
printf("\t");
}
printf("\n"); printf("\n");
}
printf("\n");
}
}
二,打印版(:即既能輸出到屏幕,又可輸出到文件)
/*萬年歷全集:程序可以實現如下三種功能:
求某個日期對應的星期
求某年某月有的天數
輸出某年的日歷
*/
/*2006-1-2 梁見斌*/
#include
#include
struct mon
{
int maxdata;
int data;
};
void SeekWeekDay(void); //求某個日期對應的星期函數
int WeekDay(int year, int month, int day); //根據輸入的日期,返回對應的星期
void HowManyDays(void);//求某年某月有的天數函數
int MonthDays(int year, int month);//根據輸入的年號和月份,返回該月的天數
void PrintWeek(int weekday, FILE *fp);//打印星期幾
void PrintMonth(int month, FILE *fp); //打印月份
void PrintData(); //打印日歷
int main(void)
{
int choice;
while(1)
{
puts("------------------------------------------");
puts("請輸入您的選擇:");
puts("輸入1求某個日期對應的星期");
puts("輸入2求某年某月有的天數");
puts("輸入3輸出某年的日歷");
puts("輸入4結束程序");
puts("------------------------------------------");
scanf("%d", &choice); fflush(stdin);
switch(choice)
{
case 1: SeekWeekDay(); break;
case 2: HowManyDays(); break;
case 3: PrintData(); break;
case 4: return 0;
default: puts("輸入錯誤,請重新輸入"); break;
}
printf("\n"); printf("\n");
}
system("pause");
return 0;
}
void HowManyDays(void) //求某年某月有的天數函數
{
int year, month, days;
puts("請輸入年號和月份:");
scanf("%d%d", &year, &month); fflush(stdin);
printf("你的輸入為 %d年%d月,", year, month);
days = MonthDays(year, month); //根據輸入的年號和月份,返回該月的天數
printf(" %d年%d月有%d天\n", year, month, days);
}
void SeekWeekDay(void) //求某個日期對應的星期函數
{
FILE *fp;
int year, month, day, weekday;
if ( (fp="fopen"("wnlweek.txt","w+")) == NULL)
{
fprintf(stderr,"\nError opening file\n");
exit(1);
}
puts("請輸入年,月, 日:");
scanf("%d%d%d", &year, &month, &day); fflush(stdin);
printf("你的輸入為 %d年%d月%d日\n", year, month, day);
weekday = WeekDay(year, month, day); //根據輸入的日期,返回對應的星期
printf("這天是 ");
PrintWeek(weekday, fp); //打印星期幾
}
void PrintWeek(int weekday, FILE *fp)//打印星期幾
{
switch(weekday)
{
case 0 : fprintf(stdout, "%s","周日 "); fprintf(fp, "%s","周日 "); break;
case 1 : fprintf(stdout, "%s","周一 "); fprintf(fp, "%s","周一 ");break;
case 2 : fprintf(stdout, "%s","周二 "); fprintf(fp, "%s","周二 ");break;
case 3 : fprintf(stdout, "%s","周三 "); fprintf(fp, "%s","周三 ");break;
case 4 : fprintf(stdout, "%s","周四 "); fprintf(fp, "%s","周四 "); break;
case 5 : fprintf(stdout, "%s","周五 "); fprintf(fp, "%s","周五 ");break;
case 6 : fprintf(stdout, "%s","周六 "); fprintf(fp, "%s","周六 ");break;
}
}
void PrintMonth(int month, FILE *fp) //打印月份
{
switch(month)
{
case 1 : fprintf(stdout, "%s","一 月 "); fprintf(fp, "%s","一 月 ");break;
case 2 : fprintf(stdout, "%s","二 月 "); fprintf(fp, "%s","二 月 ");break;
case 3 : fprintf(stdout, "%s","三 月 "); fprintf(fp, "%s","三 月 ");break;
case 4 : fprintf(stdout, "%s","四 月 "); fprintf(fp, "%s","四 月 ");break;
case 5 : fprintf(stdout, "%s","五 月 "); fprintf(fp, "%s","五 月 ");break;
case 6 : fprintf(stdout, "%s","六 月 "); fprintf(fp, "%s","六 月 ");break;
case 7 : fprintf(stdout, "%s","七 月 "); fprintf(fp, "%s","七 月 ");break;
case 8 : fprintf(stdout, "%s","八 月 "); fprintf(fp, "%s","八 月 ");break;
case 9 : fprintf(stdout, "%s","九 月 "); fprintf(fp, "%s","九 月 ");break;
case 10: fprintf(stdout, "%s","十 月 "); fprintf(fp, "%s","十 月 ");break;
case 11: fprintf(stdout, "%s","十一 月 "); fprintf(fp, "%s","十一 月 "); break;
case 12: fprintf(stdout, "%s","十二 月 "); fprintf(fp, "%s","十二 月 "); break;
}
}
int WeekDay(int year, int month, int day) //根據輸入的日期,返回對應的星期
{
int i;
int run="0", ping="0";
long sum;
for(i="1"; i {
if(i%4==0 && i%100!=0 || i%400==0)
run++;
else
ping++;
}
sum = 366*run + 365*ping;
for(i="1"; i sum += MonthDays(year, i);
sum += day; //計算總天數
return (int)sum%7;
}
int MonthDays(int year, int month)//根據輸入的年號和月份,返回該月的天數
{
switch(month)
{
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: puts("這是一個錯誤的月份!"); system("pause"); return 0;
}
}
void PrintData(void)//打印日歷,對輸出格式的控制較復雜
{
FILE *fp;
struct mon month[13];
int i, j, k;
int year, mon, week;
if ( (fp="fopen"("wnldata.txt","w+")) == NULL)
{
fprintf(stderr,"\nError opening file\n");
exit(1);
}
puts("請輸入年號:");
scanf("%d", &year);
fprintf(stdout, "--------------------------------------------------------------------------\n");
fprintf(fp, "--------------------------------------------------------------------------\n");
fprintf(stdout, "%35d %s\n", year, "年"); fprintf(fp, "%35d %s\n", year,"年");
fprintf(stdout, "--------------------------------------------------------------------------\n");
fprintf(fp, "--------------------------------------------------------------------------\n");
for(i="1"; i<13; i++) //存儲該年每個月的總天數和初始日期
{
month[i].data = 1;
month[i].maxdata = MonthDays(year, i);
}
for(i="0"; i<6; i++) //總共輸出6排
{
for(j="1"; j<=2; j++)//每排輸出2個月
{
mon = 2*i + j;
fprintf(stdout, "%15s", " "); fprintf(fp, "%15s", " ");
PrintMonth(mon, fp); //第一行打印月份
fprintf(stdout, "%15s", " "); fprintf(fp, "%15s", " ");
if(j==1)
{ fprintf(stdout, "\t"); fprintf(fp, "\t"); }
}
fprintf(stdout, "\n"); fprintf(stdout, "\n"); fprintf(fp, "\n"); fprintf(fp, "\n");
for(j="1"; j<=2; j++)
{
for(k="0"; k<7; k++)
{
PrintWeek(k, fp); //第2行打印星期
}
fprintf(stdout, "\t"); fprintf(fp, "\t");
}
printf("\n"); fprintf(fp, "\n");
for(j="1"; j<=2; j++)
{
mon = 2*i + j;
week = WeekDay(year, mon, 1); //根據輸入的日期,返回對應的星期
//控制輸出格式,把每月的1日打印在對應星期的下面
fprintf(stdout, "%*d ", week*5+2, month[mon].data); fprintf(fp, "%*d ", week*5+2, month[mon].data);
month[mon].data++;
week++;
while(week < 7) //接著在該行打印該周剩余的日期
{
fprintf(stdout, "%2d ", month[mon].data++); fprintf(fp, "%2d ", month[mon].data++);
week++;
}
if(j==1)
{ fprintf(stdout, "\t"); fprintf(fp, "\t"); }
}
fprintf(stdout, "\n"); fprintf(stdout, "\n"); fprintf(fp, "\n"); fprintf(fp, "\n");
//從第4行起打印該月剩余的日期,每7個一行;直至該月日期打印完畢
while(month[2*i+1].data<=month[2*i+1].maxdata || month[2*i+2].data<=month[2*i+2].maxdata)
{
for(j="1"; j<=2; j++)
{
mon = 2*i + j;
for(k="0"; k<7; k++)
{
if(month[mon].data<=month[mon].maxdata)
{ //如果該月日期未打印完,打印該日期
fprintf(stdout, "%2d ", month[mon].data);
fprintf(fp, "%2d ", month[mon].data);
month[mon].data++;
}
else //否則輸出空格
{ fprintf(stdout, " "); fprintf(fp," "); }
}
if(j==1)
{ fprintf(stdout, "\t"); fprintf(fp, "\t"); }
}
fprintf(stdout, "\n"); fprintf(stdout, "\n"); fprintf(fp, "\n"); fprintf(fp, "\n");
}
fprintf(stdout, "\n"); fprintf(fp, "\n");
}
fclose(fp);
}