zoukankan      html  css  js  c++  java
  • C语言实现日历输出

    这个还是挺实用的。。。。

    头文件:

    #ifndef MAIN_H
    #define MAIN_H
    
    #include "stdio.h"
    #include "math.h"
    #include "stdlib.h"
    
    //获取某一个月的最大天数 
    int monthday(int,int );
    
    //判断闰年 ,是返回1,不是返回0 
    int isleapyear(int );
    
    #endif 

    主函数:

    #include "main.h"
    
    int main()
    {
        int year,month,days,weekday;
        int i,d;
        while(1)
        {
            printf("please input the year:
    ");
            scanf("%d",&year);
            days = year-1+(year-1)/400+(year-1)/4-(year-1)/100;//计算某年第一天是星期几 
            
            for(month=1;month<=12;month++)
            {
                printf("		****%d年--%d月****
    ",year,month);
                printf("sun	mon	tues	wed	thur	fir	sat	
    ");//表头
                i = 1;    d = 1;
                weekday = (days + 1)%7;  //求星期几 
                while(i<=weekday)   //输出前面的空格 
                {
                    printf("	");
                    i++;
                } 
                
                while(d<=monthday(month,year))     //输出日期 
                {
                    weekday = (days + 1)%7;
                    if(weekday==6)   //最后一个是星期六,输出之后要换行 
                        printf("%d
    ",d);
                    else             //不是星期六的输出后不换行 
                         printf("%d	",d);
                     if(d==monthday(month,year))
                         printf("
    ");
                     d++;
                     days++;
                }
            }
        }
        
    }

    功能函数实现:

    #include "main.h"
    
    int monthday(int month,int year)
    {
        switch(month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                return 31;break;
                
                case 4:
                case 6:
                case 9:
                case 11:
                return 30;break; 
                
                case 2:
                if(isleapyear(year))    
                {
                    return 29;//闰年29天 
                    break;
                }
                else
                {
                    return 28;
                    break;
                }
                
            } 
        
    }
    
    
    int isleapyear(int year)
    {
        if((year%4==0)&&(year%100!=0)||(year%400==0))
            return 1;
        else 
            return 0;
    }

    效果图:

  • 相关阅读:
    Less:优雅的写CSS代码
    线程池(ThreadPool)
    TiDB
    Docker实现CentOS容器SSH远程登录
    Oracle-Hints详解
    Oracle sql执行计划解析
    引擎基本服务接口API介绍
    ssh远程连接docker中linux(ubuntu/centos)
    自制操作系统
    kafka-net
  • 原文地址:https://www.cnblogs.com/qsyll0916/p/7029358.html
Copyright © 2011-2022 走看看