zoukankan      html  css  js  c++  java
  • 日期之差

    #include <iostream>
    #include <cstdio>
    using namespace  std;
    int days[][2]={
    	0,0,
    	31,31,
    	28,29,
    	31,31,
    	30,30,
    	31,31,
    	30,30,
    	31,31,
    	31,31,
    	30,30,
    	31,31,
    	30,30,
    	31,31
    
    };
    int isRun(int year){
    	if(year%4 ==0 && year%100!=0 || year%400 ==0)
    		return 1;
    	else
    		return 0;
    }
    
    
    
    struct Date{
    	int year;
    	int month;
    	int day;
    	Date(int year,int month,int day)
    	{
    		this->year=year;
    		this->month=month;
    		this->day=day;
    	}
    	void nextDay()
    	{
    		day++;
    		if(day>days[month][isRun(year)])
    		{
    			day=1;
    			month++;
    			if(month>12)
    			{
    				month=1;
    				year++;
    			}
    		}
    	}
    	bool equalDate(const Date &b){
    		return (year==b.year && month==b.month && day==b.day) ;
    	}
    	bool operator==(const Date &b)const{
    		return (year==b.year && month==b.month && day==b.day) ;
    	}
    	bool operator<(const Date &b) const{
    		if(year!=b.year)
    			return year<b.year;
    		if(month!=b.month)
    			return month<b.month;
    		return day<b.day;
    	}
    };
    void swap(Date &a,Date &b)
    {
    	Date tmp=a;
    	a=b;
    	b=tmp;
    }
    int main()
    {
    	int y1,m1,d1;
    	int y2,m2,d2;
    	
    	cout<<"请输入日期:"<<endl;
    	while (scanf("%4d%2d%2d",&y1,&m1,&d1)==3 && scanf("%4d%2d%2d",&y2,&m2,&d2)==3 )
    	{
    		Date a(y1,m1,d1);
    		Date b(y2,m2,d2);
    		if(b<a) swap(a,b);
    		int count=0;
    		while (!(a==b))
    		{
    			count++;
    			a.nextDay();
    		}
    		printf("%04d年%02d月%02d日与%04d年%02d月%02d日相差%d天\n",y1,m1,d1,y2,m2,d2,count);
    		cout<<"请输入日期:"<<endl;
    	}
    	return 0;
    }
    

  • 相关阅读:
    C++: std::string 与 Unicode 如何结合?
    C++ :enum及其使用
    C++标准库(二)
    #ifdef 中的逻辑与或操作
    这是我的第一篇博客
    C++标准库(一)
    ASP.NET基础05_页面跳转与传值
    ASP.NET基础06_琐碎
    ASP.NET基础04_简单数据绑定与App_Offline.htm文件
    ASP.NET基础01_验证与缓存
  • 原文地址:https://www.cnblogs.com/wuhayaoshenmeai/p/3361878.html
Copyright © 2011-2022 走看看