实验十九:编写一个万年历系统
1.设计目的:
从实验的角度来看,这次课程设计一方面可以让自己巩固并加深对C语言程序设计知识的理解,掌握和提高C语言编程和程序的基本调试的基本技能,进一步理解和运用结构化程序的思想和方法;另一方面,可以让自己在面对一个全新的问题时,学会如何思考,如何寻找问题的关键,从而提升自己的能力。
2.总体设计:
1.、当前页以系统当前日期的月份为准,显示当前月的每一天(显示出日及对应的星期几);
2、当系统日期变到下一个月时,系统自动翻页到下一月。
3.调试与测试:
4.源程序清单和执行结果:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int leap(int year )
{
if ((year %4 == 0) && (year % 100 != 0)
|| (year % 400 == 0))
{
return 1;
}
return 0;
}
void show(int year,int month,int date,int sec,int mini,int hour) {
const char month_str[][4]={"","Jan","Feb","Mar","Apl", "May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int i,j,wdays,mdays,days;
for(i=1,days=0;i<year;i++)
{
if(leap(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && leap(year))
{
days+=29;
}
else
{
days+=month_day[i];
}
}
printf(" %s.%d.%d %d:%d:%d \n",month_str[month],date,year,hour,mini,sec);
printf(" 一二三四五六日\n");
wdays = days % 7;
for( j = 0; j < wdays; j++)
{
printf(" ");
}
if(month == 2 && leap(year))
{
mdays=29;
}
else
{
mdays= month_day[month];
}
for(i=1;i<=mdays;i++)
{
if( i > 1 && days % 7 == 0 )
{
Printf(“\n”);
printf("\n");
}
printf("%4d",i);
days=days+1;
}
printf("\n---------------------------\n\n\n");
}
void main()
{
time_t rawtime;
struct tm *info;
int year,month,date,sec,mini,hour; char ch;
time ( &rawtime );
info = localtime ( &rawtime );
year =info->tm_year + 1900; month =info->tm_mon + 1;
date =info->tm_mday;
hour =info->tm_hour;
mini =info->tm_min;
sec =info->tm_sec;
while(1)
{
show(year,month,date,sec,mini,hour); printf("↑......上一年\n\n");
printf("↓......下一年\n\n");
printf("←....前一个月\n\n");
printf("→....后一个月\n\n");
printf("Esc.......退出\n\n");
ch=getch();
switch(ch)
{
case 27://Ecs
exit(0);
case -32://Navigator
ch=getch();
if(ch==77)
{//Right
year+=(month==12)?1:0;
month=month%12+1;
}
else if(ch==75)
{//Left
year-=(month==1)?1:0;
month=(month-2+12)%12+1;
}
else if(ch==72) {//Up
year--;
}
else if(ch==80) {//Down
year++;
}
system("cls"); }
}
}。