码迷,mamicode.com
首页 > 其他好文 > 详细

日历日历日历

时间:2015-07-07 16:55:30      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

原版始于2012.1.10

#include "stdio.h"
#include "conio.h"
#include "time.h"
#include "stdlib.h"

void diao(int year,int month);//第一个被调用函数,只是获得space;
int leap(int year);//是闰年加1
int jjmonth(int year,int month);//计算n月的总天数
void shuchu(int space,int year,int month);//输出
int get_leap(int year);//判断是否为闰年
int month_of_day(int year, int month);//这月有几天啊

int main()
{
int year,month;
int kongzhi;
 struct tm*p;//tm结构指针
  time_t secs;//声明time_t类型变量
  time (&secs);//获取系统日期与时间
  p=localtime(&secs);//获取当地日期时间



printf("\n                                         now:%d-%d-%d  %d:%d:%d   星期%d  \n\n",
	   p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec,p->tm_wday); 
year=p->tm_year+1900;
month=p->tm_mon+1;
	  diao(year,month);
	while(1)
	{
		kongzhi=getch();//用getch就不用按enter了
        
		if(kongzhi=='w')year=year+1;
        if(kongzhi=='s')year=year-1;
        if(kongzhi=='d')
		{
			month=month+1;
			if(month>12)
			{
                year=year+1;
				month=1;
			}
		}
        if(kongzhi=='a')
		{
			month=month-1;
			if(month<1)
			{
              year=year-1;
			  month=12;
			}
		}
		if(kongzhi=='c')break;
         system("cls"); //清屏清屏清屏

  printf("\n                                                  现在是:%d-%d-%d  %d:%d:%d  \n\n",
	  p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec);


        diao(year,month);
	}
    system("cls");
    printf("\n\n\n\n\n\n\n\n\t\t\t\t谢谢使用");
    _sleep(10000);//延迟函数
   return 0;
}

void diao(int year,int month)
{
	int tian,space;//这个月之前的天数
	tian=(year-1600)*365+leap(year);
    tian=tian+jjmonth(year,month)-1;
	space=tian%7;//得到空格数

    shuchu(space,year,month);
}

int leap(int year)
{int yan,i=0;//1600到本年的闰年数
	for(yan=1600;yan<year;yan+=4)
		{
       if( (yan%4==0) && (yan%100 != 0) || (yan%400 == 0))i++;
	}
        return i;
}

int jjmonth(int year,int month)
{//本年已经过去天数
     if( (year%4==0) && (year%100 != 0) || (year%400 == 0))
	 {
		     switch(month-1)
				{
			    case 0:return 0;
				case 1:return 31;
				case 2:return 60;
				case 3:return 91;
				case 4:return 121;
				case 5:return 152;
				case 6:return 182;
				case 7:return 213;
				case 8:return 244;
				case 9:return 274;
				case 10:return 305;
				case 11:return 335; 
				 }
	 }
			else
			 {
              switch(month-1)
				{
			    case 0:return 0;
				case 1:return 31;
				case 2:return 59;
				case 3:return 90;
				case 4:return 120;
				case 5:return 151;
				case 6:return 181;
				case 7:return 212;
				case 8:return 243;
				case 9:return 273;
				case 10:return 304;
				case 11:return 334; 
				 }

			 }
}

void shuchu(int space,int year,int month)
{
	int i;
	 printf("                       %d年%d月                                 \n\n\n",year,month);
    printf("星期日   星期一  星期二 星期三  星期四  星期五 星期六\n");
    printf("-------------------------------------------------------\n");    
    for(i=0;i<space;i++) printf("       ");//先输出空格数
    for(i=1;i<month_of_day(year,month)+1;i++){//本月有多少天
	
      printf("%7d",i);
	  if((space+i)%7==0)printf("\n");
	}
    printf("\n");
    printf("-------------------------------------------------------\n\n");
    printf("加年w 减年s 加月d 减月a  退出按	c	\n\n\n");
}

int month_of_day(int year , int month)
{ //本月有多少天
 
    switch(month)
    {case 2:  return leap(year)?29:28;//这里起到了简化作用
    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;
    }
  
} 
/*int get_leap(int year)
{//若本年是闰年,二月加1
    if( (year%4==0) && (year%100 != 0) || (year%400 == 0))
        return 1;
    return 0;
}*/




////////////////////////////---------------------------------/



#include "stdio.h"
#include "conio.h"
#include "time.h"
#include "stdlib.h"

void diao(int year,int month);//第一个被调用函数,只是获得space;
int leap(int year);//是闰年加1
int jjmonth(int year,int month);//计算n月的总天数
void shuchu(int space,int year,int month);//输出
int get_leap(int year);//判断是否为闰年
int month_of_day(int year, int month);//这月有几天啊

int main()
{
int year,month;
int kongzhi;
 struct tm*p;//tm结构指针
  time_t secs;//声明time_t类型变量
  time (&secs);//获取系统日期与时间
  p=localtime(&secs);//获取当地日期时间



printf("\n                                         now:%d-%d-%d  %d:%d:%d   星期%d  \n\n",
	   p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec,p->tm_wday); 
year=p->tm_year+1900;
month=p->tm_mon+1;
	  diao(year,month);
	while(1)
	{
		kongzhi=getch();//用getch就不用按enter了
        
		if(kongzhi=='w')year=year+1;
        if(kongzhi=='s')year=year-1;
        if(kongzhi=='d')
		{
			month=month+1;
			if(month>12)
			{
                year=year+1;
				month=1;
			}
		}
        if(kongzhi=='a')
		{
			month=month-1;
			if(month<1)
			{
              year=year-1;
			  month=12;
			}
		}
		if(kongzhi=='c')break;
         system("cls"); //清屏清屏清屏

  printf("\n                                                  现在是:%d-%d-%d  %d:%d:%d  \n\n",
	  p->tm_mon+1,p->tm_mday,p->tm_year+1900,p->tm_hour,p->tm_min,p->tm_sec);


        diao(year,month);
	}
    system("cls");
    printf("\n\n\n\n\n\n\n\n\t\t\t\t谢谢使用");
    _sleep(10000);//延迟函数
   return 0;
}

void diao(int year,int month)
{
	int tian,space;//这个月之前的天数
	tian=(year-1600)*365+leap(year);
    tian=tian+jjmonth(year,month)-1;
	space=tian%7;//得到空格数

    shuchu(space,year,month);
}

int leap(int year)
{int yan,i=0;//1600到本年的闰年数
	for(yan=1600;yan<year;yan+=4)
		{
       if( (yan%4==0) && (yan%100 != 0) || (yan%400 == 0))i++;
	}
        return i;
}

int jjmonth(int year,int month)
{//本年已经过去天数
int y=0;                 //起到简化作用

if( (year%4==0) && (year%100 != 0) || (year%400 == 0))y=1;
	
           switch(month-1)
				{
			    case 0:return 0;
				case 1:return 31;
				case 2:return 59+y;
				case 3:return 90+y;
				case 4:return 120+y;
				case 5:return 151+y;
				case 6:return 181+y;
				case 7:return 212+y;
				case 8:return 243+y;
				case 9:return 273+y;
				case 10:return 304+y;
				case 11:return 334+y; 
				 }

			 
}

void shuchu(int space,int year,int month)
{
	int i;
	 printf("                       %d年%d月                                 \n\n\n",year,month);
    printf("星期日   星期一  星期二 星期三  星期四  星期五 星期六\n");
    printf("-------------------------------------------------------\n");    
    for(i=0;i<space;i++) printf("       ");//先输出空格数
    for(i=1;i<month_of_day(year,month)+1;i++){//本月有多少天
	
      printf("%7d",i);
	  if((space+i)%7==0)printf("\n");
	}
    printf("\n");
    printf("-------------------------------------------------------\n\n");
    printf("加年w 减年s 加月d 减月a  退出按	c	\n\n\n");
}

int month_of_day(int year , int month)
{ //本月有多少天
 
    switch(month)
    {case 2:  return get_leap(year)?29:28;
    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;
    }
  
} 

int get_leap(int year)
{//若本年是闰年,二月加1
    if( (year%4==0) && (year%100 != 0) || (year%400 == 0))
        return 1;
    return 0;
}










版权声明:本文为博主原创文章,未经博主允许不得转载。

日历日历日历

标签:

原文地址:http://blog.csdn.net/h1023417614/article/details/46790539

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!