1.用户输入年和月,用switch语句实现显示该月有多少天
2个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
main()
{
int a,b;
printf("please enter the data\n");
scanf("%d,%d",&a,&b);
switch(b)
{
case 1:printf("%d year %d month have 31 days\n",a,b);
break;
case 2:
if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0))
{
printf("%d year %d month have 29 days\n",a,b);
}
else
printf("%d year %d month have 28 days\n",a,b);
break;
case 3:printf("%d year %d month have 31 days\n",a,b);
break;
case 4:printf("%d year %d month have 30 days\n",a,b);
break;
case 5:printf("%d year %d month have 31 days\n",a,b);
break;
case 6:printf("%d year %d month have 30 days\n",a,b);
break;
case 7:printf("%d year %d month have 31 days\n",a,b);
break;
case 8:printf("%d year %d month have 31 days\n",a,b);
break;
case 9:printf("%d year %d month have 30 days\n",a,b);
break;
case 10:printf("%d year %d month have 31 days\n",a,b);
break;
case 11:printf("%d year %d month have 30 days\n",a,b);
break;
case 12:printf("%d year %d month have 31 days\n",a,b);
break;
default:printf("error\n");
}
}
注意年和月之间用逗号分开;经VC测试 程序可行
#include <stdlib.h>
main()
{
int a,b;
printf("please enter the data\n");
scanf("%d,%d",&a,&b);
switch(b)
{
case 1:printf("%d year %d month have 31 days\n",a,b);
break;
case 2:
if((year % 400 == 0)||(year % 4 == 0)&&(year % 100 != 0))
{
printf("%d year %d month have 29 days\n",a,b);
}
else
printf("%d year %d month have 28 days\n",a,b);
break;
case 3:printf("%d year %d month have 31 days\n",a,b);
break;
case 4:printf("%d year %d month have 30 days\n",a,b);
break;
case 5:printf("%d year %d month have 31 days\n",a,b);
break;
case 6:printf("%d year %d month have 30 days\n",a,b);
break;
case 7:printf("%d year %d month have 31 days\n",a,b);
break;
case 8:printf("%d year %d month have 31 days\n",a,b);
break;
case 9:printf("%d year %d month have 30 days\n",a,b);
break;
case 10:printf("%d year %d month have 31 days\n",a,b);
break;
case 11:printf("%d year %d month have 30 days\n",a,b);
break;
case 12:printf("%d year %d month have 31 days\n",a,b);
break;
default:printf("error\n");
}
}
注意年和月之间用逗号分开;经VC测试 程序可行
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream.h>
/**
* 输入年份和月来统计当月有多少天
*/
bool isleapYear(int year){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
int getDays(int year,int month){
int day = 0;
if(month < 8){
for(int i = 1;i <= 7;i++){
if(month == i){
if(i % 2 == 0){
if(i == 2){
if(isleapYear(year))
day = 29;
else
day = 28;
}else{
day = 30;
}
}else{
cout<<"hello";
day = 31;
}
break;
}
}
}else{
for(int i = 8;i <= 12;i++){
if(month == i){
if(i % 2 == 0){
day = 31;
}else{
day = 30;
}
break;
}
}
}
return day;
}
int dayOfMonth(int year,int month){
return getDays(year,month);
}
int main(){
int year,month;
cout<<"请分别输入年份和月分:\n";
cin>>year>>month;
int days = dayOfMonth(year,month);
cout<<year<<"年"<<month<<"月有"<<days<<"天"<<endl;
return 0;
}
/**
* 输入年份和月来统计当月有多少天
*/
bool isleapYear(int year){
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return true;
else
return false;
}
int getDays(int year,int month){
int day = 0;
if(month < 8){
for(int i = 1;i <= 7;i++){
if(month == i){
if(i % 2 == 0){
if(i == 2){
if(isleapYear(year))
day = 29;
else
day = 28;
}else{
day = 30;
}
}else{
cout<<"hello";
day = 31;
}
break;
}
}
}else{
for(int i = 8;i <= 12;i++){
if(month == i){
if(i % 2 == 0){
day = 31;
}else{
day = 30;
}
break;
}
}
}
return day;
}
int dayOfMonth(int year,int month){
return getDays(year,month);
}
int main(){
int year,month;
cout<<"请分别输入年份和月分:\n";
cin>>year>>month;
int days = dayOfMonth(year,month);
cout<<year<<"年"<<month<<"月有"<<days<<"天"<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |