📄 chengxu.txt
字号:
程序可以实现如下三种功能:
求某个日期对应的星期
求某年某月有的天数
输出某年的日历.
例如,打印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 梁见斌*/
#i nclude
#i nclude
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++)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -