Home Article c++打印日历程序

c++打印日历程序

Release time:2020-07-24 20:19:17 Author:admin Reading volume:65

#include <iostream>
#include <cstdio>
using namespace std;

class Calenda{
public:
    Calenda(int year,int month)
    {
        this->year = year;
        this->month = month;
    }
//计算某年是否为闰年,是返回1,否返回0
char isRn(int year)
{
    return(year % 4 == 0 && year % 100 !=0 || year % 400 == 0);
}

//计算某年月有多少天
int getDayCount(int year,int month)
{
    int dayCount = 31;
    if (month == 4 || month == 6 || month == 9|| month == 11)
    {
        dayCount = 30;
    }
    else if (month == 2)
    {
        dayCount = (isRn(year) ? 29 : 28);
    }

    return dayCount;
}

//计算某年月的一号到1900-1-1有多少天
int getInterval(int year,int month)
{
    int interval = 0;
    //累加年份的天数
    int i = 1900;
    for(;i<year;i++)
    {
        interval += isRn(year) ? 366 : 365;
    }
    //累加月份的天数
    for(i=1;i<month;i++)
    {
        interval += getDayCount(year,i);
    }
    return interval;
}

//显示某年月的月历
void showCalendar()
{
    int year = this->year;
    int month = this->month;
    int dayCount = getDayCount(year,month);
    //计算1号是星期几,即1号前面留几个空格
    int blanks = (getInterval(year,month)+1)%7;
    printf("============ %d - %d ================ ",year,month);
    printf("Sun Mon Tue Wed Thu Fri Sat ");
    int i;
    for(i = 0;i < blanks;i++)
    {
        printf(" ");
    }

    for(i = 1;i <= dayCount; i++ )
    {
        printf("%d ",i);
        //日历显示是7个一行,因为每周七天,所以输出7个要换一行输出
        if ((i+blanks)%7 == 0)
        {
            printf(" ");
        }
    }
}

private:
    int year,month;
};

int main()
{
    int year,month;
    printf("输入年份: ");
    scanf("%d",&year);
    printf("输入月份: ");
    scanf("%d",&month);

    Calenda ym(year,month);
    ym.showCalendar();   
    return 0;

}

支付宝打赏 微信打赏
  
I want to comment

Search

Leave a message
http://blog.rjxj513.com/
User login
You have not written any reviews yet!
You have commented!
Can only praise once!
You have a collection!