Beispiel #1
0
	LongInt* Multi(LongInt* otherInt) //multiplication
	{
		LongInt *result = new LongInt();
		//sign
		if(intData->head->value == otherInt->intData->head->value)
			result->intData->head->value = 0;
		else
			result->intData->head->value = 1;

		LList* myList = intData;
        LNode *myNode = intData->FirstRight();
		LList *otherList = otherInt->intData;

        //the length of the result
		for(int i = 0; i < myList->count + otherList->count; i++)
		{
			LNode* zeroNode = new LNode();
			zeroNode->value = 0;
			zeroNode->next = zeroNode->prev = NULL;
			result->intData->InsertLeft(zeroNode);
			zeroNode = NULL;
		}


		for(int i = 1; myNode != myList->head; myNode = myNode->prev, i++)
		{
			LNode* otherNode = otherList->FirstRight();
			for(int j = 1; otherNode != otherList->head; otherNode = otherNode->prev, j++)
			{
				int myValue =  myNode->value;
				int otherValue = otherNode->value;
				int tempResult = myValue * otherValue;
				int carry = OverFlow(tempResult);
				tempResult = tempResult - carry * (int)pow(10.0, (double)EVERY_NODE_LEN);

				LNode* resultNode = result->intData->CountFromRight(i + j - 1);
				LNode* carryNode = result->intData->CountFromRight(i + j);

				resultNode->value += tempResult; //may carry
				int carry2 = OverFlow(resultNode->value);
				resultNode->value = resultNode->value - carry2 * (int)pow(10.0, (double)EVERY_NODE_LEN);

				carryNode->value += carry + carry2; //may carry
				int carry3 = OverFlow(carryNode->value);
				carryNode->value = carryNode->value - carry3 * (int)pow(10.0, (double)EVERY_NODE_LEN);

				carryNode->prev->value += carry3;


			}
		}

		LNode* delNode = result->intData->head->next;
		while(delNode->value == 0)
		{
			result->intData->head->next = result->intData->head->next->next;
			result->intData->head->next->prev = result->intData->head;
			delete delNode;
			delNode = result->intData->head->next;
		}

		return result;

	}
Beispiel #2
0
	LongInt* Add(LongInt* otherInt) //addition
	{
		LongInt *result = new LongInt();
		LList* otherList = otherInt->intData;
		LList* resultList = result->intData;
		LNode* myNode = intData->FirstRight();
		LNode* otherNode = otherList->FirstRight();

		int addition = 0;
		int carry = 0;
		LNode* tempNode;

		while(intData->count != otherList->count)
		{
			LNode* zeroNode = new LNode();
			zeroNode->value = 0;
			zeroNode->next = zeroNode->prev = NULL;
			if(intData->count > otherList->count)
				otherList->InsertLeft(zeroNode);
			else
				intData->InsertLeft(zeroNode);
			zeroNode = NULL;
		}

		if(intData->head->value == otherList->head->value)
		{
			resultList->head->value = intData->head->value;

			while(myNode != intData->head)
			{
				addition = myNode->value + otherNode->value + carry;
				carry = OverFlow(addition);
				addition = addition - carry * (int)pow(10.0, (double)EVERY_NODE_LEN);

				tempNode = new LNode();
				tempNode->value = addition;
				tempNode->prev = tempNode->next = NULL;
				resultList->InsertLeft(tempNode);
				tempNode = NULL;
				myNode = intData->NextLeft(myNode);
				otherNode = otherList->NextLeft(otherNode);
			}

			if(carry != 0)
			{
				LNode* carryNode = new LNode();
				carryNode->value = carry;
				carryNode->next = carryNode->prev = NULL;
				resultList->InsertLeft(carryNode);
			}
		}
		else //opposite sign
		{
			LList *leftList, *rightList;

			int abs = CompAbs(otherInt);
			if(abs == 0)
			{
				LNode* zeroNode = new LNode();
				zeroNode->value = 0;
				zeroNode->next = zeroNode->prev = NULL;
				resultList->InsertLeft(zeroNode);
				return result;
			}
			else if(abs == 1)
			{
				resultList->head->value = intData->head->value;
				leftList = intData;
				rightList = otherInt->intData;
			}
			else
			{
				resultList->head->value = otherInt->intData->head->value;
				leftList = otherInt->intData;
				rightList = intData;
			}

			myNode = leftList->FirstRight();
			otherNode = rightList->FirstRight();
			carry = 0;

			while(myNode != leftList->head)
			{

				addition = myNode->value - otherNode->value + carry;
				if(addition < 0)
				{
					carry = -1;
					addition = addition + (int)pow(10.0, (double)EVERY_NODE_LEN);
				}
				else
					carry = 0;

				tempNode = new LNode();
				tempNode->value = addition;
				tempNode->prev = tempNode->next = NULL;
				resultList->InsertLeft(tempNode);
				tempNode = NULL;
				myNode = leftList->NextLeft(myNode);
				otherNode = rightList->NextLeft(otherNode);
			}
		}

		return result;
	}