C++ 實現判斷一個輸入日期是星期幾,是一年中的第幾天

bgn4 9年前發布 | 2K 次閱讀 C/C++

/
通過輸入年月日,計算出這一天是星期幾,計算這一天是這一年的多少天,判斷這一年是否為閏年/

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);//計算當日是星期幾 int Daycount(time & t);//計算當日是第公元多少天 int Daysyearcount(time & t);//計算當日是該年的第多少天 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;//年是四的倍數而且不是100的倍數,是閏年 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;//返回計算的天數加上當月的天數 }</pre>
 本文由用戶 bgn4 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!