输入大于1900的任意一年,输出该年的日历
#include <iostream>
#include <string>
#define MONTH_TOTAL 12
#define WEEK_DAY_TOTAL 7
#define LOWER_LIMIT_YEAR 1900
#define UPPER_LIMIT_YEAR 2050
#define ONE_YEAR_OF_DAYS 365
#define Sunday 0
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6
#define January 1
#define February 2
#define March 3
#define April 4
#define May 5
#define June 6
#define July 7
#define August 8
#define September 9
#define October 10
#define November 11
#define December 12
using namespace std;
/**
Functions prototype
**/
static int getYearFromUser();
static string getAnswerFromUser();
static bool stringEqual(string s1, string s2);
static bool isLegalYear(int year);
static void printCalendar(int year);
static void printCalendarMonth(int year, int month);
static void printLine();
static void printBlank(int count);
static void printMonthDay(int day);
static int firstDayOfMonth(int year, int month);
static void indentFirstLine(int weekday);
static int monthOfDays(int year, int month);
static string monthName(int month);
static int monthIndex(int month);
static bool isLeapYear(int year);
/**
constants
**/
static int monthOfTotalDays[MONTH_TOTAL] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
static string monthNamed[MONTH_TOTAL] = { "January", "February", "March", "April", "May" , "June",
"July", "August", "September" , "October" , "November", "December"
};
int main() {
while (true) {
int year = getYearFromUser();
printCalendar(year);
cout << "would you like to go on?" << endl;
string answer = getAnswerFromUser();
if (stringEqual(answer, "no")) {
break;
}
}
return 0;
}
int getYearFromUser() {
while (true) {
cout << "which year ?" << endl;
int year;
cin >> year;
if (year >= LOWER_LIMIT_YEAR) return year;
cout << "you must input year at least " << LOWER_LIMIT_YEAR << endl;
}
}
static string getAnswerFromUser() {
string s;
string validAnswer[] = {"yes", "no"};
cin >> s;
return s;
}
static bool stringEqual(string s1, string s2) {
return s1 == s2;
}
bool isLegalYear(int year){
return (LOWER_LIMIT_YEAR <= year) && (year <= UPPER_LIMIT_YEAR);
}
void printCalendar(int year){
for (int month = 1; month <= 12; month++){
printCalendarMonth(year, month);
printLine();
}
}
void printLine(){
cout << endl;
}
void printCalendarMonth(int year, int month){
printBlank(10);
cout << monthName(month) << " " << year << endl;
cout << " Sun Mon Tues Wed Thur Fri Sat" << endl;
int weekday = firstDayOfMonth(year, month);
indentFirstLine(weekday);
int nDays = monthOfDays(year, month);
for (int i = 1; i <= nDays; i++){
printMonthDay(i);
if (weekday == Saturday) {
printLine();
}
weekday = (weekday + 1) % WEEK_DAY_TOTAL;
}
if (weekday != Sunday)
printLine();
}
void printBlank(int count) {
for (int i = 1; i <= count; i++) {
cout << " ";
}
}
void printMonthDay(int day) {
printBlank(3);
//cout << day;
printf("%2d", day);
}
string monthName(int month) {
return monthNamed[monthIndex(month)];
}
int monthIndex(int month) {
return month - 1;
}
int monthOfDays(int year, int month){
switch(month) {
case April: case June: case September: case November:
return 30;
case February:
return (28 + (isLeapYear(year)? 1 : 0));
default:
return 31;
}
// return monthOfTotalDays(monthIndex(month));
}
bool isLeapYear(int year){
return ( (year % 4 == 0) && (year % 100 != 0) ) ||
( (year % 400 == 0) );
}
int firstDayOfMonth(int year, int month){
int weekday = Monday;
for (int i = LOWER_LIMIT_YEAR; i < year; i++) {
weekday = (weekday + ONE_YEAR_OF_DAYS + (isLeapYear(i) ? 1 : 0)) % WEEK_DAY_TOTAL;
}
for (int i = January; i < month; i++) {
weekday = (weekday + monthOfDays(year, i)) % WEEK_DAY_TOTAL;
}
return weekday;
}
void indentFirstLine(int weekday){
for (int i = 0; i < weekday; i++){
printBlank(5);
}
}