C++编程:给出年、月、日,计算该日是该年的第几天。

来源:百度知道 编辑:UC知道 时间:2024/07/04 07:31:54
用swich语句

#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去掉更简洁点,

#include<iostream>
using namespace std;

bool LeapYear(int year){ //判断是否为闰年,是则返回true,不是则返回false