首页 > 学院 > 开发设计 > 正文

4、编写一个程序,计算两个日期之间所经过的天数

2019-11-08 03:08:15
字体:
来源:转载
供稿:网友

这里写图片描述

#include<stdio.h>#include<math.h>int f(int year,int month){ if (month<3) { return year-1; } else { return year; }}int g(int month){ if (month<3) { return month+3; } else { return month+1; }}int N_Value(int year,int month,int day){ int N = 1461*f(year,month)/4+153*g(month)/5+day; return N;}void main(){ int year1,mongth1,day1,year2,mongth2,day2,N1,N2,dN; PRintf("Please input integer year,mongth,day,the first date:/n"); scanf("%d,%d,%d",&year1,&mongth1,&day1); printf("Please input integer year,mongth,day,the seconddate:/n"); scanf("%d,%d,%d",&year2,&mongth2,&day2); N1 = N_Value(year1,mongth1,day1); N2 = N_Value(year2,mongth2,day2); dN = abs(N1-N2); printf("There are %d days between the two dates./n",dN);}

结果: 这里写图片描述

但是后期检验时,发现教材中计算日期N值的公式有Bug

// 计算两个日期之间所经过的天数#include<stdio.h>#include<math.h>int f(int year,int month){ if (month<3) { return year-1; } else { return year; }}int g(int month){ if (month<3) { return month+3; } else { return month+1; }}int N_Value(int year,int month,int day){ int N = 1461*f(year,month)/4+153*g(month)/5+day; return N;}void main(){ int year1,mongth1,day1,year2,mongth2,day2,N1,N2,dN; printf("Please input integer year,mongth,day,the first date:/n"); scanf("%d,%d,%d",&year1,&mongth1,&day1); printf("Please input integer year,mongth,day,the seconddate:/n"); scanf("%d,%d,%d",&year2,&mongth2,&day2); N1 = N_Value(year1,mongth1,day1); N2 = N_Value(year2,mongth2,day2); dN = abs(N1-N2); printf("There are %d days between the two dates./n",dN); printf("N1 %d ./n",N1); printf("N2 %d ./n",N2);}

这里写图片描述


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表