C语言课设作业——万年历(可翻页式)

经过借鉴他人的代码并经过自己的改动达到了需要的程度,是一个有瑕疵的成品,仅供新人使用需求。

#include<stdio.h>
	#include<time.h>
	#include<conio.h>             
	#include<stdlib.h>           												   //用来使用翻页
	int rn(int year)          														  //判断是否为闰年
	{		
	    if((year%4==0)&&(year%100!=0)||year%400==0)
	        return 1;
	    else return 0;
	}
	void date(int year,int month,int day,int hour,int min,int sec)          
	{
	    int month_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};        
	    char month_str[13][4]={"","Jan","Feb","Mar","Apl","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	    int i,j,mdays,wdays,days=0;         										 //mdays表示每月的天数,days表示到现在的总天数
	    for(i=1;i<year;i++)
	    {
	        if(rn(i))
	            days+=366;
	        else
	            days+=365;
	    }
	    for(i=1;i<month;i++)
	    {
	        if(i==2&&rn(year))
	        days+=29;
	        else
	        days+=month_day[i];
	    }
	    printf("   %s.%d.%d     %d:%d:%d\n",month_str[month],day,year,hour,min,sec);    //输出当前系统的时间
	    printf("  一  二  三  四  五  六  日\n");
	    wdays=days%7;
	    for(j=0;j<wdays;j++)
	        printf("    ");                         //计算每月一日为星期几来决定打印空格数
	        if(month==2&&rn(year))
	        mdays=29;
	        else
	        mdays=month_day[month];
	        for(i=1;i<=mdays;i++)                   //以当前月份输出日期
	        {
	            if(i>1&&days%7==0)
	            {
	                printf("\n");        //以7天作为一行输出
	                printf("\n");        //将两周日期分开,使日历更清晰
	            }
	            printf("%4d",i);         //输出日期
	            days=days+1;
	        }
	        printf("\n----------------------------\n\n\n");
	}
	void main()
	{
	    time_t t;
	    struct tm *ofo;
	    int year,month,day,hour,min,sec,ch;
	    time(&t);
	    ofo=localtime(&t);                 //使结构体指针指向系统时间
	    year=ofo->tm_year+1900;           //取出ofo所指向的系统时间(系统从1900年开始计算)
	    month=ofo->tm_mon+1;
	    day=ofo->tm_mday;
	    hour=ofo->tm_hour;
	    min=ofo->tm_min;
	    sec=ofo->tm_sec;
	    while(1)
	    {
	        date(year,month,day,hour,min,sec);          //传递系统时间
	        printf("      ↑......上一年\n\n");
	        printf("      ↓......下一年\n\n");
	        printf("      ←....前一个月\n\n");
	        printf("      →....后一个月\n\n");
	        printf("      Esc.......退出\n\n");
	        printf("\n\n");
	        system("pause");
	        system("cls");					//清屏
	        while(1)
	        {
	        ch=getch();
	        if(ch==0) ch=getch();           
	        if( ch == 72 )                  //Up
	        {
	            year--;
	        }
	        if ( ch == 80)                  //Down
	        {
	            year++;
	        }
	        if ( ch == 75)                  //Left
	        {
	            year-=(month==1)?1:0;
	            month=(month==1)?12:month-1;
	        }
	        if ( ch == 77)                  //Right
	        {
		        year+=(month==12)?1:0;
		        month=month%12+1;
	        }
	        if(ch == 27)                         //按Esc退出
	        return ;
	        break;
	        }
	    }
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值