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
|
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
bool isLeap(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
int days_in_month(int month, int year)
{
if (month == 2)
{
if (isLeap(year))
{
return 29;
}
else
{
return 28;
}
}
else
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
break;
}
}
return 0;
}
int days_in_year(int day, int month, int year)
{
int days = 0;
for (int i = 1; i < month; i++)
{
days += days_in_month(i, year);
}
days += day;
return days;
}
string day_of_week(int year, int day, int month)
{
int leapAmount = (int)year / 4;
//Calculating no of days between years
long dayVal = (year - leapAmount) * 365 + leapAmount * 366;
//Adding the no of days in the remaining months of given year
for (int i = 1; i < month; i++)
{
dayVal += days_in_month(i, year);
}
//Adding the no of days entered
dayVal += day - 2;
int day_of_week = (int)dayVal % 7;
switch (day_of_week) {
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
case 0:
return "Saturday";
case 1:
return "Sunday";
default:
break;
}
return "";
}
int day_value(int day, int month, int year){
int day_value = 0;
for (int i = 1900; i < year; i++)
{
if (isLeap(i) == true)
{
day_value += 366;
}
else
{
day_value += 365;
}
}
day_value += days_in_year(day, month, year);
return day_value;
}
int main()
{
int day=0, month=0, year=0;
ifstream input;
input.open("input.txt");
if (input.is_open())
{
input >> day >> month >> year;
input.close();
}
ofstream output("output.txt");
if (output.is_open())
{
output << day_of_week(day, month, year) << " " << day << "/" << month << "/" << year << " Has a day value of " << day_value(day, month, year) << "\n";
output.close();
}
}
|