当前位置: 首页>>代码示例>>C++>>正文


C++ BigInteger::addHead方法代码示例

本文整理汇总了C++中BigInteger::addHead方法的典型用法代码示例。如果您正苦于以下问题:C++ BigInteger::addHead方法的具体用法?C++ BigInteger::addHead怎么用?C++ BigInteger::addHead使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BigInteger的用法示例。


在下文中一共展示了BigInteger::addHead方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: sumDigit

BigInteger BigInteger::sumDigit (BigInteger const &A, BigInteger const &B)
{
	int t = 0, sum = 0;
	Node *a;
	Node *b;
	Node *c;
	BigInteger S;
	a = A.pTail;
	b = B.pTail;
	while( a != NULL && b != NULL) 
	{
		sum = t + convertCharToInt(a->getValue()) + convertCharToInt(b->getValue()); // plus two node of A, B
		S.addHead(Node::getNode(convertIntToChar(sum % 10)));    // get the unit number eg 18 -> 8 -> node
		t = sum / 10; // get the left number 18 -> 1;
		a = a->getpPrev();
		b = b->getpPrev();
	}

	if( a == NULL){
		while( b != NULL) {               // scan the redundancy of b
			sum = t + convertCharToInt(b->getValue());
			S.addHead(Node::getNode(convertIntToChar(sum % 10)));
			t = sum/10;
			b = b->getpPrev();
		}
		S.digit = B.digit;
	}
	else if( b == NULL) {
		while( a != NULL) {               // scan the element of redundancy of a
			sum = t + convertCharToInt(a->getValue());
			S.addHead(Node::getNode(convertIntToChar(sum % 10)));
			t = sum/10;
			a = a->getpPrev();
		}
		S.digit = A.digit;
	}

	if( t== 1) {
		S.addHead(Node::getNode(convertIntToChar(t)));
		S.digit++;
	}
	S.setFlag(true);
	return S;
}
开发者ID:mrwen00,项目名称:BigNumber,代码行数:44,代码来源:BigInteger.cpp

示例2: subtractDigit

BigInteger BigInteger::subtractDigit (BigInteger const &A, BigInteger const &B)
{
	int t = 0, sum = 0;
	int x, y;
	Node *a;
	Node *b;
	Node *c;
	BigInteger S;
	a = A.pTail;
	b = B.pTail;
	while( a != NULL && b != NULL) 
	{
		x = convertCharToInt(a->getValue());
		y = convertCharToInt(b->getValue());
		if(t + x - y >= 0) {
			sum = t + x - y ; // subtract two node of A, B
			t = 0;
		}
		else {
			sum = 10 + t + x - y ;
			t = -1;          // borrow the previous unit
		}
		S.addHead(Node::getNode(convertIntToChar(sum)));    // get the unit number eg 18 -> 8 -> node
		S.digit++;
		a = a->getpPrev();
		b = b->getpPrev();
	}

	if( a == NULL){
		while( b != NULL) {               // scan the redundancy of b
			y = convertCharToInt(b->getValue());
			if(t - y >= 0) {
				sum = t - y ; // subtract two node of A, B
				t = 0;
			}
			else {
				sum = 10 + t - y ;
				t = -1;          // borrow the previous unit
			}
			S.addHead(Node::getNode(convertIntToChar(sum)));    // get the unit number eg 18 -> 8 -> node
			S.digit++;
			b = b->getpPrev();
		}
	}
	else if( b == NULL) {
		while( a != NULL) {               // scan the element of redundancy of a
			x = convertCharToInt(a->getValue());
			if(t + x >= 0) {
				sum = t + x ; // subtract two node of A, B
				t = 0;
			}
			else {
				sum = 10 + t + x;
				t = -1;          // borrow the previous unit
			}
			S.addHead(Node::getNode(convertIntToChar(sum)));    // get the unit number eg 18 -> 8 -> node
			S.digit++;
			a = a->getpPrev();
		}
	}
	S.setFlag(true);
	S.removePrefixZero();
	return S; // error automatically destructor and lost all of information
}
开发者ID:mrwen00,项目名称:BigNumber,代码行数:64,代码来源:BigInteger.cpp


注:本文中的BigInteger::addHead方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。