第1个回答 2009-10-14
#include<iostream>
using namespace std;
int d[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31} };
int days(int m,int a=0)
{
int sumday=0;
for(int i=0;i<m-1;++i)
sumday+=d[a][i];
return sumday;
}
int main()
{
int day=0,month=0,year=0;
cout<<"day :";cin>>day;
cout<<"month :";cin>>month;
cout<<"year :";cin>>year;
int a=0;
if((year%4==0&&year%100!=0)||year%400==0)a=1;
switch(month)
{
case 1:case 2:
case 3:case 4:
case 5:case 6:
case 7:case 8:
case 9:case 10:
case 11:case 12: cout<<days(month,a)+day<<endl;
default: break;
}
}
有点牵强,把switch去掉更简洁点,本回答被提问者采纳
第2个回答 2009-10-14
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
bool leapYear(int year)
{
if (year%4==0 && year%100!=0)
return true;
if (year%400==0)
return true;
return false;
}
void main()
{
int year,month,day;
int MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
while (true)
{
cout<<"请输入年(0~3000)、月(1~12)、日(1~月份而定): "<<endl;
cin>>year>>month>>day;
if (cin.good() && (year>=0&&year<=3000) && (month>=1&&month<=12)
&& (day>=1&&day<=MonthDay[month]))
{
break;
}
cout<<"输入有误,";
cin.clear();
cin.sync();
}
cout<<year<<"年"<<month<<"月"<<day<<"日是该年的第 ";
switch(month)
{
case 12:day+=30;
case 11:day+=31;
case 10:day+=30;
case 9:day+=31;
case 8:day+=31;
case 7:day+=30;
case 6:day+=31;
case 5:day+=30;
case 4:day+=31;
case 3:day+=((leapYear(year))?29:28);
case 2:day+=31;
case 1:break;
default: cout<<"Exception!"<<endl;
}
cout<<day<<" 天!"<<endl;
}