输入日期计算这个日期在这一年里是多少天成功返回天数不成功返回-1(c++)

#include<iostream>
using namespace std;
int count(int year,int month, int day)
{
	int days = 0;
	if (month < 13 && day <= 31)
	{

		switch (month)
		{
		case 1:	
			days = day;
			break;
		case 2:
			days = 31 + day;
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			{
				if (day > 29)
				{
					days = 0;
				}
			}
			else
			{
				if (day > 28)
				{
					days = 0;
				}
			}
			break;
		case 3:
			days = 31 + day + 29;
			if (day > 31)
			{
				days = 0;
			}
			break;
		case 4:
			days = 31 * 2 + day + 29;
			if (day > 30)
			{
				days = 0;
			}
			break;
		case 5:
			days = 31 * 2 + day + 29 + 30;
			if (day > 31)
			{
				days = 0;
			}
			break;
		case 6:
			days = 31 * 3 + day + 29 + 30;
			if (day > 30)
			{
				days = 0;
			}
			break;
		case 7:
			days = 31 * 3 + day + 29 + 30 * 2;
			if (day > 31)
			{
				days = 0;
			}
			break;
		case 8:
			days = 31 * 4 + day + 29 + 30 * 2;
			if (day > 31)
			{
				days = 0;
			}
			break;
		case 9:
			days = 31 * 5 + day + 29 + 30 * 2;
			if (day > 30)
			{
				days = 0;
			}
			break;
		case 10:
			days = 31 * 5 + day + 29 + 30 * 3;
			if (day > 31)
			{
				days = 0;
			}
			break;
		case 11:
			days = 31 * 6 + day + 29 + 30 * 3;
			if (day > 30)
			{
				days = 0;
			}
			break;
		case 12:
			days = 31 * 6 + day + 29 + 30 * 4;
			if (day > 31)
			{
				days = 0;
			}
			break;
		default:
			break;
		}
	}
	else
	{
		days = 0;
	}
	return days;
}
int main()
{
	int days;
	int year, month, day;
	while (cin >> year >> month >> day)
	{
		days = count(year,month, day);
		if (days == 0)
		{
			cout << -1 << endl;
		}
		else
		{
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			{

				cout << days << endl;
			}
			else
			{
				cout << days - 1 << endl;

			}
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 以下是Python代码实现: ```python year = int(input("请输入年份:")) month = int(input("请输入月份:")) day = int(input("请输入日期:")) # 计算是否为闰年 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: leap = 1 else: leap = 0 # 计算天数 days = 0 for i in range(1, month): if i == 2: days += 28 + leap elif i in [4, 6, 9, 11]: days += 30 else: days += 31 days += day print("该日期是这一年的第%d。" % days) ``` 用户需要输入年份、月份和日期,程序会判断是否为闰年,然后根据月份计算出该日期是这一年的第几,并输出结果。 ### 回答2: 为了编写一个程序计算输入日期是这一年的第几,我们需要按照以下步骤操作: 第一步:引入用户输入。我们需要使用input函数来获取用户输入日期,例如“2022-02-22”。 第二步:使用split函数来将日期字符串从“-”分隔符划分为单独的年、月、日数值,以方便后续计算。 第三步:使用datetime库的date函数将输入的年、月、日转换为日期对象。 第四步:使用date对象内置的toordinal()函数来获取该日期在公元1年1月1日后的天数。 第五步:计算日期是这一年的第几。由于1月1日是这一年的第一,我们可以用该日期天数减去1月1日的天数即可。 第六步:输出结果,告诉用户输入日期是这一年的第几。 下面是一个Python程序的示例代码,请参考: ``` import datetime # 输入日期 datestr = input("请输入一个日期(格式:yyyy-mm-dd):") # 分隔年月日 year, month, day = map(int, datestr.split('-')) # 将年月日转换为日期对象 date1 = datetime.date(year, month, day) # 计算日期是公元1年1月1日后的天数 days = date1.toordinal() - datetime.date(1, 1, 1).toordinal() # 计算日期是这一年的第几 yearday = days - datetime.date(year, 1, 1).toordinal() + 1 # 输出结果 print("输入日期在本年的第 {:d} 。".format(yearday)) ``` 以上代码能够计算2022年2月22日是这一年的第53。 ### 回答3: 作为一名AI语言模型,我很高兴回答这个问题。 要编写一个程序,用户输入日期计算日期是这一年的第几,需要先了解以下几点: 1. 闰年和平年的概念:闰年是指能够被4整除但不能被100整除或者能够被400整除的年份,平年则是指不能被4整除或者能够被100整除但不能被400整除的年份。 2. 月份和天数的对应关系:每月的天数不同,需要知道每个月有多少。 基于上述知识点,可以编写以下程序: ``` #include <iostream> using namespace std; int isLeapYear(int year) // 判断是否闰年 { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 1; return 0; } int main() { int year, month, day; int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int days = 0; cout << "请输入日期(格式:年月日,用空格隔开):" << endl; cin >> year >> month >> day; // 判断是否闰年并修改2月份天数 if (isLeapYear(year)) days_in_month[1] = 29; // 累加月份之前的天数 for (int i = 0; i < month - 1; i++) { days += days_in_month[i]; } // 累加当月已过天数 days += day; cout << "该日期是这一年的第" << days << "。" << endl; return 0; } ``` 程序的主要步骤为: 1. 用户输入日期; 2. 判断该年是否闰年,如果是闰年则将2月份的天数改为29; 3. 累加月份之前的天数; 4. 累加当月已过天数; 5. 输出结果。 如果用户输入日期格式不正确,程序需要进行错误处理。 以上就是如何编写一个计算日期是这一年的第几的程序,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三少爷的剑!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值