只用if 和switch语句做的这个小程序能不能精简一点。
intyear,month,day,leap;cout<<"请输入年月日:";cin>>year>>month>>day;if(year%4==0&&year%100!=...
int year,month,day,leap;
cout<<"请输入年月日:";
cin>>year>>month>>day;
if(year%4==0&&year%100!=0)
{
leap=1;
}
else
{
if(year%400==0)
{
leap=1;
}
else
leap=0;
}
if(leap==0)
{
switch (month)
{
case 1: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<day<<"天"<<endl; break;
case 2: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<31+day<<"天"<<endl; break;
case 3: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<59+day<<"天"<<endl; break;
case 4: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<90+day<<"天"<<endl; break;
case 5: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<120+day<<"天"<<endl; break;
case 6: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<151+day<<"天"<<endl; break;
case 7: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<181+day<<"天"<<endl; break;
case 8: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<212+day<<"天"<<endl; break;
case 9: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<243+day<<"天"<<endl; break;
case 10: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<273+day<<"天"<<endl; break;
case 11: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<304+day<<"天"<<endl; break;
case 12: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<334+day<<"天"<<endl; break;
}
}
else
{
switch(month)
{
case 1: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<day<<"天"<<endl; break;
case 2: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<32+day<<"天"<<endl; break;
case 3: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<60+day<<"天"<<endl; break;
case 4: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<91+day<<"天"<<endl; break;
case 5: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<121+day<<"天"<<endl; break;
case 6: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<152+day<<"天"<<endl; break;
case 7: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<182+day<<"天"<<endl; break;
case 8: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<213+day<<"天"<<endl; break;
case 9: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<244+day<<"天"<<endl; break;
case 10: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<274+day<<"天"<<endl; break;
case 11: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<305+day<<"天"<<endl; break;
case 12: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<335+day<<"天"<<endl; break;
}
} 展开
cout<<"请输入年月日:";
cin>>year>>month>>day;
if(year%4==0&&year%100!=0)
{
leap=1;
}
else
{
if(year%400==0)
{
leap=1;
}
else
leap=0;
}
if(leap==0)
{
switch (month)
{
case 1: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<day<<"天"<<endl; break;
case 2: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<31+day<<"天"<<endl; break;
case 3: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<59+day<<"天"<<endl; break;
case 4: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<90+day<<"天"<<endl; break;
case 5: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<120+day<<"天"<<endl; break;
case 6: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<151+day<<"天"<<endl; break;
case 7: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<181+day<<"天"<<endl; break;
case 8: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<212+day<<"天"<<endl; break;
case 9: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<243+day<<"天"<<endl; break;
case 10: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<273+day<<"天"<<endl; break;
case 11: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<304+day<<"天"<<endl; break;
case 12: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<334+day<<"天"<<endl; break;
}
}
else
{
switch(month)
{
case 1: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<day<<"天"<<endl; break;
case 2: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<32+day<<"天"<<endl; break;
case 3: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<60+day<<"天"<<endl; break;
case 4: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<91+day<<"天"<<endl; break;
case 5: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<121+day<<"天"<<endl; break;
case 6: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<152+day<<"天"<<endl; break;
case 7: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<182+day<<"天"<<endl; break;
case 8: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<213+day<<"天"<<endl; break;
case 9: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<244+day<<"天"<<endl; break;
case 10: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<274+day<<"天"<<endl; break;
case 11: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<305+day<<"天"<<endl; break;
case 12: cout<<year<<month<<day<<" "<<"这是一年中的第:"<<335+day<<"天"<<endl; break;
}
} 展开
3个回答
展开全部
用数组和循环。可以改为下面这样(如果语法不对自己改一下):
int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31}
int day_of_year=0;
if (month < 1 || month >12)
{
count << "无效的月份。";
return;
}
if (year%4 == 0)
{
if (year%100 == 0)
{
if (year%400 == 0) leap = 1;
else leap = 0;
}
else leap = 1;
}
else leap = 0;
day_of_month[1] += leap; //如果闰年,二月增加1天。
if (day < 1 || day > days_of_month[month])
{
count << "无效的日期。";
return;
}
for (int i=0;i<month;i++)
{
day_of_year+= day_of_month[i];
}
day_of_year += day;
cout<<year<<month<<day<<" "<<"这是一年中的第:"<<day_of_year<<"天"<<endl;
int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31}
int day_of_year=0;
if (month < 1 || month >12)
{
count << "无效的月份。";
return;
}
if (year%4 == 0)
{
if (year%100 == 0)
{
if (year%400 == 0) leap = 1;
else leap = 0;
}
else leap = 1;
}
else leap = 0;
day_of_month[1] += leap; //如果闰年,二月增加1天。
if (day < 1 || day > days_of_month[month])
{
count << "无效的日期。";
return;
}
for (int i=0;i<month;i++)
{
day_of_year+= day_of_month[i];
}
day_of_year += day;
cout<<year<<month<<day<<" "<<"这是一年中的第:"<<day_of_year<<"天"<<endl;
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这问题用switch要麻烦很多得先用一个if else语句得到分数对应的字符 int i switch 似乎是不能判断的,你只能用 if else来做。。 用switch判断
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询