【c++习题】输出某日在本年中是第几天(第七章)

【问题描述】定义一个结构体变量(包括年、月、日),编写程序,要求输入年、月、日,程序能计算并输出该日在本年中是第几天。注意闰年问题。

【输入形式】根据系统提示,输入年、月、日。

【输出形式】输出该日在本年中是第几天。

【样例输入】

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++练习的!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值