This article illustrates a C + + custom function that determines how many days of the year a certain month of the year is. Share to everyone for your reference, specific as follows:
* * Author: Liu Tongbin * Completion date: November 30, 2012 * Version number: v1.0 * Input Description: * Problem Description: Write function to determine the day of the year in a certain year is the first days, in the main function called the function.
* Program Output: * Problem analysis: slightly * algorithm design: Slightly * * #include <iostream> using namespace std;
int main () {void f (int year,int month,int day);
int year,month,day;
cout<< "Please enter the month/year date:" <<endl;
f (year,month,day);
cout<<endl;
return 0;
//Judge the date entered is the first day of the year!
void f (int year,int Month,int day) {int. f1 (int year,int month,int day);
int Leap (int year);
int t=0,s=0,i; int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
Defines the number of days of the array for each month int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};
while (1) {cin>>year>>month>>day;
if (F1 (Year,month,day)) {if (Leap (year)) {for (i=0;i<=month-2;i++) {t=t+b[i];
} S=t+day;
else {for (i=0;i<=month-2;i++) {t=t+a[i];
} S=t+day; cout<< "This is the first" <<s<< "Day of the year!
"<<endl;
Break
else cout<< "Input error, please re-enter:" <<endl;
}//Judge whether it is a leap year! int leap(int year) {if (year%4==0&&year%400==0) | | |
(year&4==0&&year%100!=0))
{return true;
else return false;
//Determine if the input is legal!
int F1 (int year,int month,int day) {int-days (int year,int month,int day);
if (year>0) {if (month>=1&&month<=12) {if days (Year,month,day)) {return true;
else return false;
else return false;
else return false;
//Judge If the input day is legal!
int days (int year,int month,int day) {int Leap (int year); if (month==1| | month==3| | month==5| | month==7| | month==8| |month==10| |
MONTH==12) {if (day>0&&day<=31) {return true;
else return false; else if (month==2| | month==4| | month==6| | month==9| |
month==11) {if (day>0&&day<=30) {return true;
else return false;
else {if (Leap (year) {if (day>0&&day<=29) {return true;
else return false; } else {if (day>0&&day<=28) {return true;
else return false;
}
}
}
The effect is as shown in the following illustration:
PS: Here again for you to recommend a C language online format tool, I believe that in future development can be used:
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json
I hope this article will help you with C + + program design.