Exemple #1
0
std::string BigData::Div(std::string left, std::string right)
{
	char lsize = left.size();
	char rsize = right.size();
	char csymbol = '+';
	if (left[0] != right[0])
	{
		csymbol = '-';
	}
	if ((lsize < rsize) ||
		(lsize == rsize && strcmp(left.c_str() + 1, right.c_str() + 1) < 0))
	{
		return "0";
	}
	else
	{
		if (right == "-1" || right == "+1")
		{
			left[0] = csymbol;
			return left;
		}
	}
	string strret;
	strret.append(1, csymbol);
	char *pleft = (char*)(left.c_str() + 1);
	char *pright = (char*)(right.c_str() + 1);
	int datalen = 1;
	lsize -= 1;
	for (idx = 0; idx < lsize;)
	{
		if (*pleft == '0')
		{
			strret.append(1, '0');
			pleft++;
			idx++;
			continue;
		}
		if (!IsLeftStrBig(pleft, datalen, pright, rsize - 1))
		{
			strret.append(1, '0');
			datalen++;
			if (idx + datalen > lsize)
			{
				break;
			}
			continue;
		}
		else
		{
			strret.append(1, SubLoop(pleft, datalen, pright, rsize - 1));
			datalen++;
		}
	}
	return strret;
}
std::string BigData::Div(std::string left, std::string right)
{
	string sRet;
	sRet.append(1, '+');
	if (left[0] != right[0])
	{
		sRet[0] = '-';
	}

	char *pLeft = (char *)(left.c_str() + 1);
	char *pRight = (char *)(right.c_str() + 1);
	int DataLen = right.size() - 1;
	int Lsize = left.size() - 1;
	for (int i = 0; i < Lsize; i++)
	{
		if (!IsLeftStrBig(pLeft, DataLen, pRight, right.size() - 1))
		{
			sRet.append(1, '0');
			DataLen++;
		}
		else
		{
			sRet.append(1, SubLoop(pLeft, DataLen, pRight, right.size() - 1));
		}

		if (DataLen + i>Lsize)
		{
			break;
		}

		if (*pLeft == '0')
		{
			pLeft++;
		}
		else
		{
			DataLen = right.size();
		}

	}
	return sRet;
}
Exemple #3
0
std::string BigData::Div(std::string left, std::string right)
{
	char cSymbol = '+';
	if (left[0] != right[0])
	{
		cSymbol = '-';
	}

	int iLSize = left.size();
	int iRSize = right.size();
	if (iLSize < iRSize ||
		iLSize == iRSize && strcmp(left.c_str() + 1, right.c_str() + 1) < 0)
	{
		return "0";
	}
	else
	{
		if ("+1" == right || "-1" == right)
		{
			left[0] = cSymbol;
			return left;
		}
	}

	std::string strRet;
	strRet.append(1, cSymbol);
	char *pLeft = (char*)(left.c_str() + 1);
	char *pRight = (char*)(right.c_str() + 1);
	int iDataLen = 1;
	iLSize -= 1;
	// "2422222222"  33
	for (int iIdx = 0; iIdx < iLSize;)
	{
		if ('0' == *pLeft)
		{
			strRet.append(1, '0');
			pLeft++;
			iIdx++;

			continue;
		}

		if (!IsLeftStrBig(pLeft, iDataLen, pRight, iRSize - 1))
		{
			strRet.append(1, '0');
			iDataLen++;
			if (iIdx + iDataLen > iLSize)
			{
				break;
			}
			continue;
		}
		else
		{
			// 循环相减
			strRet.append(1, SubLoop(pLeft, iDataLen, pRight, iRSize - 1));

			// pLeft
			while ('0' == *pLeft && iDataLen > 0)
			{
				pLeft++;
				iIdx++;
				iDataLen--;
			}

			iDataLen++;
			if (iIdx + iDataLen > iLSize)
			{
				break;
			}
		}
	}

	return strRet;
}