【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. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-27 13:56:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-27 13:56:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-27 13:56:06       87 阅读
  4. Python语言-面向对象

    2024-03-27 13:56:06       96 阅读

热门阅读

  1. c# 设置图片透明度

    2024-03-27 13:56:06       41 阅读
  2. 【LAMMPS学习】五、LAMMPS命令(1) LAMMPS 输入脚本

    2024-03-27 13:56:06       38 阅读
  3. 数据库(四)

    2024-03-27 13:56:06       38 阅读
  4. Spring和Spring Boot的区别

    2024-03-27 13:56:06       44 阅读