【数组】2021年10月18日

#include<stdio.h>
#include<stdlib.h>

//[数组应用最广泛的功能就是查表]
//数组的两个属性:1.数组的类型  2.数组的元素个数
//数组在内存中是连续存储的。


//使用数组实现查询某年某月的天数

//【函数】判断是否是闰年。
bool IS_lead(int year)
{
	return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));
}

//【函数】输入年份和月份,返回该月天数。
int Get_YM_day(int year, int month)
{
	static const int ar[] = { 29,31,28,31,30,31,30,31,31,30,31,30,31 };
	//                        0  1  2  3  4  5  6  7  8  9  10 11 12

	if (year < 1 || month < 1 || month>12)return -1;//检验参数的合法性

	//if (IS_lead(year)&&month==2)
	if (month == 2 && IS_lead(year))
	{
		month = 0;
	}
	return ar[month];
}

//输入年月日,返回改日是该年的多少天
int Get_YMD_Count(int year, int month, int day)
{
	if (year < 1)return -1;                            //检验参数的合法性
	if (month < 1 || month>12)return -2;               //检验参数的合法性
	if (day<1 || day>Get_YM_day(year, month))return -3;//检验参数的合法性
	int count = 0;

	for (int s_month = 1; s_month < month; s_month++)
	{
		count += Get_YM_day(year, s_month);
	}

	return count + day;
}

int main()
{
	//数组的定义(两种方式)
	int ar[] = { 1,2,3,4,5,6,7,8 };
	int br[8];

	//数组的元素个数必须用常量来声明

	//c语言中的常量
	//10                  字面常量
	//#define LEN 10        宏常量
	//const int len = 10;   常变量
	//enum{ 10 };         枚举常量



	//计算数组的大小
	sizeof(ar) / sizeof(ar[0]);//ar在sizeof里面代表整个数组

	//【问题】数组为什么要从0下标开始?
	//【问题】数组能不能从1下标开始?

	//打印数组
	for (int i = 0; i < (sizeof(ar) / sizeof(ar[0])); i++)
	{
		printf("%d ", ar[i]);
	}

	printf("%d\n", Get_YMD_Count(2021, 10, 18));

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值