怎样做日历表

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/08 14:54:27
怎样做日历表

怎样做日历表
怎样做日历表

怎样做日历表
#include"stdio.h"
  #include"time.h"
  main(){
  //定义变量
  struct tm t,*date;
  time_t t_day;
  int year,month;
  int month_day;
  int i,j;
  struct tm *local;
  time_t t_mow;
  t_mow=time(NULL);
  local=gmtime(&t_mow);
  //输入年份和月份
  printf("请输入年份:\n");
  scanf("%d",&year);
  printf("请输入月份:\n");
  scanf("%d",&month );
  if(month==2 ){
  if(year%4==0 && year%100!=0)
  month_day=29;
  else
  month_day=28;
  }
  else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
  {
  month_day=31;
  }
  else //大月有30天
  month_day=30;
  printf("当前时间是:");
  printf(asctime(local));
  printf("\n\n");
  printf("当月天数是:%d \n\n 日历\n",month_day); //输出当前天数
  printf("一\t二\t三\t四\t五\t六\t日\t\n"); //日历的第一行,显示一二三四五六日
  for (i=1;i<=month_day;i++)
  {
  t.tm_year=year-1900;
  t.tm_mon=month-1;
  t.tm_mday=i;
  t.tm_hour=0;
  t.tm_min=0;
  t.tm_sec=0;
  t.tm_isdst=0;
  t_day=mktime(&t);
  date=gmtime(&t_day);
  //下面的date->tm_wday 就是星期 – 取值区间为0到6,其中0代表星期天,1代表星期一,以此类推
  //由于输出是从屏幕最左端开始的,如果1号是星期2,我们就要把星期1底下的数空出来,
  //如果1号是星期3,我们就要把星期1和星期2底下的数空出来 以此类推
  if(i==1)
  {
  for(j=0;j<date->tm_wday;j++)
  printf("\t");
  }
  if(date->tm_wday==6)
  {
  printf("%d\n",i);
  }
  else
  printf("%d\t",i);
  }
  printf("\n\n",i);
  }