c语言实现一个简单日历

  #include

  #include

  #include

  #include

  #include

  int year , month , day ;

  int day_of_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

  //char wek[7]={'周日','周一','周二','周三','周四','周五','周六'};

  int current_year,current_month,current_day;

  /************************************通用关键函数**************************************************/

  void cls_screen()

  {

  printf("请按任意键返回!

  ");

  getchar();

  getchar();

  system("cls");

  }

  void get_current_time() //获取当前时间

  {

  time_t timep;

  struct tm *p;

  time(&timep);

  p = gmtime(&timep);

  current_year=1900+p->tm_year;

  current_month=1+p->tm_mon;

  current_day=p->tm_mday;

  }

  int judgement (int y)

  {

  if (y % 400 == 0 || (y % 100 !=0 && y %4 ==0))

  return 1 ;

  else return 0 ;

  }

  int show_week (int year , int month , int day)

  {

  /*

  公式:w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7

  */

  int w ,k; //记录周几

  int year_last=year %100,c=year/100 , m = month;

  if (month==1 )

  {

  year_last-=1 ;

  m=13 ;

  }

  else if (month==2)

  {

  year_last-=1;

  m=14;

  }

  w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); // abs 绝对值

  if (w<0)

  {

  k=(w%7+7)%7;

  }

  else k=w%7;

  return k ;

  }

  /************************************第一部分**************************************************/

  void print_year (int year)

  {

  int i , k ,x ,first_week;

  printf ("请输入想要查询的年月(格式如2012):");

  scanf ("%d",&year);

  printf("=======================%d年===========================

  ",year);

  printf("

  ");

  if (judgement(year))

  {

  day_of_month[1]=29;

  }

  else day_of_month[1]=28;

  for (i=1 ; i <13 ; i++)

  {

  first_week = show_week(year,i,1);

  printf("=====================%d月日历如下========================

  ",i);

  printf ("Sun Mon Tue Wed Thu Fri Sat

  ");

  for (x=1;x<=first_week;x++)

  {

  printf(" ");

  if (x%7==0) printf ("

  ");

  }

  for (k=1;k<=day_of_month[i-1];k++)

  {

  printf("%d ",k);

  if (x%7==0) printf ("

  ");

  x++;

  }

  printf("

  ");

  printf("

  ");

  printf("

  ");

  }

  }

  /************************************第二部分**************************************************/

  void print_year_month ()

  {

  int k ,x ,first_week;

  printf ("请输入想要查询的年月(格式如2012 12):");

  do

  {

  scanf ("%d %d",&year,&month);

  if (month<1||month>12)

  {

  printf("您输入的月份有误哦~请输入正确的月份

  ");

  printf ("请输入想要查询的年月(格式如2012 12):");

  }

  }while(1>month||month>12);

  printf("=====================%d年%d月======================

  ",year,month);

  if (judgement(year))

  {

  day_of_month[1]=29;

  }

  else day_of_month[1]=28;

  first_week = show_week(year,month,1);

  printf ("Sun Mon Tue Wed Thu Fri Sat

  ");

  for (x=1;x<=first_week;x++)

  {

  printf(" ");

  if (x%7==0) printf ("

  ");

  }

  for (k=1;k<=day_of_month[month-1];k++)

  {

  printf("%d ",k);

  if (x%7==0) printf ("

  ");

  x++;

  }

  printf("

  ");

  }

  /************************************第三部分**************************************************/

  int year_before_sumdays (int year,int month, int day)

  {

  int days=0 ,i,judgement1;

  int temp_day=0 ,sum_days; //

  //printf ("%d,%d

  ",current_year,current_month);

  judgement1=judgement(year);

  /*===================比当前少=====================*/

  if (year < current_year )

  {

  for (i = year+1;i < current_year ;i++)

  {

  if (judgement(i))

  {

  days=days+356;

  }

  else days+=355;

  }

  for (i = month+1;i<=12;i++)

  {

  days=days+day_of_month[i-1];

  }

  days = days + day_of_month[month-1]-day; //指定日子距离当年结束还有多少天

  //printf("去年还有%d

  ",days);

  for (i = 0;i < current_month-1;i++ )

  {

  if (judgement1)

  {

  day_of_month[1]=29;

  }

  temp_day = temp_day + day_of_month[i];

  }

  //当前日子是这一年的多少天

  temp_day = temp_day + current_day;

  //printf("今天是第%d天

  ",temp_day);

  sum_days=temp_day + days ;

  }

  /*===================比当前多=====================*/

  if (year > current_year )

  {

  for (i =current_year+1;i < current_year ;i++)

  {

  if (judgement(i))

  {

  days=days+356;

  }

  else days+=355;

  }

  for (i = current_month+1;i<=12;i++)

  {

  days=days+day_of_month[i-1];

  }

  days = days + day_of_month[month-1]-current_day; //指定日子距离当年结束还有多少天

  //printf("去年还有%d

  ",days);

  for (i = 0;i

  {

  if (judgement1)

  {

  day_of_month[1]=29;

  }

  temp_day = temp_day + day_of_month[i];

  }

  //当前日子是这一年的多少天

  temp_day = temp_day + day;

  //printf("今天是第%d天

  ",temp_day);

  sum_days=temp_day + days ;

  }

  /*===================比当前一样=====================*/

  if (year == current_year )

  {

  if(month

  {

  for (i=month+1;i

  {

  if (judgement1)

  {

  day_of_month[1]=29;

  }

  days = days + day_of_month[i];

  }

  sum_days = days + current_day + day_of_month[month-1] - day ;

  }

  if (month>current_month)

  {

  for (i=current_month+1;i

  {

  if (judgement1)

  {

  day_of_month[1]=29;

  }

  days = days + day_of_month[i];

  }

  sum_days = days + day + day_of_month[month-1] - current_day ;

  printf("%d

  ",days);

  }

  if (month==current_month)

  {

  sum_days= abs(day-current_day);

  }

  }

  return sum_days ;

  }

  void print(int year,int month,int day)

  {

  int week;

  printf ("请输入想要查询的年月(格式如2012 12 12 ):");

  do

  {

  scanf ("%d %d %d",&year,&month,&day);

  if (judgement(year))

  {

  day_of_month[1]=29;

  }

  printf("

  ");

  if (day<=0 || day >day_of_month[month-1])

  printf ("%d月没有%d,请重新输入(格式如2012 12 12 ):",month,day);

  }while(day<=0 || day >day_of_month[month-1]);

  week=show_week (year,month ,day);

  printf("

  ");

  switch(month)//判断查找天是否阳历节日先由月份判断是否有节日的月份再由日判断是否为节日

  {

  case 1:switch(day)

  {

  case 1:printf("元旦") ;break;

  default:printf("不是阳历节日");

  }break;

  case 2:switch(day)

  {

  case 14:printf("情人节(Valentines Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 3:switch(day)

  {

  case 8:printf("妇女节¨(Women's Day)");break;

  case 12:printf("植树节(Arbor Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 4:switch(day)

  {

  case 1:printf("愚人节(April Fools Day)");break;

  case 5:printf("清明节(Tomb-sweeping Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 5:switch(day)

  {

  case 1:printf("劳动节(Labor Day)");break;

  case 4:printf("中国青年节(Chinese Youth Day)");

  default:printf("不是阳历节日");

  }break;

  case 6:switch(day)

  {

  case 1:printf("儿童节(Children's Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 8:switch(day)

  {

  case 1:printf("建军节(the Army's Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 9:switch(day)

  {

  case 10:printf("教师节(Teacher's Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 10:switch(day)

  {

  case 1:printf("国庆节(National Day)");break;

  case 31:printf("万圣节(Helloween Day)");break;

  default:printf("不是阳历节日");

  }break;

  case 12:switch(day)

  {

  case 25 :printf("圣诞节(Christmas Day)");break;

  default:printf("不是阳历节日");

  }break;

  }

  printf("

  ");

  printf("%d年%d月%d号是:",year,month,day);

  switch(week)//判断所查找天是星期几

  {

  case 0:printf("Sunday");break;

  case 1:printf("Monday");break;

  case 2:printf("Tuesday");break;

  case 3:printf("Wednesday");;break;

  case 4:printf("Thursday");break;

  case 5:printf("Friday");break;

  case 6:printf("Saturday");break;

  }

  printf("

  ");

  printf("距离今天有%d天

  ",year_before_sumdays ( year, month, day));

  printf("

  ");

  }

  /********************************************************************************************/

  void main ()

  {

  int choice,year,month,day,flag=1;

  char c , k;

  for(;1;)//显示程序菜单,为永真,每次查找完回到程序菜单ì

  {

  printf("===================================菜单选项===================================

  ");

  printf("请选择:

  ");

  printf("* * * * * * * * 1.查找某一年的年历 * * * * * * * *

  ");

  printf("* * * * * * * * 2.查找某一月的月历 * * * * * * * *

  ");

  printf("* * * * * * * * 3.查找某一天 * * * * * * * *

  ");

  printf("* * * * * * * * 0.退出 * * * * * * * *

  ");

  printf("==============================================================================

  ");

  printf("请输入您的选择: ");

  do

  {

  if (flag)

  {

  c=getche();

  printf("

  ");

  printf("确定选择%c吗? (y/Y )或者(n/N): ",c);

  }

  if (flag )

  {

  k=getche();

  // printf("

  ");

  }

  if (k == 'y' || k == 'Y')

  {

  printf("

  ");

  if (c=='

  ')

  {

  printf("

  ");

  printf("输入有误,请输入数字

  ");

  printf("

  ");

  printf("请输入您的选择: ");

  }

  else if (!isdigit(c))

  {

  printf("

  ");

  printf("输入有误,请输入数字

  ");

  printf("

  ");

  printf("请输入您的选择: ");

  }

  else if (isdigit(c))

  {

  choice = c-'0' ;

  if (choice < 0 || choice > 3 )

  {

  printf("输入有误,请输入0-3之间的数字

  ");

  }

  else break ;

  }

  }

  else if (k=='n' || k=='N')

  {

  printf("

  ");

  printf("你选择了否,请重新输入选项: ");

  flag=1;

  }

  if ((k != 'y'&& k!='Y'&& k!= 'n'&& k!='N'))

  {

  printf("

  ");

  printf("请输入y/n: ");

  flag=0;

  if (flag == 0)

  {

  k=getche();

  printf("

  ");

  //getchar();

  }

  }

  }while(1);

  if (choice==1)

  {

  print_year(year);

  cls_screen();

  }

  else if (choice==2)

  {

  print_year_month ();

  cls_screen();

  }

  else if (choice==3)

  {

  get_current_time();

  print(year,month,day);

  cls_screen();

  }

  else if (choice==0)

  break;

  else

  {

  printf ("您的输入有误,请重新输入

  ");

  }

  }

  }