- 输入年月日,输出该日期是当年的第几天
- 输入两个日期(年 月 日 年 月 日), 输出这两个日期之间差多少天
sum = delt(year1 year2) - dayofyear(date1) + dayofyear(date2)- 输入一个日期,输出该日期是星期几
a. delt % 7 b. (7 – delt % 7) % 7- 输入 一个日期 和一个整数 n,输出从该日期起经过 n 天以后的日期
- 输入一个年份,月份,输出该月份的日历
- 输出指定年的年历
//输入年月日,输出该日期是当年的第几天
int dateCount(int year, int mon, int day){
int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int total, i;
total = 0;
for(i=0; i<mon-1; i++){
total = total+a[i];
if(i == 1 && (year%4==0 && year%100!=0 || year%400==0)){
total++;
}
}
total = total+day;
//printf("%04d-%02d-%02d is No.%dday\n", year, mon, day, total);
return total;
}
//输入两个日期(年 月 日 年 月 日), 输出这两个日期之间差多少天
int dateDist(int year, int mon, int day, int year2, int mon2, int day2){
int total, i, t1, t2;
total = 0;
if(year>year2)
printf("error input\n");
t1 = dateCount(year, mon, day);
t2 = dateCount(year2, mon2, day2);
if(year == year2){
total = t2-t1;
goto lab;
}
t1 = 365 - t1;
if(year%4==0 && year%100!=0 || year%400==0) t1++;
for(i=year+1; i<year2; i++){
total = total+365;
if(i%4==0 && i%100!=0 || i%400==0){
total++;
}
}
total = total+t1+t2;
lab:
return total;
}
//输入一个日期,输出该日期是星期几
//delt % 7 (7 – delt % 7) % 7
int dateWeek(int year, int mon, int day){
int dist, flag;
char week[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
if(year>2020 || (year==2020 && (mon>1 || mon==1 && day>=5))){
dist = dateDist(2020, 1, 5, year, mon, day);
flag = dist%7;
}
else{
dist = dateDist(year, mon, day, 2020, 1, 5);
flag = (7-dist%7)%7;
}
//printf("%04d-%02d-%02d is %s\n", year, mon, day, week[flag]);
return flag;
}
//输入 一个日期 和一个整数 n,输出从该日期起经过 n 天以后的日期
void dateDay(){
int year, mon, day, year2, mon2, day2, n, i;
int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
while(scanf("%d %d %d", &year, &mon, &day) != EOF){
printf("please input days:");
scanf("%d", &n);
if(year%4==0 && year%100!=0 || year%400==0)
a[1] = 29;
else
a[1] = 28;
year2=year; mon2=mon;day2=day;
for(i=1; i<=n; i++){
if(day2<a[mon2-1])
day2++;
else if(day2 == a[mon2-1]){
if(mon2 == 12){
mon2 = 1;
day2 = 1;
year2++;
if(year2%4==0 && year2%100!=0 || year2%400==0)
a[1] = 29;
else
a[1] = 28;
}
else{
mon2++;
day2 = 1;
}
}
}
printf("The date of %04d-%02d-%02d after %d days is:%04d-%02d-%02d\n", year, mon, day, n, year2, mon2, day2);
}
}
//输入一个年份,月份,输出该月份的日历
void calMon(){
int year, mon, week, num, i, j, cnt;
int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int cal[6][7];
while(scanf("%d %d", &year, &mon) != EOF){
printf("Sun Mon Tue Wed Thu Fri Sat\n");
week = dateWeek(year, mon, 1);
if(mon == 2){
if(year%4==0 && year%100!=0 || year%400==0)
a[1] = 29;
else
a[1] = 28;
}
memset(cal, 0, 42*sizeof(int));
for(i=0; i<week; i++){
printf(" ");
}
for(i=week, cnt=1; i<7; i++, cnt++){
cal[0][i] = cnt;
printf(" %d ", cal[0][i]);
}
printf("\n");
for(i=1; i<6; i++){
for(j=0; j<7 && cnt<=a[mon-1]; j++, cnt++){
cal[i][j] = cnt;
if(cnt<10){
printf(" %d ", cal[i][j]);
}
else{
printf(" %d ", cal[i][j]);
}
}
printf("\n");
}
}
}
//输出指定年的年历
void calYear(){
int year, mon, mon1, week, week1, i, j, j1, cnt, cnt1, flag, flag1;
int a[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int cal[6][7], cal1[6][7];
while(scanf("%d", &year) != EOF){
flag = 0; flag1 = 0;
if(year%4==0 && year%100!=0 || year%400==0)
a[1] = 29;
else
a[1] = 28;
printf("|------------------The Calculate of Year %d------------------|\n",year);
for(mon=1; mon<=11; mon=mon+2){
mon1 = mon+1;
printf("|");
if(mon<10) printf(" %d Sun Mon Tue Wed Thu Fri Sat ", mon);
else printf("%d Sun Mon Tue Wed Thu Fri Sat ", mon);
if(mon1<10) printf(" %d Sun Mon Tue Wed Thu Fri Sat |\n", mon1);
else printf("%d Sun Mon Tue Wed Thu Fri Sat |\n", mon1);
week = dateWeek(year, mon, 1);
week1 = dateWeek(year, mon1, 1);
memset(cal, 0, 42*sizeof(int));
memset(cal1, 0, 42*sizeof(int));
//No.1
printf("| ");
for(i=0; i<week; i++){
printf(" ");
}
for(i=week, cnt=1; i<7; i++, cnt++){
cal[0][i] = cnt;
printf(" %d ", cal[0][i]);
}
printf(" ");
for(i=0; i<week1; i++){
printf(" ");
}
for(i=week1, cnt1=1; i<7; i++, cnt1++){
cal1[0][i] = cnt1;
printf(" %d ", cal1[0][i]);
}
printf("|\n");
//No.2-6
for(i=1; i<6; i++){
if(i==5 && flag==1 && flag1==1){
break;
}
printf("| ", mon);
for(j=0; j<7; j++, cnt++){
if(cnt>a[mon-1]){
printf(" ");
continue;
}
cal[i][j] = cnt;
if(cnt<10){
printf(" %d ", cal[i][j]);
}
else{
printf(" %d ", cal[i][j]);
}
if(cnt==a[mon-1] && i<=4)
flag = 1;
}
printf(" ");
for(j1=0; j1<7; j1++, cnt1++){
if(cnt1>a[mon1-1]){
printf(" ");
continue;
}
cal1[i][j1] = cnt1;
if(cnt1<10){
printf(" %d ", cal1[i][j1]);
}
else{
printf(" %d ", cal1[i][j1]);
}
if(cnt1==a[mon1-1] && i<=4){
flag1 = 1;
}
}
printf("|\n");
}
}
printf("|--------------------------------------------------------------|\n");
}
}