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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
// C++ Calendar.cpp : main project file.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int MIN = 1582;
const int MAX = 9999;
int daysinmonths[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const string MONTHS[13] = { " ", "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" };
bool isleapyear(const int year);
void printyear(const int year);
int dayofweek(const int month, const int year);
int main()
{
int year1;
ifstream File;
File.open("calendar.txt");
cout << "Please enter a year between 1582 and 9999\nto obtain the full calendar year of that date.\n";
cin >> year1;
while (year1 < MIN || year1 > MAX){
cout << "Incorrect input, please enter a year within the range of 1582 through 9999!\n";
cin >> year1;
}
printyear(year1);
return 0;
}
bool isleapyear(const int year)
{
if ((year % 400 == 0) == true &&! (year % 100 == 0)){
daysinmonths[2] = 29;
cout << "This is a leap year.\n" << endl;
}
else if ((year % 4 == 0) == true){
daysinmonths[2] = 29;
cout << "This is a leap year.\n" << endl;
}
return year;
}
int dayofweek(int month, int year)
{
int t[12] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
int y = year - (month < 2);
y = (y + y / 4 - y / 100 + y / 400 + t[month] + 1) % 7;
// y = 0 for Sunday, 1 for Monday.. etc.
return y;
}
void printyear(const int year)
{
isleapyear(year);
int length = 0, days, skip ;
cout << setw(17) << year;
for(int month=1;month<13;month++)
{
days=daysinmonths[month];
skip = dayofweek(month-1, year);
length = MONTHS[month].length();
length = (30 - length) / 2 + length;
cout << endl << setw(length) << MONTHS[month] << endl;
cout << " Sun Mon Tue Wed Thu Fri Sat" << endl;
cout << " ";
for(int x = 0;x < skip;x++)
cout << " ";
for (int count = 0; count < daysinmonths[month]; count++)
{
if ((count + skip) % 7 == 0 )
{
cout << endl << " "; // Make starting number under 'Sun'
}
if(count+1 < 10)
cout << " "; // To keep numbers aligned
cout << count+1 << " ";
}
cout << endl << endl;
}
}
|