编写日历

实现最简单的日历编写以及输出

# include<stdio.h>
# include <math.h>
void printmonth(int m);
void printhead(int m);
int daysofmonth(int m);
int firstday(int y);
int year,weekday;

void main()
{
 int i;
 printf("
请输入年份:");
 scanf("%d",& year);
 weekday=firstday(year);
 printf("\n\n");
 printf("                 %d
年\n",year);
 for(i=1;i<=12;i++)
 {
  printmonth(i);
  printf("\n");
 }
 printf("\n\n");
}
void printmonth(int m)  //打印每月日历
{
 int i,days;
 printhead(m);
 days=daysofmonth(m);
 for(i=1;i<=days;i++)
 {
  printf("%5d",i);
  weekday=(weekday+1)%7;
  if (weekday==0) printf("\n   ");
 }
}
void printhead(int m) //打印每月的日历头(判定起始位置)
{
 int i;
 printf("\n%d月   日   一   二   三   四   五   六\n",m);
 printf("   ");
 for(i=0;i<weekday;i++)
  printf("     ");
}
int daysofmonth(int m)  //每月的天数
{
 switch (m)
 {
 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;
 case 2:if (((year%4==0 &&year%100!=0)||year%400==0))
      return 29;
     else
      return 28;
 default: return 0;
 }
}
int firstday(int y)  //判断某年元旦是星期几
{
 double s ;
 s=floor(year-1+(year-1)/4.0-(year-1)/100.0+(year-1)/400.0+1);
 return (int)s%7;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

StriveZs

用爱发电

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值