【问题描述】定义一个结构体变量(包括年、月、日),编写程序,要求输入年、月、日,程序能计算并输出该日在本年中是第几天。注意闰年问题。
【输入形式】根据系统提示,输入年、月、日。
【输出形式】输出该日在本年中是第几天。
【样例输入】
2022 12 3
【样例输出】
input year,month,day:
12/3 is the 337th day in 2022.
【样例说明】根据系统提示,输入年、月、日。输出该日在本年中是第几天。
【评分标准】 结果完全正确得15分,每个测试点5分。提交程序名为:xt7-1-1.c或xt7-1-1.cpp
#include<iostream>
using namespace std;
struct Time
{
public:
int year;
int month;
int day;
};
bool leapYear(int year)
{
if(year%4==0&&year%100!=0)
{
return true;
}
if(year%400==0)
{
return true;
}
else return false;
}
int month_day(int month,int year)
{
switch(month)
{
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(leapYear(year))
{
return 29;
}
else
{
return 28;
}
default:
return 31;
}
}
int showTime(int day,int month,int year)
{
int mon;
for(int i=1;i<month;i++)
{
mon+=month_day(i,year);
}
return mon+day;
}
int main()
{
Time t1;
cout<<"input year,month,day:"<<endl;
cin>>t1.year>>t1.month>>t1.day;
int res=showTime(t1.day,t1.month,t1.year);
cout<<t1.month<<"/"<<t1.day<<" is the 337th day in "<<t1.year<<".";
cout<<res;
return 0;
}
要是习题答案用这个提交通过了,记得点个赞加个关注再走呀!!!以后会继续更新C++练习的!