【C++习题】给定某个年月日的值,例如1998年4月7日。计算出这一天属于该年的第几天。要求写出计算闰年的函数和计算日期的函数

问题描述:给定某个年月日的值,例如1998年4月7日。计算出这一天属于该年的第几天。要求写出计算闰年的函数和计算日期的函数。

代码展示:

IsLeapYear.cpp如下:

bool IsLeapYear(int year){
    
    
	if((year%400==0)||(year%4==0&&year%100!=0))
		return true;
	else
		return false;
}

Countday.cpp如下:

int Countday(int month,int day){
    
    
	int LeapYearDay[12]={
    
    31,29,31,30,31,30,31,31,30,31,30,31};
	int count=0;
	for(int i=0;i<month-1;i++)
		count+=LeapYearDay[i];
	count+=day;
	return count;
}

main.cpp如下:

#include<iostream>
using namespace std;
bool IsLeapYear(int year);
int Countday(int month,int day);
int main(){
    
    
	int year,month,day;
	int countout;
	cout<<"Please enter a date separated by spaces:(example:2020 9 1)"<<endl;
	cin>>year>>month>>day;
	cout<<"DATE:"<<year<<"/"<<month<<"/"<<day<<endl;
	if(IsLeapYear(year))
		countout=Countday(month,day);
	else
		if(month>2)
			countout=Countday(month,day)-1;
		else
			countout=Countday(month,day);
	cout<<"This date is the "<<countout<<"th day of the year!"<<endl; 	
	return 0;
} 

运行结果如下:

在这里插入图片描述

问题解决。

猜你喜欢

转载自blog.csdn.net/xfjssaw/article/details/109251689
今日推荐