C语言打印日历——第一篇FOR WORK写博客

一篇应该不太有人看的博客,哈哈哈~~只是因为前辈叮嘱开始写博客记录我的IT生活。
**

PrintCalendar——打印日历

**。Language:C
题目:
1、用户从终端输入一个长度为8位的字符串
2、字符串的内容为某年某月某日的数值
3、程序通过计算输出当年当月日历
4、程序设计原理及思路 允许 百度
5、希望诸位充分理解题意写出没有BUG的代码
6、程序中 不能 预设题外已知值 如:定义2019年11月1日为星期五
7、提供如下两串神秘代码可以使用

^20[0|1][0-9](0[1-9]|1[0|1|2])(0[1-9]|[1|2][0-9]|3[0-1])$

^20[0|1][0-9](((0[1|3|5|7|8])(0[1-9]|[1|2][0-9]|3[0-1]))|((0[4|6|9])(0[1-9]|[1|2][0-9]|30))|(02(0[1-9]|[1|2][0-9]))|(1[0|2](0[1-9]|[1|2][0-9]|3[0-1]))|(11(0[1-9]|[1|2][0-9]|30)))$

8、只有正确的输入才能得到正确的结果 错误的输入必然得不到正确的结果
9、提供一个正确的示例:
终端输入:
20191118
终端输出:
日      一      二      三      四      五      六
	                                    1       2
3       4       5       6       7       8       9
10      11      12      13      14      15      16
17      18      19      20      21      22      23
24      25      26      27      28      29      30

首先本人没有用到上面两个正则,原因有两个:一、用正则要用regex类库,本人不是很熟悉。二、这个正则没有考虑所有情况。
这也导致本代码在输入校验上做的不是很好

写代码且先必须知道的两个判定条件或公式
1.计算某年某月某天是星期几:
蔡勒(Zeller)公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1
其中:y为年份后两位,c为世纪即年份前两位,m为月份(大于等于3,小于等于14,本年1,2月算上年13,14月),d为日。
详细阐述见链接:

  • https://blog.csdn.net/wcg_jishuo/article/details/7039091

以上公式只适用于1582年10月15日之后的情形

2.判断闰年条件:
1⃣️普通年能被4整除且不能被100整除的为闰年。
2⃣️能被100整除且能被400整除的是闰年(能被100整除不能被400整除是平年)。

知道以上条件就可以直接上代码了,这里你只要对着日历想算法,你就能想到了。

#include<stdio.h>
int getWeekday(long value);/*求出某年某月某天是星期几*/
int isLeapyear(long value);/*判断闰年*/
void printCalendayNumber(int end);/*数字格式输出*/
//函数声明
int year,weekday,month,day,century;
//全局变量
int main(){
    long value;
    printf("Please enter the value: ");
    scanf("%ld",&value);/*这里输入是以数值形式*/
    weekday=getWeekday(value);
    printf("日 \t一 \t二 \t三 \t四 \t五 \t六\n");
    if(month==14){
        if(isLeapyear(value)==1){
            printCalendayNumber(29);
        }
        else printCalendayNumber(28);
    }
    if(month==4||month==6||month==9||month==11){
        printCalendayNumber(30);
    }
    if(month==3||month==5||month==7||month==8||month==10||month==12||month==13){
        printCalendayNumber(31);
    }
    return 0;
}
int getWeekday(long value){
//day,century,month,year全部通过数值的计算取出
    day=value%100;
    if(day>31){
         printf("The format of the input is incorrect");
        return 0;
    }
    year=(value/10000)%100;
    month=(value/100)%100;
    if(month<1||month>12){
        printf("The format of the input is incorrect");
        return 0;
    }
    if(month==1||month==2){
        month=12+month;
        year--;
    }
    century=value/1000000;
    if(century<15||(century==15&&year<82)||(century==15&&year==82&&month<10)||(century==15&&year==82&&month==10&&day<15)){
        printf("The format of the input is incorrect");
        return 0;
    }
    return (year+(year/4)+(century/4)-2*century+(26*(month+1)/10)+day-1)%7;
    //蔡勒公式
}
int isLeapyear(long value){
    int tem=value/10000;
    if(tem%4==0&&tem%100!=0||tem%400==0)
    //判断闰年条件
        return 1;
    else return 2;
}
//打印数字,传入的是该月有多少天
void printCalendayNumber(int end){
    int firstDay,i;
    if(day>weekday){
        firstDay=7-((day-(weekday+1))%7);
        //这里的firstDay计算是求出前面的空格数
    }
    else
        firstDay=weekday-(day-1);
    for(i=0;i<firstDay;i++){
        printf("\t");
    }
    for(i=1;i<=end;i++){
    if(i-1==(7-firstDay)||(i-1-(7-firstDay))%7==0)/*换行条件,看着复杂,对着日历你就能想到了*/
        printf("\n");
    printf("%2d\t",i);
    }
}

以上代码的改进最好是先传字符串,通过类库做一次正则校验,然后再转数值进行计算。**以上为输出结果**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值