最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實現(xiàn)比較日期大小的示例代碼

 更新時間:2023年04月04日 15:29:43   作者:歐特克_Glodon  
這篇文章主要為大家詳細(xì)介紹了如何使用C++實現(xiàn)比較日期大小的功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的可以了解一下

一、目的

用來比較兩個日期。日期格式:2023-03-31 09:16:56。

二、代碼

//std::wstring strA = L"2023-03-31 09:16:56";
//std::wstring strB = L"2023-03-31 09:21:34";
bool LessThanEx(std::wstring strA, std::wstring strB)
{
	std::wstring strLeftA, strRightA;
	std::wstring strLeftB, strRightB;
	{
		std::wstring strLeft, strRight;
		std::size_t nIndex = strA.find(L" ");
		if (nIndex!=std::string::npos)
		{
			strLeft = strA.substr(0,nIndex);
			strRight = strA.substr(nIndex+1);

			std::wstring wsDivide = L"-";
			strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
			strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");

			wsDivide = L":";
			strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
			strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
		}

		strLeftA = strLeft;
		strRightA = strRight;
	}

	{
		std::wstring strLeft, strRight;
		std::size_t nIndex = strB.find(L" ");
		if (nIndex!=std::string::npos)
		{
			strLeft = strB.substr(0,nIndex);
			strRight = strB.substr(nIndex+1);

			std::wstring wsDivide = L"-";
			strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");
			strLeft.replace(strLeft.find(wsDivide),wsDivide.length(),L"");

			wsDivide = L":";
			strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
			strRight.replace(strRight.find(wsDivide),wsDivide.length(),L"");
		}

		strLeftB = strLeft;
		strRightB = strRight;
	}

	__int64 nLeftA = std::stoi(strLeftA);
	__int64 nLeftB = std::stoi(strLeftB);

	__int64 nRightA = std::stoi(strRightA);
	__int64 nRightB = std::stoi(strRightB);
	if(nLeftA < nLeftB)
	{
		return true;
	}
	else if(nLeftA > nLeftB)
	{
		return false;
	}
	else
	{
		if(nRightA >= nRightB)
		{
			return false;
		}
		
		return true;
	}

	return true;
}

//CString strA = _T("2023-03-31 09:16:56");
//CString strB = _T("2023-03-31 09:21:34");
bool LessThan(CString strA, CString strB)
{
	CString strLeftA, strRightA;
	CString strLeftB, strRightB;
	{
		CString strLeft, strRight;
		int nIndex = strA.Find(_T(" "));
		if (nIndex > -1)
		{
			strLeft = strA.Left(nIndex);
			strRight = strA.Mid(nIndex+1,strA.GetLength() - nIndex-1);

			strLeft.Replace(_T("-"),_T(""));
			strRight.Replace(_T(":"),_T(""));
		}

		strLeftA = strLeft;
		strRightA = strRight;
	}

	{
		CString strLeft, strRight;
		int nIndex = strB.Find(_T(" "));
		if (nIndex > -1)
		{
			strLeft = strB.Left(nIndex);
			strRight = strB.Mid(nIndex+1,strB.GetLength() - nIndex-1);

			strLeft.Replace(_T("-"),_T(""));
			strRight.Replace(_T(":"),_T(""));
		}

		strLeftB = strLeft;
		strRightB = strRight;
	}

	__int64 nLeftA = _tstoi64(strLeftA);
	__int64 nLeftB = _tstoi64(strLeftB);

	__int64 nRightA = _tstoi64(strRightA);
	__int64 nRightB = _tstoi64(strRightB);
	if(nLeftA < nLeftB)
	{
		return true;
	}
	else if(nLeftA > nLeftB)
	{
		return false;
	}
	else
	{
		if(nRightA >= nRightB)
		{
			return false;
		}

		return true;
	}

	return true;
}

三、補(bǔ)充

除了比較大小,C++還可以實現(xiàn)計算日期相差多少天,下面是實現(xiàn)代碼,希望對大家有所幫助

#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
bool isLeap(int year) {
	return (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
}
int main() {
	//定義好平年和閏年每月的天數(shù)
	int monthDays[13][2] = {
		{0,0},{31,31},{28,29},{30,30},{31,31},{30,30},
		{31,31},{30,30},{31,31},{30,30},{31,31},{30,30},
		{31,31}
	};
	int time1, year1, month1, days1;
	int time2, year2, month2, days2;
	int numbers =1;
	// 輸入兩個日期
	cout << "輸入兩個日期,空格分隔";
	cin >> time1 >> time2;
	if (time1>time2){
		int temp = time1;
		time1 = time2;
		time2 = temp;

	}
	//拆解日期,分為年,月,號
	year1 = time1 / 10000; month1 = time1 / 100 % 100; days1 = time1 % 100;
	year2 = time2 / 10000; month2 = time2 / 100 % 100; days2 = time2 % 100;
	//第一個日期 累加到 第二個日期
	while (year1 < year2 || month1 < month2 || days1 < days2) {
		days1++;// 在第一個日期基礎(chǔ)上  加一天
		//加一天后,相應(yīng)的月,年可能也要做一定的變化
		if (days1 == monthDays[month1][isLeap(year1)]+1) {//當(dāng)前號超過當(dāng)前月最高天數(shù):月份加1,號變成下月的1號
			month1++;
			days1 = 1;
		}
		if (month1 == 13) {//月份超過12個月 :年份加1,月份變成下年的1月
			year1++;
			month1 = 1;
		}
		numbers++;
	}
	cout << numbers << endl;
	return 0;
}

到此這篇關(guān)于C++實現(xiàn)比較日期大小的示例代碼的文章就介紹到這了,更多相關(guān)C++比較日期大小內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++實現(xiàn)賓館房間管理系統(tǒng)

    C++實現(xiàn)賓館房間管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)賓館房間管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • c++超細(xì)致講解引用

    c++超細(xì)致講解引用

    在我們?nèi)粘5纳钪忻總€人都或多或少存在一個"外號",例如《西游記》中孫悟空就有諸多外號:美猴王,孫行者,齊天大圣等等。那么在C++中,也可以給一個已經(jīng)存在的變量取別名,這就是引用。那么接下來深入來探討一下引用
    2022-05-05
  • C/C++ Qt ToolBar菜單組件的具體使用

    C/C++ Qt ToolBar菜單組件的具體使用

    ToolBar工具欄在所有窗體應(yīng)用程序中都廣泛被使用,使用ToolBar可以很好的規(guī)范菜單功能分類,本文就詳細(xì)的介紹一下ToolBar組件的應(yīng)用,感興趣的可以了解一下
    2021-11-11
  • c++ class中成員與分配內(nèi)存的問題詳解

    c++ class中成員與分配內(nèi)存的問題詳解

    很多人都知道C++類是由結(jié)構(gòu)體發(fā)展得來的,所以他們的成員變量(C語言的結(jié)構(gòu)體只有成員變量)的內(nèi)存分配機(jī)制是一樣的,下面這篇文章主要給大家介紹了關(guān)于c++ class中成員與分配內(nèi)存問題的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • C語言中使用快速排序算法對元素排序的實例詳解

    C語言中使用快速排序算法對元素排序的實例詳解

    這篇文章主要介紹了C語言中使用快速排序算法對元素排序的實例詳解,文中細(xì)分了幾個情況來舉例,在注釋里有說明,需要的朋友可以參考下
    2016-04-04
  • C語言求解定積分的方法

    C語言求解定積分的方法

    這篇文章主要為大家詳細(xì)介紹了C語言求解定積分的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++中的std::initializer_list使用解讀

    C++中的std::initializer_list使用解讀

    這篇文章主要介紹了C++中的std::initializer_list使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C++中BitBlt的使用方法詳解

    C++中BitBlt的使用方法詳解

    這篇文章主要介紹了C++中BitBlt的使用方法詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • C++11中的可變參數(shù)模板/lambda表達(dá)式

    C++11中的可變參數(shù)模板/lambda表達(dá)式

    C++11的新特性可變參數(shù)模板能夠讓我們創(chuàng)建可以接受可變參數(shù)的函數(shù)模板和類模板,相比C++98和C++03,類模板和函數(shù)模板中只能含固定數(shù)量的模板參數(shù),可變參數(shù)模板無疑是一個巨大的改進(jìn),這篇文章主要介紹了C++11中的可變參數(shù)模板/lambda表達(dá)式,需要的朋友可以參考下
    2023-03-03
  • 詳談全排列next_permutation() 函數(shù)的用法(推薦)

    詳談全排列next_permutation() 函數(shù)的用法(推薦)

    下面小編就為大家?guī)硪黄斦勅帕衝ext_permutation() 函數(shù)的用法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03

最新評論

盐津县| 普安县| 安庆市| 霍州市| 杭锦后旗| 石渠县| 贞丰县| 益阳市| 汉沽区| 成都市| 南平市| 阜康市| 马尔康县| 济南市| 元朗区| 江安县| 彰武县| 宜州市| 龙南县| 泸定县| 宣化县| 平顺县| 江陵县| 资中县| 大竹县| 电白县| 黄浦区| 庆安县| 曲阳县| 竹北市| 富蕴县| 红原县| 越西县| 泉州市| 抚宁县| 尼勒克县| 卓尼县| 渭南市| 南陵县| 澳门| 凭祥市|