Exemplo n.º 1
0
	void SetData(char* longNum) //set long integer
	{
		delete intData;
		intData = NULL;
		intData = new LList();
		intData->head->value = 0;

		int numLen = strlen(longNum);
		char* tempStr = NULL;
		while(numLen > 0) //get the last four digits everytime
		{
			tempStr = new char[EVERY_NODE_LEN + 1];
			tempStr[EVERY_NODE_LEN] = '\0';
			for(int i = EVERY_NODE_LEN-1, j = 0; i >= 0; i--, j++)
			{
				if (numLen-1-j >= 0)
					tempStr[i] = longNum[numLen-1-j];
				else
					tempStr[i] = '0';
			}

			LNode* tempNode = (LNode*) malloc(sizeof(LNode));
			tempNode->value = atoi(tempStr); //turn into integer
			tempNode->next = tempNode->prev = NULL;
			intData->InsertLeft(tempNode);

			tempNode = NULL;
			tempStr = NULL;
			numLen -= EVERY_NODE_LEN;
		}

	}
Exemplo n.º 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;
	}