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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
// Zeller Congruence.cpp : main project file.
#include <iostream>
#include <string>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
void Sort_Date(string Date, int &month, int &day, int &year);
bool ck_date(int month, int day, int year, int days[12], string Month[12]);
int Zeller_Congruence(int month, int day, int year);
char WaitKey();
int main()
{
int month = 0, day = 0, year = 0, Date_falls_on;
bool ok = false;
string Date;
cout << "This program will ask for a specific date, and then," << endl << "will let you know what day it fell on." << endl << endl << endl;
string Month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
string Day_of_week[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
do
{
do
{
cout << endl << "Enter the date as 'MM/DD/YYYY' or 'MM-DD-YYYY'" << endl << "Enter a '0', to end program." << endl << endl;
cin >> Date;
if (Date != "0")
{
if (((Date[2] == '/' && Date[5] == '/') || (Date[2] == '-' && Date[5] == '-')) && Date.length() == 10)
{
Sort_Date(Date, month, day, year);
if (year < 1582)
cout << "This program only works correctly when using the Gregorian Calendar" << endl << " which was started in 1582. Please enter a different date.." << endl << endl;
ok = ck_date(month, day, year, days, Month);
}
else
cout << "Your date format, is not correct. Please re-enter a date.." << endl << endl;
}
else
ok = true;
} while (!ok);
if (Date != "0")
{
Date_falls_on = Zeller_Congruence(month, day, year);
cout << endl << endl << Month[month - 1] << " " << day << ", " << year << ", falls on a " << Day_of_week[Date_falls_on] << ", in a ";
bool A_Leap_Year = ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
if (!A_Leap_Year)
cout << "non-";
cout << "leap year!" << endl << endl;
}
} while (Date != "0");
cout << "\t\tThe program is now closed. Bye..." << endl;
WaitKey();
return 0;
}
void Sort_Date(string Date, int &month, int &day, int &year)
{
month = ((Date[0]) - 48) * 10;
month += Date[1] - 48;
day = ((Date[3]) - 48) * 10;
day += Date[4] - 48;
year = ((Date[6]) - 48) * 1000;
year += ((Date[7]) - 48) * 100;
year += ((Date[8]) - 48) * 10;
year += Date[9] - 48;
}
bool ck_date(int month, int day, int year, int days[12], string Month[12])
{
bool ck = true;
bool A_Leap_Year = ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
if (A_Leap_Year)
days[1] = 29;
if (year < 1582)
ck = false;
if (month < 1 || month >12)
{
ck = false;
if (month < 1)
cout << "You have to give me something I can work with. Try again.." << endl << endl;
if (month>12)
cout << "Don't know where you're from, but we only have 12 months.." << endl << "Please try again." << endl << endl;
}
if (ck && ( day < 1 || day > days[month - 1]))
{
ck = false;
if (day < 1)
cout << "You have to give me something I can work with. Try again.." << endl << endl;
if (day > days[month - 1])
cout << "Sorry to inform you, but there are only " << days[month - 1] << " days in " << Month[month - 1] << ".. " << endl << "Please try again." << endl << endl;
}
return ck;
}
int Zeller_Congruence(int month, int day, int year)
{
int week_day;
if (month < 3)
{
year--;
month += 12;
}
week_day = (((13 * month + 3) / 5 + day + year + year / 4 - year / 100 + year / 400 + 1) % 7);
return week_day;
}
char WaitKey()
{
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD irInputRecord;
DWORD dwEventsRead;
CHAR cChar;
cout << endl << endl << "\tPress 'ENTER', to close this program." << endl << endl;
while (ReadConsoleInputA(hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
if (irInputRecord.EventType == KEY_EVENT
&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
{
cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
ReadConsoleInputA(hStdin, &irInputRecord, 1, &dwEventsRead); /* Read key release */
return cChar;
}
return EOF;
}
|