学者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);
}