C++计算出该日是本年中的第几天

C++计算出该日是本年中的第几天

任务描述

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

测试输入:`

2000 12 20

预期输出:

12/20 is the 355th day in 2000

测试输入:

1998 10 1

预期输出:

10/1 is the 274th day in 1998

源代码:

#include <iostream>
using namespace std;
struct
{ 
int year;
int month;
int day;
}date;

int main()
{
int days;
cin>>date.year>>date.month>>date.day;

// 请在此添加代码
    /********** Begin *********/
	days=0;
    int i;
	int year1[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int year2[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    if((date.year%4==0&&date.year%100!=0)||(date.year%400==0)){
		for(i=0;i<date.month-1;i++){
			days+=year2[i];
		}
	}else{
		for(i=0;i<date.month-1;i++){
			days+=year1[i];
		}
	}
    days+=date.day;
    /********** End **********/

cout<<date.month<<"/"<<date.day<<" is the "<<days<<"th day in "<<date.year<<"."<<endl;
return 0;
}

原文链接: https://www.cnblogs.com/lightice/p/12691787.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++计算出该日是本年中的第几天

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/195832

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午7:05
下一篇 2023年2月12日 下午7:05

相关推荐