今天看到这么一个题让算下用户输入的年月日来判断是这一年的多少天,要注意闰年的情况,代码如下:
#include<iostream> using namespace std;
struct sDate { unsigned int nYear; unsigned int nMouth; unsigned int nDay; };
bool GetYearType( int nYear ) { if ( ( (0 == ( ( nYear) % 4 ) )&& ( 0 != ( ( nYear) % 100 ) ) ) || ( nYear % 400 ) ) { return true; } else { return false; } }
int GetMouth( sDate *p ) {
int nNum; switch ( p ->nMouth ) { case 1: if ( 0 == p ->nDay || p ->nDay >= 32 ) { cout<<"一月份天数输入错误了"<<endl; exit(0) ; } nNum = p ->nDay; break; case 2: if( true == GetYearType( p ->nYear ) ) { if ( 0 == p ->nDay || p ->nDay >= 30 ) { cout<<"二月份天数输入错误了"<<endl; exit(0) ; } } else { if ( 0 == p ->nDay || p ->nDay >= 29 ) { cout<<"二月份天数输入错误了"<<endl; exit(0); }
} nNum = 31 + p ->nDay; break; case 3: if ( 0 == p ->nDay || p ->nDay >= 32) { cout<<"三月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + p ->nDay ; break; } else { nNum = 31 + 28 + p ->nDay ; break; } case 4:
if ( 0 == p ->nDay || p ->nDay >= 31) { cout<<"四月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + p ->nDay ; break; } else { nNum = 31 + 28 + 31 + p ->nDay ; break; } case 5: if ( 0 == p ->nDay || p ->nDay >= 32) { cout<<"五月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + p ->nDay; break; } else { nNum = 31 + 28 + 31 + 30 + p ->nDay; break; } case 6: if ( 0 == p ->nDay || p ->nDay >= 31) { cout<<"六月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + p ->nDay ; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + p ->nDay ; break; } case 7: if ( 0 == p ->nDay || p ->nDay >= 32) { cout<<"七月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + 30 + p ->nDay; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + 30 + p ->nDay; break; } case 8: if ( 0 == p ->nDay || p ->nDay >= 32) { cout<<"八月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + p ->nDay ; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + p ->nDay ; break; } case 9: if ( 0 == p ->nDay || p ->nDay >= 31) { cout<<"九月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + p ->nDay ; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + p ->nDay ; break; } case 10: if ( 0 == p ->nDay || p ->nDay >= 32) { cout<<"十月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + p ->nDay ; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + p ->nDay ; break; } case 11: if ( 0 == p ->nDay || p ->nDay >= 31) { cout<<"十一月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + p ->nDay; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + p ->nDay; break; } case 12: if ( 0 == p ->nDay || p ->nDay >= 32) { cout<<"十二月份天数输入错误了"<<endl; exit(0); } if( true == GetYearType( p ->nYear ) ) { nNum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + p ->nDay; break; } else { nNum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + p ->nDay; break; } default: { cout<<"其他月份不是一年之内的"; } } return nNum; }
int main() { sDate Date = {0,0,0}; sDate *P = &Date; cout<<"请输入年月日:"<<endl; cout<<"输入的年为:"<<endl; cin>>P->nYear; cout<<"输入的月为:"<<endl; cin>>P->nMouth; cout<<"输入的日为:"<<endl; cin>>P->nDay; if ( 0 == P->nYear ) { cout<<"年不能为0"<<endl; }
if ( true == GetYearType( P ->nYear ) ) { cout<<"你输入的日期是当年的"<<GetMouth(P) <<"天"<<endl;; } else { cout<<"你输入的日期是当年的"<<GetMouth(P)<<"天"<<endl; }
return 0;
}
|
阅读(1571) | 评论(0) | 转发(0) |