2017年学者出国求学的总天数(含离开和返回的当天)C语言

学者A去国外求学,假定离开祖国的日子为公元A年B月C日,返回的日子为公元X年Y月Z日。请计算A出国求学的总天数(含离开和返回的当天)
1.求离开日期距离0年0月0日总天数
2.求回来日期距离0年0月0日总天数
3.两个总天数相减加一(因为要包含离开和回来的天数)

#include<stdio.h>
typedef struct Date {
	int year;
	int mon;
	int day;
	int sum;
}Date;
int isrun(int n) {
	if (n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
		return 1;
	}
	else {
		return 0;
	}
}
int getyear(int n) {
	int total=0;
	for (int i = 1; i < n; i++) {
		if (isrun(i)) {
			total += 366;
		}
		else {
			total += 365;
		}
	}
	return total;
}
int getmon(Date t) {
	int p[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	int r[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
	int total = 0;
	for (int i = 0; i < t.mon-1; i++) {
		if (isrun(t.year)) {
			total += r[i];
		}
		else {
			total += p[i];
		}
	}
	return total;
}
int getday(Date &t1,Date &t2) {
	
	int s1 ,s2;
	s1 = getyear(t1.year);
	s2 = getyear(t2.year);
	s1 += getmon(t1);
	s2 += getmon(t2);
	s1 += t1.day;
	s2 += t2.day;
	return s2-s1+1;
}
int main(){
	Date t1; Date t2;
	scanf_s("%d%d%d", &t1.year, &t1.mon, &t1.day);
	scanf_s("%d%d%d", &t2.year, &t2.mon, &t2.day);
	int c=getday(t1,t2);
	printf("%d", c);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值