C++从键盘输入年月,输出该月有多少天

从键盘输入年月,输出该月有多少天。(4、6、9、11各30天,1、3、5、7、8、10、12各31天,平年2月28天,闰年2月29天)

#include<iostream> using namespace std; bool leapYear(int year) { if(year%4==0&&year%100!=0||year%4==0&&year%400==0) return true; else return false; }

用switch判断下就好了~~你试试看~

#include<iostream>
using namespace std;

bool leapYear(int year)
{
if(year%4==0&&year%100!=0||year%4==0&&year%400==0)
return true;
else
return false;
}
int main()
{

int year,month,day;

cout<<"请输入年份:";
cin>>year;
cout<<"请输入月份:";
cin>>month;

switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if(leapYear(year))
day = 29;
else
day = 28;
break;
default:
break;
}

cout<<year<<"年"<<month<<"月份有"<<day<<"天"<<endl;

return 0;

}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-11-06
思路:
1.先判断输入年份是否为闰年
2.判断输入的月份
3.对应于闰年和平年的月份天数

#include <iostream>
using namespace std;

bool leapYear(int year)
{
if(year%4==0&&year%100!=0||year%4==0&&year%400==0)
return true;
else
return false;
}

int main()
{
int year, month, days;
leapdays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
unleapdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

printf("Enter date(yy-mm): ");
scanf("%d-%d", &year, &month);

if ( leapYear(year) )
days = leapdays[month-1]; // 闰年的月份天数
else
days = unleapdays[month-1]; // 平年的月份天数

printf("%d-%d has %d days.\n", year, month, days);
return 0;
}本回答被网友采纳
相似回答
大家正在搜