inputs a date (e.g. July 4, 2008) and outputs the day of the week-根据输入日期判断星期几:
//inputs a date (e.g. July 4, 2008) and outputs the day of the week #include<iostream> #include<string> using namespace std; bool leapyear; void getInput(string& a,int& month,int& day,int& year); bool isLeapYear(int year); int getCenturyValue(int year); int getYearValue(int year); int getMonthValue(string a,int month,int year); int main() { int month,day,year,week; int centuryvalue,yearvalue,monthvalue; string a,b; getInput(a,month,day,year); leapyear = isLeapYear(year); cout<<"isLeapYear "<<leapyear<<endl; centuryvalue = getCenturyValue(year); cout<<"centuryvalue "<<centuryvalue<<endl; yearvalue = getYearValue(year); cout<<"yearvalue "<<yearvalue<<endl; monthvalue = getMonthValue(a,month,year); cout<<"monthvalue "<<monthvalue<<endl; week = (day + monthvalue + yearvalue + centuryvalue) % 7; cout<<"week "<<week<<endl; switch(week) { case 1: b = "Monday"; break; case 2: b = "Tuesday"; break; case 3: b = "Wednesday"; break; case 4: b = "Thursday"; break; case 5: b = "Firday"; break; case 6: b = "Saturday"; break; case 0: b = "Sunday"; break; default: cout<<"ERROR!"; } cout<<"The day of week is "<<b<<endl; return 0; } void getInput(string& a,int& month,int& day,int& year) { int number; cout<<"Enter the date:"<<endl; cout<<"1:month is english,"<<endl; cout<<"2:month is numbers."<<endl; cin>>number; switch(number) { case 1: cout<<"Enter the date(july 4 2008):\n"; cin>>a>>day>>year; break; case 2: cout<<"Enter the date(7 4 2008):\n"; cin>>month>>day>>year; break; default: cout<<"Error!"; } } bool isLeapYear(int year) { if(0 == year % 400 || (year % 4 ==0 && 0 != year % 100)) return 1; return 0; } int getCenturyValue(int year) { int tem; tem =(3 - (year / 100) % 4)*2; return tem; } int getYearValue(int year) { int tem; tem = (year % 100) / 4 + year % 100; return tem; } int getMonthValue(string a,int month,int year) { if(a == "october" || month == 10 || (a == "january" && (!leapyear)) || (month == 1 && (!leapyear))) return 0; else if(a == "may" || month == 5) return 1; else if(a == "august" || month == 8 || (a == "february" && (leapyear)) || (month == 2 && (leapyear))) return 2; else if(a == "march" || month == 3 || a == "november" || month == 11 || (a == "february" && (!leapyear)) || (month == 2 && (!leapyear))) return 3; else if(a == "june" || month == 6 ) return 4; else if(a == "september" || month == 9 || a == "december" || month == 12) return 5; else if(a == "april" || month == 4 || a == "july" || month == 7 || (a == "january" && (leapyear))) return 6; return -1; }
结果:
Enter the date: 1:month is english, 2:month is numbers. 2 Enter the date(7 4 2008): 9 1 2014 isLeapYear 0 centuryvalue 6 yearvalue 17 monthvalue 5 week 1 The day of week is Monday Enter the date: 1:month is english, 2:month is numbers. 2 Enter the date(7 4 2008): 7 4 2008 isLeapYear 1 centuryvalue 6 yearvalue 10 monthvalue 6 week 5 The day of week is Firday Enter the date: 1:month is english, 2:month is numbers. 1 Enter the date(july 4 2008): july 4 2008 isLeapYear 1 centuryvalue 6 yearvalue 10 monthvalue 6 week 5 The day of week is Firday
时间: 2024-10-31 20:53:27