中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C++ 實(shí)現(xiàn)判斷一個輸入日期是星期幾,是一年中的第幾天

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
/*
通過輸入年月日,計算出這一天是星期幾,計算這一天是這一年的多少天,判斷這一年是否為閏年
*/
#include<iostream>
using namespace std;

struct time
{
int year;
int month;
int day;
unsigned int weekday;
};

void initialtime(time & t);//輸入初始化時間
void Show(time & t);//顯示時間信息
int Weekdaycount(time & t);//計算當(dāng)日是星期幾
int Daycount(time & t);//計算當(dāng)日是第公元多少天
int Daysyearcount(time & t);//計算當(dāng)日是該年的第多少天
bool isleapyear(time & t);//判斷該年是不是閏年
bool check(time &t);//檢查時間格式是否正確

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 == 12) && t.day > 31)return false;
else
{
if ((t.month == 4 || t.month == 6 || t.month == 9 || t.month == 11
) && t.day > 30)return false;
else
{
if (t.month == 2) {
if (isleapyear(t)) {
if (t.day > 29)return false; else return true;
}
else
{
if (t.day > 28)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) / 400 + Daysyearcount(t);
return days;
}
bool isleapyear(time & t)
{
if (t.year % 4 == 0 && t.year % 100 != 0) return true;//年是四的倍數(shù)而且不是100的倍數(shù),是閏年
if (t.year % 400 == 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;//如果是閏年,再加上一天
return days+t.day;//返回計算的天數(shù)加上當(dāng)月的天數(shù)
}

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:java對properties文件進(jìn)行解析

下一篇:Java對json是否合法進(jìn)行格式校驗(yàn)