//用结构体整体作为参数
#include <iostream>
using namespace std;
struct Date
{
int year;
int month;
int day;
};
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int days(Date);
int main()
{
Date date;
cout<<"input year,month,day:";
cin>>date.year>>date.month>>date.day;
cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第";
cout<<days(date)<<"天。"<<endl;/*这句话不可以再写成cout<<date.month<<"月"<<date.day<<"日是"<<date.year<<"年的第"<<days
<<"天。"<<endl;*/
return 0;
}
int days(Date ymd)//函数定义需要形参
{
int n=0,i;
for(i=1;i<ymd.month;++i)//ymd本身就是一个Date数据类型
n+=d[i];//当月之前的天数
n+=ymd.day;//加上本月天数
if((ymd.year%4==0&&ymd.year%100!=0||ymd.year%400==0)&&ymd.month>=3)
n+=1;//闰年的二月要加一天
return n;
}
/*有一个警告||=== 生成: Debug in 524 (compiler: GNU GCC Compiler) ===|
D:\work\524\main.cpp||In function 'int main()':|
D:\work\524\main.cpp|19|warning: suggest parentheses around '&&' within '||' [-Wparentheses]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
||=== 运行: Debug in 524 (compiler: GNU GCC Compiler) ===|*/