欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、目的

用來(lái)比較兩個(gè)日期。日期格式: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++還可以實(shí)現(xiàn)計(jì)算日期相差多少天,下面是實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助

#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;
	// 輸入兩個(gè)日期
	cout << "輸入兩個(gè)日期,空格分隔";
	cin >> time1 >> time2;
	if (time1>time2){
		int temp = time1;
		time1 = time2;
		time2 = temp;

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

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

相關(guān)文章

  • C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計(jì)

    C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • C語(yǔ)言編程深入理解取整取余取模問(wèn)題示例分析

    C語(yǔ)言編程深入理解取整取余取模問(wèn)題示例分析

    這篇文章主要為大家介紹了C語(yǔ)言編程深入理解取整取余取模問(wèn)題的示例分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • C語(yǔ)言代碼中調(diào)用C++代碼的方法示例

    C語(yǔ)言代碼中調(diào)用C++代碼的方法示例

    這篇文章主要介紹了C語(yǔ)言代碼中調(diào)用C++代碼的方法示例,文中也介紹了C++代碼調(diào)用C代碼的方法,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02
  • Qt QFrame的具體使用

    Qt QFrame的具體使用

    本文主要介紹了Qt QFrame的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 探討編寫(xiě)int strlen(char *strDest);不允許定義變量的問(wèn)題

    探討編寫(xiě)int strlen(char *strDest);不允許定義變量的問(wèn)題

    本篇文章是對(duì)編寫(xiě)int strlen(char *strDest);不允許定義變量的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C++11/C++14中constexpr的使用案例詳解

    C++11/C++14中constexpr的使用案例詳解

    C++11規(guī)定,允許將變量聲明為constexpr類(lèi)型以便由編譯器來(lái)驗(yàn)證變量的值是否是一個(gè)常量表達(dá)式,這篇文章主要介紹了C++11/C++14中constexpr的使用,需要的朋友可以參考下
    2023-06-06
  • Qt消除警告的實(shí)現(xiàn)示例

    Qt消除警告的實(shí)現(xiàn)示例

    Qt5 和 Qt6 之間存在一些差異,導(dǎo)致在編譯時(shí)可能產(chǎn)生警告,為了消除這些警告,Qt 提供了一些宏定義來(lái)幫助你在代碼中處理這些差異,本文主要介紹了Qt消除警告的實(shí)現(xiàn)示例,感興趣的可以了解一下
    2023-09-09
  • C++淺析類(lèi)與對(duì)象基礎(chǔ)點(diǎn)

    C++淺析類(lèi)與對(duì)象基礎(chǔ)點(diǎn)

    類(lèi)和對(duì)象是兩種以計(jì)算機(jī)為載體的計(jì)算機(jī)語(yǔ)言的合稱(chēng)。對(duì)象是對(duì)客觀事物的抽象,類(lèi)是對(duì)對(duì)象的抽象。類(lèi)是一種抽象的數(shù)據(jù)類(lèi)型;變量就是可以變化的量,存儲(chǔ)在內(nèi)存中—個(gè)可以擁有在某個(gè)范圍內(nèi)的可變存儲(chǔ)區(qū)域
    2022-07-07
  • C++實(shí)現(xiàn)DES加密算法實(shí)例解析

    C++實(shí)現(xiàn)DES加密算法實(shí)例解析

    這篇文章主要介紹了C++實(shí)現(xiàn)DES加密算法實(shí)例解析,是一個(gè)很實(shí)用的功能,需要的朋友可以參考下
    2014-08-08
  • C++實(shí)現(xiàn)基數(shù)排序的方法詳解

    C++實(shí)現(xiàn)基數(shù)排序的方法詳解

    本篇文章是對(duì)使用C++實(shí)現(xiàn)基數(shù)排序的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論