本文整理汇总了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;
}
示例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
}