/*
Calculate the day of the week by entering the day of the month, calculate how many days of the year it is, and determine whether the year is a leap years
*/
#include <iostream>
using namespace Std;
struct TIME
{
int year;
int month;
int day;
unsigned int weekday;
};
void Initialtime (time & T);//input initialization times
void Show (Time & T);
int Weekdaycount (time & T);//Calculate the day of the week
int Daycount (time & T);//Calculation Day is the number of days of A.D.
int Daysyearcount (time & T);//Calculation Day is the number of days of the year
BOOL Isleapyear (time & T);//Judge whether the year is a leap
BOOL Check (time &t);//Check if the format is correct
int main ()
{
Time t;
Initialtime (t);
Show (t);
return 0;
}
BOOL Check (Time &t)
{
if (t.year <= 0 | | (t.month <= 0 | | t.month>12) | | T.day <= 0) return false;
else{
if (T.month = = 1 | | t.month = = 3 | | t.month = = 5 | | t.month = = 7
|| T.month = = 8 | | T.month = = 10 | | T.month = = && T.day >) return false;
Else
{
if (T.month = = 4 | | t.month = = 6 | | t.month = = 9 | | t.month = = 11
) && t.day > () return false;
Else
{
if (T.month = = 2) {
if (Isleapyear (t)) {
if (T.day >) return false; else return true;
}
Else
{
if (T.day >) return false; else return true;
}
}
}
}
}
}
void Initialtime (Time & T)
{
cout << "Enter the Time (year,month,day): \ n";
Cin >> T.year;
Cin.get ();
Cin>> T.month;
Cin.get ();
Cin >> T.day;
Cin.get ();
if (!check (t)) {cout << "Try again:\n"; initialtime (t);}
Else
T.weekday = Weekdaycount (t);
}
void Show (Time & T)
{
cout << "Year:" << t.year << "\ t";
cout << "Month:" << t.month << "\ t";
cout << "Day:" << t.day << "\ t";
cout << "Weekday:" << t.weekday << Endl;
cout << "This is a";
if (Isleapyear (t)) cout << "Leap"; else cout << "Nonleap";
cout << "year.\n";
cout << "Today is the" << daysyearcount (t) << "Days of the year.\n";
}
int Weekdaycount (time & T)
{
return Daycount (t)% 7;
}
int Daycount (time & T)
{
int days = 0;
Days = (t.year-1) * 365 + (T.YEAR-1)/4-(t.year-1)/100
+ (T.YEAR-1)/+ daysyearcount (t);
return days;
}
BOOL Isleapyear (time & T)
{
if (t.year% = = 0 && t.year% = 0) Return true;//is a multiple of four and not a multiple of 100, is a leap year
if (t.year% = = 0) return true;
else return false;
}
int Daysyearcount (time & T)
{
int days = 0;
int mtemp = t.month-1;
while (Mtemp > 0)
{
Switch (mtemp)
{
Case (1):
Case (3):
Case (5):
Case (7):
Case (8):
Case (10):
Case (12):
Days + = 31; Break
Case (4):
Case (6):
Case (9):
Case (11):
Days + = 30; Break
Case (2):
Days + = 28; Break
Default
Break
}
--mtemp;
}
if (Isleapyear (t)) ++days;//if it is a leap year, plus one day
Return days+t.day;//returns the calculated number of days plus the number of days in the month
}
The above is the implementation of independent functions of the code, the next improvement in class to modify the expression!
The C + + implementation determines the day of the week that an input date is a day of the year