日期差值--补充while(scanf())与while(scanf()!=EOF)的理解

在这里插入图片描述
代码:

#include<bits/stdc++.h>
using namespace std;
int f[2][13]={
    
    {
    
    0,31,28,31,30,31,30,31,31,30,31,30,31},
            {
    
    0,31,29,31,30,31,30,31,31,30,31,30,31}};
int leapyear(int n)
{
    
    
	if(n%400==0||(n%4==0&&n%100!=0)) return 1;
	else return 0;
}
struct node{
    
    
	int year,month,day;
}pnode;
int all_day(node n)//结构体作为输入参数,算差值可以找一个参照物,都算出与此参照物的差值,选取参照物为1年1月1日
{
    
    
	int sum=0;
	int i;
	for(i=1;i<n.year;++i)
	{
    
    
		if(leapyear(i))
		{
    
    
			sum+=366;
		}
		else sum+=365;
	}
	for(i=1;i<n.month;++i)
	{
    
    
		sum+=f[leapyear(n.year)][i];
	}
	sum+=n.day;
	return sum;
}
int main()
{
    
     
    node a,b;
    while(scanf("%4d%2d%2d",&a.year,&a.month,&a.day)!=EOF)//不能用while(scanf())
    {
    
    
    	scanf("%4d%2d%2d",&b.year,&b.month,&b.day);
    	printf("%d\n",abs(all_day(a)-all_day(b))+1);
	}
	
	return 0;
 } 

scanf("%d,%d", &a, &b):
  如果a和b都被成功读入,那么scanf的返回值就是2
  如果只有a被成功读入,返回值为1
  如果a和b都未被成功读入,返回值为0
  如果遇到错误或遇到end of file,返回值为EOF

猜你喜欢

转载自blog.csdn.net/weixin_43901182/article/details/112642099