第7章 指针 第6题

题目:

Julian历法是用年及这一年中的第几天来表示日期。

设计一个函数将Julian历法表示的日期转换成月和日,如Mar8(注意闰年的问题)。

函数返回一个字符串,即转换后的月和日。如果参数有错,如天数为第370天,返回NULL。


代码:

#include <iostream>
using namespace std;

int function(int year, int days, int *a);

int main()
{
	cout << "把Julian历法转换为年月日" << endl << endl;

	int year, days, a[8] = { 00000000 };

	cout << "请问今年的年份是:";
	cin >> year;
	cout << "请问今天是" << year << "年的第几天:";
	cin >> days;
		
	function(year, days, a);

	cout << endl << "今天的日期是:";
	for (int i = 0; i < 8; ++i) cout << a[i];
	cout << endl << endl;

	system("pause;");
	return 0;
}

int function(int year, int days, int *a)
{
	if (year < 0)
	{
		cout << "年份错误!";
		return NULL;
		system("pause;");
		return 0;
	}
	
	if (days < 1)
	{
		cout << "天数错误!";
		return NULL;
		system("pause;");
		return 0;
	}
	


	bool judge;

	if (year % 100 == 0)
	{
		if (year % 400 == 0) judge = true;
		else judge = false;
	}
	else
	{
		if (year % 4 == 0) judge = true;
		else judge = false;
	}



	if (judge == true)
	{
		if (days > 366)
		{
			return NULL;
			system("pause;");
			return 0;
		}

		int month = 0;
		for (int i : {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31})
		{
			month = month + 1;
			
			if (days < i)
			{
				int j;

				//年份(4位)
				for (j = 3; ; j--)
				{
					a[j] = year % 10;
					year = year / 10;
					if (year == 0) break;
				}

				//月份(2位)
				for (j = 5; ; j--)
				{
					a[j] = month % 10;
					month = month / 10;
					if (month == 0) break;
				}

				//日数(2位)
				for (j = 7; ; j--)
				{
					a[j] = days % 10;
					days = days / 10;
					if (days == 0) break;
				}

				break;
			}
			
			else days = days - i;
		}
	}

	else
	{
		if (days > 365)
		{
			return NULL;
			system("pause;");
			return 0;
		}

		int month = 0;
		for (int i : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31})
		{
			month = month + 1;

			if (days < i)
			{
				int j;

				//年份(4位)
				for (j = 3; ; j--)
				{
					a[j] = year % 10;
					year = year / 10;
					if (year == 0) break;
				}

				//月份(2位)
				for (j = 5; ; j--)
				{
					a[j] = month % 10;
					month = month / 10;
					if (month == 0) break;
				}

				//日数(2位)
				for (j = 7; ; j--)
				{
					a[j] = days % 10;
					days = days / 10;
					if (days == 0) break;
				}

				break;
			}

			else days = days - i;
		}
	}
	
	return *a;
}
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值