1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      C++實現(xiàn)大數(shù)運算-創(chuàng)新互聯(lián)

      項目背景:

      專業(yè)領(lǐng)域包括成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、商城網(wǎng)站建設(shè)、微信營銷、系統(tǒng)平臺開發(fā), 與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,創(chuàng)新互聯(lián)建站的整合解決方案結(jié)合了幫做網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,為客戶提供全網(wǎng)互聯(lián)網(wǎng)整合方案。

        大數(shù)運算,顧名思義,就是很大的數(shù)值的數(shù)進(jìn)行一系列的運算。

        我們知道,在數(shù)學(xué)中,數(shù)值的大小是沒有上限的,但是在計算機(jī)中,由于字長的限制,計算機(jī)所能表示的范圍是有限的,當(dāng)我們對比較小的數(shù)進(jìn)行運算時,如:1234+5678,這樣的數(shù)值并沒有超出計算機(jī)的表示范圍,所以可以運算。但是當(dāng)我們在實際的應(yīng)用中進(jìn)行大量的數(shù)據(jù)處理時,會發(fā)現(xiàn)參與運算的數(shù)往往超過計算機(jī)的基本數(shù)據(jù)類型的表示范圍,比如說,在天文學(xué)上,如果一個星球距離我們?yōu)?00萬光年,那么我們將其化簡為公里,或者是米的時候,我們會發(fā)現(xiàn)這是一個很大的數(shù)。這樣計算機(jī)將無法對其進(jìn)行直接計算。

        可能我們認(rèn)為實際應(yīng)用中的大數(shù)也不過就是幾百位而已,實際上,在某些領(lǐng)域里,甚至可能出現(xiàn)幾百萬位的數(shù)據(jù)進(jìn)行運算,這是我們很難想象的。如果沒有計算機(jī),那么計算效率可想而知。

        由于編程語言提供的基本數(shù)值數(shù)據(jù)類型表示的數(shù)值范圍有限,不能滿足較大規(guī)模的高精度數(shù)值計算,因此需要利用其他方法實現(xiàn)高精度數(shù)值的計算,于是產(chǎn)生了大數(shù)運算。本項目實現(xiàn)了大數(shù)運算主要的加、減、乘除四種方法。

      BigData.h

      #ifndef BIG_DATA_H
      #define BIG_DATA_H
      
      #include 
      
      #define MAX_INT64 (INT64)0x7FFFFFFFFFFFFFFF
      #define MIN_INT64 (INT64)0x8000000000000000 //如果不強(qiáng)轉(zhuǎn),系統(tǒng)會定義為unsigned INT64類型
      
      #define UN_INIT 0xCCCCCCCCCCCCCCCC
      typedef long long INT64;
      
      class BigData
      {
      public:
      	BigData(INT64 value);
      	BigData(const char* pData);
      
      public:
      	BigData operator+(const BigData& bigdata);
      	BigData operator-(const BigData& bigdata);
      	BigData operator*(const BigData& bigdata);
      	BigData operator/(const BigData& bigdata);
      
      protected:
      	std::string Add(std::string left, std::string right);
      	std::string Sub(std::string left, std::string right);
      	std::string Mul(std::string left, std::string right);
      	std::string Div(std::string left, std::string right);	
      
      protected:
      	bool IsINT64Overflow() const;
      	friend std::ostream& operator<<(std::ostream& _cout, const BigData& bigdata);
      	void INT64ToString();
      	bool IsLeftStrBig(const char* pLeft, int LSize, const char* pRight, int RSize);
      	char SubLoop(char* pLeft, int LSize, char* pRight, int RSize);
      
      protected:
      	INT64 _value;
      	std::string _strData;
      };
      
      #endif

      BigData.cpp

      #include "BigData.h"
      #include 
      
      BigData::BigData(INT64 value)
      	:_value(value)
      {
      	INT64ToString(); //將_value中的值保存在_strData中
      }
      
      BigData::BigData(const char* pData)
      {
      	//要處理的輸入:"12345678"   "234567qwe"    "+"    "    "     "0000123456"
      
      	if (NULL == pData)
      	{
      		assert(false);
      		return;
      	}
      
      	//處理符號位
      	char* pStr = (char*)pData; 
      	char cSymbol = pData[0];
      	if ('+' == cSymbol || '-' == cSymbol)
      		pStr++;
      	else if (cSymbol >= '0' && cSymbol <= '9')
      		cSymbol = '+';
      	else
      		return;
      
      	//"0000123456"
      	int iDataLen = strlen(pData);
      	if (iDataLen > 1)
      	{
      		while ('0' == *pStr)
      			pStr++;
      	}
      
      	_strData.resize(strlen(pData)+1); //為_strData分配空間
      	_strData[0] = cSymbol; //第0位保存符號
      
      	//"123456qwe"
      	_value = 0;
      	int iCount = 1;
      	while (*pStr >= '0' && *pStr <= '9')
      	{
      		_value = _value*10 + (*pStr - '0');
      		_strData[iCount++] = *pStr;
      		pStr++;
      	}
      
      	_strData.resize(iCount);
      
      	if (cSymbol == '-')
      		_value = 0 - _value;
      }
      
      bool BigData::IsINT64Overflow() const
      {
      	std::string temp("+9223372036854775807");
      	
      	if (_strData[0] == '-')
      		temp = "-9223372036854775808";
      
      	if (_strData.size() < temp.size())
      		return false;
      	else if (_strData.size() == temp.size() && _strData <= temp)
      		return false;
      
      	return true;
      }
      
      void BigData::INT64ToString()
      {
      	//處理符號位
      	char cSymbol = '+';
      	if (_value < 0)
      		cSymbol = '-';
      
      	INT64 temp = _value;
      	while (temp)
      	{
      		if (cSymbol == '+')
      			_strData.append(1, temp%10 + '0');
      		else
      			_strData.append(1, -(temp%10) + '0');
      
      		temp /= 10;
      	}
      	_strData.append(1, cSymbol);
      	
      	std::reverse(_strData.begin(), _strData.end());
      }
      
      bool BigData::IsLeftStrBig(const char* pLeft, int LSize, const char* pRight, int RSize)
      {
      	assert(pLeft != NULL && pRight != NULL);
      
      	if ((LSize > RSize) || 
      		(LSize == RSize && strcmp(pLeft, pRight) >= 0))
      		return true;
      	else
      		return false;
      }
      
      char BigData::SubLoop(char* pLeft, int LSize, char* pRight, int RSize)
      {
      	assert(pLeft != NULL && pRight != NULL);
      
      	char cRet = '0';
      
      	while (true)
      	{
      		if (!IsLeftStrBig(pLeft, LSize, pRight, RSize))
      			break;
      		
      		int iLIdx = LSize - 1;
      		int iRIdx = RSize - 1;
      		while (iLIdx >= 0 && iRIdx >= 0)
      		{
      			char ret = pLeft[iLIdx] - '0';
      			ret -= pRight[iRIdx] - '0';
      			
      			if (ret < 0)
      			{
      				pLeft[iLIdx - 1] -= 1;
      				ret += 10;
      			}
      			
      			pLeft[iLIdx] = ret + '0';
      			iLIdx--;
      			iRIdx--;
      		}
      
      		while (*pLeft == '0' && LSize > 0)
      		{
      			pLeft++;
      			LSize--;
      		}
      
      		cRet++;
      	}
      
      	return cRet;
      }
      
      std::ostream& operator<<(std::ostream& _cout, const BigData& bigdata)
      {
      	if (!bigdata.IsINT64Overflow()) //_value沒有溢出
      	{
      		_cout<= 0 && bigdata._value <= MAX_INT64 - _value) || 
      				(_value < 0 && bigdata._value >= MIN_INT64 - _value)) //相加后的和沒有溢出
      				return BigData(_value + bigdata._value);
      		}
      	}
      
      	//兩個數(shù)至少有一個溢出
      	//相加后的和溢出
      	if (_strData[0] == bigdata._strData[0]) //同號相加,調(diào)用加法
      		return BigData(Add(_strData, bigdata._strData).c_str());
      	else //異號相加,調(diào)用減法
      		return BigData(Sub(_strData, bigdata._strData).c_str());
      }
      
      BigData BigData::operator-(const BigData& bigdata)
      {
      	if (!IsINT64Overflow() && !bigdata.IsINT64Overflow()) //兩個數(shù)都沒有溢出
      	{
      		if (_strData[0] == bigdata._strData[0])//同號
      		{
      			return BigData(_value - bigdata._value);
      		}
      		else //異號
      		{
      			if ((_value >= 0 && MAX_INT64 + bigdata._value >= _value) || 
      				(_value < 0 && MIN_INT64 + bigdata._value <= _value)) //結(jié)果沒有溢出
      				return BigData(_value - bigdata._value);
      		}
      	}
      
      	//至少有一個數(shù)溢出或結(jié)果溢出
      	if (_strData[0] != bigdata._strData[0]) //異號相減,調(diào)用加法
      		return BigData(Add(_strData, bigdata._strData).c_str());
      	else //同號相減,調(diào)用減法
      		return BigData(Sub(_strData, bigdata._strData).c_str());
      }
      
      BigData BigData::operator*(const BigData& bigdata)
      {
      	if (!IsINT64Overflow() && !bigdata.IsINT64Overflow()) //都沒有溢出
      	{
      		if (_value == 0 || bigdata._value == 0)
      		{
      			return BigData((INT64)0);
      		}
      
      		if (_strData[0] == bigdata._strData[0]) //同號,積為正
      		{
      			if ((_value > 0 && MAX_INT64/_value >= bigdata._value) || 
      				(_value < 0 && MAX_INT64/_value <= bigdata._value)) //積沒有溢出
      			{
      				return BigData(_value * bigdata._value);
      			}
      		}
      		else //異號,積為負(fù)
      		{
      			if ((_value > 0 && MIN_INT64/_value <= bigdata._value) || 
      				(_value < 0 && MIN_INT64/_value >= bigdata._value)) //積沒有溢出
      			{
      				return BigData(_value * bigdata._value);
      			}
      		}
      	}
      
      	return BigData(Mul(_strData, bigdata._strData).c_str());
      }
      
      BigData BigData::operator/(const BigData& bigdata)
      {
      	//除數(shù)不能為0
      	if ('0' == bigdata._strData[1])
      		assert(false);
      
      	//	都沒溢出
      
      
      	if (!IsINT64Overflow() && !bigdata.IsINT64Overflow())
      		return BigData(_value / bigdata._value);
      
      	//	至少有一個溢出
      
      
      	//除數(shù) == ±1
      	if (bigdata._strData == "+1" && bigdata._strData == "-1") 
      	{
      		std::string strRet = _strData;
      
      		if (_strData[0] != bigdata._strData[0])
      			strRet[0] = '-';
      
      		return BigData(strRet.c_str());
      	}
      
      	//左 < 右
      	if ( (_strData.size() < bigdata._strData.size()) || 
      		 (_strData.size() == bigdata._strData.size() && strcmp(_strData.c_str()+1, bigdata._strData.c_str()+1) < 0) ) 
      		return BigData(INT64(0));
      
      	//左 == 右
      	if (strcmp(_strData.c_str()+1, bigdata._strData.c_str()+1) == 0) //左==右
      	{
      		std::string strRet = "+1";
      		if (_strData[0] != bigdata._strData[0])
      			strRet[0] = '-';
      		else
      			return BigData(strRet.c_str());
      	}
      
      	//左 > 右
      	return BigData(Div(_strData, bigdata._strData).c_str());
      }
      
      std::string BigData::Add(std::string left, std::string right)
      {
      	//讓left保存位數(shù)大的數(shù)
      	int iLeftSize = left.size();
      	int iRightSize = right.size();
      	if (iLeftSize < iRightSize)
      	{
      		std::swap(left, right);
      		std::swap(iLeftSize, iRightSize);
      	}
      
      	//相加
      	std::string strRet;
      	strRet.resize(iLeftSize + 1);
      	strRet[0] = left[0]; //符號位
      	char Step = 0; //進(jìn)位
      	for (int iIdx = 1; iIdx < iLeftSize; ++iIdx)
      	{
      		char cRet = left[iLeftSize - iIdx] - '0' + Step;
      
      		if (iIdx < iRightSize)
      			cRet += right[iRightSize - iIdx] - '0';
      
      		strRet[iLeftSize - iIdx + 1] = cRet % 10 + '0';
      		Step = cRet / 10;
      	}
      
      	strRet[1] = Step + '0';
      	
      	return strRet;
      }
      
      std::string BigData::Sub(std::string left, std::string right)
      {
      	int iLeftSize = left.size();
      	int iRightSize = right.size();
      	char cSymbol = left[0]; //差的符號位
      
      	if ((iLeftSize < iRightSize) || 
      		(iLeftSize == iRightSize && left < right))
      	{
      		std::swap(left, right);
      		std::swap(iLeftSize, iRightSize);
      
      		if (cSymbol == '+')
      			cSymbol = '-';
      		else
      			cSymbol = '+';
      	}
      
      	//相減
      	std::string strRet; //保存差
      	strRet.resize(left.size()); //先設(shè)置和左操作數(shù)一樣大的空間
      	strRet[0] = cSymbol; //符號位
      
      	for (int Idx = 1; Idx < iLeftSize; ++Idx)
      	{
      		char cRet = left[iLeftSize - Idx] - '0';
      		
      		if (Idx < iRightSize)
      			cRet -= (right[iRightSize - Idx] - '0');
      
      		if (cRet < 0) //有借位
      		{
      			left[iLeftSize - Idx - 1] -= 1;
      			cRet += 10;
      		}
      
      		strRet[iLeftSize - Idx] = cRet + '0';
      	}
      
      	return strRet;
      }
      
      std::string BigData::Mul(std::string left, std::string right)
      {
      	//確定符號位
      	char cSymbol = '+';
      	if (left[0] != right[0])
      		cSymbol = '-';
      
      	//使左操作數(shù)位數(shù)小于右操作數(shù)位數(shù)
      	int iLeftSize = left.size();
      	int iRightSize = right.size();
      	if (iLeftSize > iRightSize)
      	{
      		std::swap(left, right);
      		std::swap(iLeftSize, iRightSize);
      	}
      
      	std::string strRet;
      	//strRet.resize(iLeftSize+iRightSize-1);
      	strRet.assign(iLeftSize+iRightSize-1, '0');
      	strRet[0] = cSymbol;
      	int iRetLen = strRet.size();
      	int iOffset = 0; //偏移
      
      	for (int iLeftIndex = 1; iLeftIndex < iLeftSize; ++iLeftIndex)
      	{
      		char cLeft = left[iLeftSize - iLeftIndex] - '0';
      		
      		if (cLeft == 0) //當(dāng)左操作數(shù)中有0時,直接左移,不計算
      		{
      			++iOffset;
      			continue;
      		}
      
      		char cStep = 0;
      		for (int iRightIndex = 1; iRightIndex < iRightSize; ++iRightIndex)
      		{
      			char cRet = cLeft*(right[iRightSize - iRightIndex] - '0') + cStep;
      			cRet += strRet[iRetLen - iRightIndex - iOffset] - '0';
      			strRet[iRetLen - iRightIndex - iOffset] = (cRet%10) + '0';
      			cStep = cRet/10;
      		}
      		strRet[iRetLen-iRightSize -iOffset] += cStep;
      		++iOffset;
      	}
      
      	return strRet;
      }
      
      std::string BigData::Div(std::string left, std::string right)
      {
      	std::string strRet;
      	
      	//確定符號位
      	strRet.append(1, '+');
      	if (left[0] != right[0])
      		strRet[0] = '-';
      
      	char* pLeft = (char*)(left.c_str()+1);
      	char* pRight = (char*)(right.c_str()+1);
      	int iLSize = left.size()-1; //被除數(shù)長度
      	int iDataLen = right.size()-1; //取iDataLen位被除數(shù)
      
      	int iIdx = 0; //iIdx和pLeft同步走,用來判斷是否走到被除數(shù)結(jié)尾
      	while (iIdx < iLSize)
      	{
      		if (*pLeft == '0')
      		{
      			strRet.append(1, '0');
      			pLeft++;
      			iIdx++;
      			continue;
      		}
      
      		if (!IsLeftStrBig(pLeft, iDataLen, pRight, right.size()-1)) //取的iDataLen位被除數(shù) < 除數(shù)
      		{
      			strRet.append(1, '0');
      			iDataLen++;
      			if (iDataLen + iIdx > iLSize)
      				break;
      		}
      		else
      		{
      			//循環(huán)相減
      			strRet.append(1, SubLoop(pLeft, iDataLen, pRight, right.size()-1)); 
      			while (*pLeft == '0')
      			{
      				pLeft++;
      				iIdx++;
      				iDataLen--;
      			}
      
      			iDataLen++;
      		}
      	}
      
      	return strRet;
      }

      BigDataTest.cpp

      #include 
      using namespace std;
      #include "BigData.h"
      
      void AddTest()
      {
      	//BigData b1("12345678");
      	//BigData b2("12345qwe");
      	//BigData b3("+");
      	//BigData b4("00001234");
      	//BigData b5("   ");
      	//BigData b6("99999999999999999999999999999999999999999999999999999999");
      	//BigData b7 ("-123213");
      
      	//cout<>input;
      
      		if (input == "quit")
      		{
      			break;
      		}
      
      		if (input.find('+') != -1)
      		{
      			string left = input.substr(0, input.find('+'));
      			string right = input.substr(input.find('+')+1, input.length()-1);
      			BigData bigdata1(left.c_str());
      			BigData bigdata2(right.c_str());
      			cout<<"結(jié)果:  "<

      下面是一個簡單的測試:

      C++實現(xiàn)大數(shù)運算

      另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


      網(wǎng)頁標(biāo)題:C++實現(xiàn)大數(shù)運算-創(chuàng)新互聯(lián)
      標(biāo)題URL:http://www.ef60e0e.cn/article/gsepj.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        怀仁县| 明星| 民丰县| 辽宁省| 名山县| 六安市| 蓝田县| 定州市| 乌恰县| 商水县| 娄底市| 莎车县| 勃利县| 阿巴嘎旗| 泗水县| 额尔古纳市| 栾城县| 华亭县| 苍梧县| 武定县| 都昌县| 河池市| 玛曲县| 潼南县| 射阳县| 贵州省| 府谷县| 赞皇县| 正定县| 莱阳市| 丹寨县| 揭西县| 全州县| 桃园县| 紫阳县| 遵化市| 法库县| 承德县| 平顶山市| 隆回县| 故城县|