• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            子彈 の VISIONS

            NEVER back down ~~

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks
            #
            // created by ztwaker on 2006-8-24
            // Used for: count days
            // No guarantees offered. Constructive comments to waker615@gmail.com
            
            // 需求:計(jì)算兩個(gè)日期之間相距天數(shù)。
            // 應(yīng)用:倒計(jì)時(shí)等
            
            #include <iostream>
            #include <algorithm>
            namespace date {
            
            //#define min(a, b) (((a) > (b)) ? (b) : (a))
            //#define max(a, b) (((a) > (b)) ? (a) : (b))
            	
            	class Bad_date {}; // exception throw
            	class Bad_year  : public Bad_date {};
            	class Bad_month : public Bad_date {};
            	class Bad_day   : public Bad_date {};
            	
                ??? static const int this_year = 2006;
            
            	inline bool leapyear(int year)
            	{
            		//if (year > this_year)
            		// 	throw Bad_year();
            		
            		return ((year % 100==0) ? (year % 400==0) : (year % 4==0));
            	}
            	
            	inline int yday(int year)
            	{
            		//if (year > this_year)
            		//	throw Bad_year();
            		
            		return (365 + leapyear(year));
            	}
            	
            	inline int mday(int month, int year)
            	{
            		switch(month) {
            		case 1: case 3: case 5: 
            		case 7: case 8: case 10:
            		case 12:
            			return 31;
            			
            		case 4: case 6: case 9: 
            		case 11: 
            			return 30;
            			
            		case 2: 
            			return 28 + leapyear(year);
            			
            		default:
            			throw Bad_month();
            		}
            		
            		throw Bad_month();
            	}
            	
            	class Date{
            		int y, m, d;
            		
            	public:
            		Date(int year, int month, int day)
            		{
            			// check year/month/day available
            			if (0 == year)
            				throw Bad_year();
            
            			if (1 > month || 12 < month)
            				throw Bad_month();
            
            			if (1 > day || mday(month, year) < day)
            				throw Bad_day();
            			
            			y = year; m = month; d = day;
            		}
            		Date(const Date& rhs)
            		{
            			this->y = rhs.year();
            			this->m = rhs.month();
            			this->d = rhs.day();
            		}		
            		Date& operator=(const Date& rhs)
            		{
            			if (rhs == (*this))
            				return (*this);
            			
            			this->y = rhs.year();
            			this->m = rhs.month();
            			this->d = rhs.day();
            			
            			return (*this);
            		}
            		int year () const { return y; }
            		int month() const { return m; }
            		int day  () const { return d; }
            		
            		int passday() const
            		{
            			int dpass = 0;
            			for (int i = 1; i != m; ++i)
            			{
            				dpass += mday(i, y);
            			}
            			
            			return (dpass + d);
            		}
            
            		friend std::ostream& operator<<(std::ostream& os, const Date& rhs);
            		
            		friend bool operator==(const Date& lhs, const Date& rhs);
            		friend bool operator!=(const Date& lhs, const Date& rhs);
            		friend bool operator< (const Date& lhs, const Date& rhs);
            		friend bool operator> (const Date& lhs, const Date& rhs);
            		friend bool operator<=(const Date& lhs, const Date& rhs);
            		friend bool operator>=(const Date& lhs, const Date& rhs);
            		
            		friend int  distant(const Date& lhs, const Date& rhs);
            	};
            
            	inline std::ostream& operator<<(std::ostream& os, const Date& rhs)
            	{
            		return (os << rhs.y << "-" << rhs.m << "-" << rhs.d);
            	}
            	inline bool operator==(const Date& lhs, const Date& rhs)
            	{
            		return (lhs.y == rhs.y && lhs.m == rhs.m && lhs.d == rhs.d);
            	}
            	inline bool operator!=(const Date& lhs, const Date& rhs)
            	{
            		return (!(lhs==rhs));
            	}
            	inline bool operator<(const Date& lhs, const Date& rhs)
            	{
            		// compare year
            		if (lhs.y < rhs.y)
            		{
            			return true;
            		}
            		else if (lhs.y == rhs.y)
            		{
            			// compare month
            			if (lhs.m < rhs.m)
            			{
            				return true;
            			}
            			else if (lhs.m == rhs.m)
            			{
            				// compare day
            				if (lhs.d < rhs.d)
            					return true;
            				else
            					return false;
            			}
            			return false;
            		}
            		return false;
            	}
            	inline bool operator>(const Date& lhs, const Date& rhs)
            	{
            		// compare year
            		if (lhs.y > rhs.y)
            		{
            			return true;
            		}
            		else if (lhs.y == rhs.y)
            		{
            			// compare month
            			if (lhs.m > rhs.m)
            			{
            				return true;
            			}
            			else if (lhs.m == rhs.m)
            			{
            				// compare day
            				if (lhs.d > rhs.d)
            					return true;
            				else
            					return false;
            			}
            			return false;
            		}
            		return false;
            	}
            	inline bool operator<=(const Date& lhs, const Date& rhs)
            	{
            		return (!(lhs>rhs));
            	}
            	inline bool operator>=(const Date& lhs, const Date& rhs)
            	{
            		return (!(lhs<rhs));
            	}
            	
            	int distant(const Date& lhs, const Date& rhs)
            	{
            		int distance = 0;
            		const int lowyear  = std::min(lhs.y, rhs.y);
            		const int highyear = std::max(lhs.y, rhs.y);
            		for (int yn = lowyear; yn != highyear; ++yn)
            		{
            			distance += yday(yn);
            		}
            		
            		return (
            			distance - std::min(lhs, rhs).passday() 
            			+ std::max(lhs, rhs).passday()
            			);
            	}
            } // end of namespace date
            
            void test()
            {
            	try {
            		date::Date today(2006, 1, 29);    // 丙戌年春節(jié)
            		date::Date deadline(2007, 2, 18); // 丁亥年春節(jié)
            		std::cout << "From "<< today << " to " << deadline << " is ";
            		std::cout << date::distant(today, deadline) << " days left.\n";
            	}
            	catch (date::Bad_year) {
            		std::cout << "Bad year" << std::endl;
            	}
            	catch (date::Bad_month) {
            		std::cout << "Bad month" << std::endl;
            	}
            	catch (date::Bad_day) {
            		std::cout << "Bad day" << std::endl;
            	}
            	catch (date::Bad_date) {
            		std::cout << "Bad date" << std::endl;
            	}
            }
            
            int main()
            {
                test();
                return 0;
            }
            posted on 2006-08-25 11:08 子彈のVISIONS 閱讀(1723) 評(píng)論(6)  編輯 收藏 引用

            Feedback

            # re: [OPPD] 計(jì)算兩個(gè)日期之間相距天數(shù) 2006-08-25 11:10 子彈
            如果限于此需求,對(duì)于operator,僅重載">"也夠用了;甚至
            copy constructor [Date(const Date& rhs)] 和operator = 也可以不提供。  回復(fù)  更多評(píng)論
              

            # re: [OPPD] 計(jì)算兩個(gè)日期之間相距天數(shù) 2006-08-25 19:08 chenger
            min和max兩個(gè)宏用標(biāo)準(zhǔn)庫提供的min和max算法代替更好些  回復(fù)  更多評(píng)論
              

            # re: [OPPD] 計(jì)算兩個(gè)日期之間相距天數(shù) 2006-08-28 09:06 子彈
            @chenger
            贊成。

            之前使用宏的原因是:VC6對(duì)generaic algorithms min and max支持不好。為了方便編譯而使用。

            // 已經(jīng)改用STL的算法min和max  回復(fù)  更多評(píng)論
              

            # re: [OPPD] 計(jì)算兩個(gè)日期之間相距天數(shù) 2006-08-28 15:43 chenger
            聽說VC6對(duì)C++標(biāo)準(zhǔn)的支持很不好,特別是template。居然連min max這樣的簡單模板函數(shù)都搞不定?
            還是用g++或者vs2005得編譯器好  回復(fù)  更多評(píng)論
              

            # re: [OPPD] 計(jì)算兩個(gè)日期之間相距天數(shù) 2006-08-29 08:58 子彈
            @chenger

            裝了SP6之后對(duì)template支持稍好  回復(fù)  更多評(píng)論
              

            # re: [OPPD] 計(jì)算兩個(gè)日期之間相距天數(shù) 2006-08-30 16:25
            scut師兄?支持一下!~  回復(fù)  更多評(píng)論
              


            只有注冊用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产毛片欧美毛片久久久| 婷婷五月深深久久精品| 久久精品国产亚洲7777| 久久精品亚洲男人的天堂| 亚洲AⅤ优女AV综合久久久| 无码人妻久久久一区二区三区| 国产精品99久久精品| 久久久久亚洲AV无码专区桃色| 成人综合久久精品色婷婷| av无码久久久久不卡免费网站 | 色婷婷综合久久久久中文一区二区| 久久国产精品成人影院| 精品久久人人爽天天玩人人妻| 综合人妻久久一区二区精品| 国内精品久久久久久麻豆 | 亚洲国产精品一区二区久久hs| 丁香五月网久久综合| 久久久久久无码国产精品中文字幕 | 亚洲国产美女精品久久久久∴| 久久国产香蕉一区精品| 精品久久一区二区| 性色欲网站人妻丰满中文久久不卡| 久久青青草原精品国产软件| 国产精品免费看久久久| 少妇久久久久久被弄高潮| 久久久久久久91精品免费观看| 久久久亚洲精品蜜桃臀| 日本精品久久久中文字幕| 99久久免费国产特黄| 久久亚洲AV成人无码国产| 麻豆亚洲AV永久无码精品久久| 久久精品国产清自在天天线| 亚洲日本va午夜中文字幕久久| 久久精品国产亚洲一区二区三区 | 亚洲国产成人精品女人久久久| 国内精品久久久久久中文字幕| 91精品免费久久久久久久久| 欧美激情精品久久久久| 国产精品VIDEOSSEX久久发布| 大香网伊人久久综合网2020| 国产精品美女久久久免费|