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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
const double JDN_CONST = 1720994.5;
const double YEAR_CONST = 365.25;
const double MONTH_CONST = 30.6001;
long Res1(int day, int month, int year) //function to find intRes1 using formula intRes1 = ((2 - year) / (100 + year)) / 400
{
bool isGregorian = ( (year > 1582) || ((year == 1582) && (month >= 10) && (day > 15)) ); //determines the truth value of if the date is using the Gregorian calendar
double a = 2 - year;
double b = 100 + year;
double c = (a / b) / 400;
cout << "intRes1 : " << floor(c) << endl;
if(isGregorian) {return floor(c);}
else return 0;
}
long Res2(int year) //function to find intRes2 using formula 365.25 * year
{
cout << "intRes2 : " << YEAR_CONST * year << endl;
return static_cast<int>(YEAR_CONST * year);
}
long Res3(int month) //function to find intRes3 using formula 30.6001 * (month + 1)
{
cout << "intRes3 : " << static_cast<int>(MONTH_CONST * (month + 1)) << endl;
return static_cast<int>(MONTH_CONST * (month + 1));
}
long JulianDayNumber(int day, int month, int year) //function to find the JDN using formula JDN = intRes1 + intRes2 + intRes3 + day + 1720994.5
{
cout << "JDN : " << Res1(day, month, year) + Res2(year) + Res3(month) + day + JDN_CONST << endl;
return static_cast<int>(Res1(day, month, year) + Res2(year) + Res3(month) + day + JDN_CONST);
}
string DayOfWeek(int day, int month, int year) //function to find the day of the week of a given date using formula (JDN + 1) % 7. this outputs an int (0 <= i <= 6) where 0 corresponds to Sunday, 1 to Monday, etc.
{
long JDN = JulianDayNumber(day, month, year);
int dayInt = ((JDN + 1) % 7);
cout << "dayInt : " << dayInt << endl;
if(dayInt == 0) return "Sunday";
else if(dayInt == 1) return "Monday";
else if(dayInt == 2) return "Tuesday";
else if(dayInt == 3) return "Wednesday";
else if(dayInt == 4) return "Thursday";
else if(dayInt == 5) return "Friday";
else if(dayInt == 6) return "Saturday";
}
int main()
{
int day;
int month;
int year;
ifstream inData;
inData.open("indata7.txt");
if(inData.is_open()) //if the file indata7.txt is found, reads from file. if not, takes user input
{
while(!inData.eof())
{
inData >> month >> day >> year;
cout << "Date: " << month << "/" << day << "/" << year << ", Day of week: " << DayOfWeek(day, month, year) << endl;
}
}else //user input
{
cout << "input date in order month day year (example input: '2 21 2019')" << endl << "input 'stop' to end input" << endl;
cout << " " << endl;
while(month > 0)
{
cin >> month >> day >> year;
cout << "Date: " << month << "/" << day << "/" << year << ", Day of week: " << DayOfWeek(day, month, year) << endl;
cout << " " << endl;
}
}
return 0;
}
|