C语言 计算日期是当年的第几天

/*输入一个日期,包括年 月 日 计算并输出这一天是这一年的第几天,要求利用自定义函数计算,用结构指针作为函数参数*/
#include<stdio.h>
struct date{
	int year;
	int month;
	int day;
}d,*p_d = &d;
//日期计算函数原型
int date_calculation(date *p_d);
//日期输入函数原型
void date_input(date *p_d);

void main(){
	//输入日期
	date_input(p_d);
	//计算日期
	printf("Your input:%d-%d-%d\n",p_d->year,p_d->month,p_d->day);
	printf("It's the %d day of the year!\n",date_calculation(p_d));
}

int date_calculation(date *p_d){
	int temp = 0;
	for(int i = 1; i < p_d->month;i++){
		switch(i){
			case 1: temp += 31;break;
			case 2: if((p_d->year % 4 == 0  && p_d->year % 100 != 0) || p_d->year % 400 == 0){temp += 29;break;}	
					else {temp += 28;break;}
			case 3:	temp += 31;break;
			case 4:	temp += 30;break;
			case 5:	temp += 31;break;
			case 6:	temp += 30;break;
			case 7:	temp += 31;break;
			case 8:	temp += 31;break;
			case 9:	temp += 30;break;
			case 10:temp += 31;break;
			case 11:temp += 30;break;
			case 12:temp += 31;break;
		}
	}
	return(temp + p_d->day);
}

void date_input(date *p_d){
	printf("Enter a date example:2018/8/15\n");
	scanf("%d/%d/%d",&p_d->year,&p_d->month,&p_d->day);
}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值