C程序设计(第五版)【谭浩强】第九章课后习题1

C程序设计(第五版)【谭浩强】第九章课后习题1

//结构体变量(年、月、日),计算该日在本年是第几天,注意闰年问题

#include<stdio.h>

//定义结构体
struct date
{
	int year;
	int month;
	int day;
};

//函数1判断是否是闰年
int func1(int year)
{
	int leap = 0;
	if ((year % 400 == 0)|| ((year % 4 == 0) && (year % 100 != 0)))
		leap = 1;  //leap=1表示该年是闰年

	return leap;
}

//函数2计算并输出结果
void func2(int year, int month, int day)
{
	//每月天数数组
	int dayy[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };

	//result中存放结果
	int result = 0;
	
	//引用函数1
	int leap = func1(year);
	printf("leap=%d\n", leap);
	if (leap == 1)  //闰年,2月28号后的天数要加1
	{
		if ((month > 2) || ((month == 2) && (day > 28)))
		{
			for (int i = 1; i < month; i++)
			{
				result = result+dayy[i] ;
			}
			result = result +day + 1 ;

		}
		
		
	}
	else
	{
		for (int i = 1; i < month; i++)
		{
			result = result + dayy[i];
		}
		result = result + day;
	}

	printf("the result is %d\n", result);

	
}

//主函数
int main()
{
	//定义结构体变量
	struct date days;

	//输入年月日
	printf("Please enter the date to be calculated (year, month, day)>>\n");
	scanf_s("%d%d%d", &days.year, &days.month, &days.day);

	//调用函数输出结果
	func2(days.year, days.month, days.day);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值