码迷,mamicode.com
首页 > 其他好文 > 详细

已知1800年1月1日是星期三,打印指定的年份和月份月历

时间:2017-06-11 18:22:25      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:play   err   ber   --   ios   bsp   log   efi   define   

技术分享
#ifndef CALENDAR_H_
#define CALENDAR_H_

#include <string>
#include <iostream>
#include <stdio.h>


void showCalendar(int year, int month);
void showHead(int year, int month);
void showBody(int year, int month);
std::string month_number_toString(int month);
int getFirstDayOfMonth(int year, int month);
int getMonthDays(int year, int month);
bool isLeapYear(int year);


void showCalendar(int year, int month)
{
    
    showHead(year, month);
    showBody(year, month);
    std::cout << "\n-----------------------------------\n";
}

void showHead(int year, int month)
{
    std::cout << "           ";
    std::cout << month_number_toString(month) << " " << year << std::endl;
    std::cout << "-----------------------------------\n";
    std::cout << " Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
}

void showBody(int year, int month)
{
    int firstDay = getFirstDayOfMonth(year, month);
    int i = 0;
    int monthDays = getMonthDays(year, month);
    for (i = 0; i < firstDay; i++)
    {
        std::cout << "     ";
    }
    for (i = 1; i <= monthDays; i++)
    {
        printf(" %-3d ", i);
        if((firstDay + i) % 7 == 0)
            std::cout << std::endl;
    }
}

std::string month_number_toString(int month)
{
    std::string ret;
    switch (month)
    {
    case 1:        ret = "January";    break;
    case 2:        ret = "February";    break;
    case 3:        ret = "March";        break;
    case 4:        ret = "April";        break;
    case 5:        ret = "May";        break;
    case 6:        ret = "June";        break;
    case 7:        ret = "July";        break;
    case 8:        ret = "August";        break;
    case 9:        ret = "September";    break;
    case 10:    ret = "October";    break;
    case 11:    ret = "November";    break;
    case 12:    ret = "December";    break;
    default:    ret = "ErrorMonth";    
    }
    return ret;
}

int getFirstDayOfMonth(int year, int month)
{
    int totalDays = 0;
    for (int i = 1800; i < year; i++)
        totalDays += isLeapYear(i) ? 366 : 365;
    for (int i = 1; i < month; i++)
        totalDays += getMonthDays(year, i);
    return (totalDays + 3) % 7;
}

int getMonthDays(int year, int month)
{
    int monthArray[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    return (month == 2 && isLeapYear(year)) ? 29 : monthArray[month - 1];
}

bool isLeapYear(int year)
{
    return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}

#endif

#include "calendar.h"
#include <iostream>


int main()
{
    using namespace::std;
    int year, month;
    cout << "Enter full year (e.g., 2001): ";
    cin >> year;
    while (getchar() != \n);
    cout << "Enter month in number between 1 and 12: ";
    cin >> month;
    showCalendar(year, month);
    return 0;
}
View Code
Enter full year (e.g., 2001): 2017
Enter month in number between 1 and 12: 6
           June 2017
-----------------------------------
 Sun  Mon  Tue  Wed  Thu  Fri  Sat
                     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
-----------------------------------

 

已知1800年1月1日是星期三,打印指定的年份和月份月历

标签:play   err   ber   --   ios   bsp   log   efi   define   

原文地址:http://www.cnblogs.com/endenvor/p/6985694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!