Skip to content

Instantly share code, notes, and snippets.

@Folling
Created May 10, 2017 20:19
Show Gist options
  • Save Folling/58fb471b8e2ef6313a1ca0529bbe6107 to your computer and use it in GitHub Desktop.
Save Folling/58fb471b8e2ef6313a1ca0529bbe6107 to your computer and use it in GitHub Desktop.
largeNum operator+(largeNum& summand1, largeNum& summand2)
{
largeNum returnNum;
//macros to shorten the code
largeNum LNtmp1 = summand1;
largeNum LNtmp2 = summand2;
std::vector<int>& tmp1 = LNtmp1.getValue();
std::vector<int>& tmp2 = LNtmp2.getValue();
int size;
bool storedTen = false;
int temp;
if (LNtmp1 == zero) return summand2;
if (LNtmp2 == zero) return summand1;
//adapts the smaller vector to the larger vector by resizing him and reversing him so 1 turns into 00...01
if (tmp1.size() > tmp2.size())
{
//puts as many 0s in front of the number as there are more digits in the other number
size = tmp1.size();
while (tmp1.size() != tmp2.size())
{
tmp2.insert(tmp2.begin(), 0);
}
}
else if (tmp2.size() > tmp1.size())
{
//puts as many 0s in front of the number as there are more digits in the other number
size = tmp2.size();
while (tmp2.size() != tmp1.size())
{
tmp1.insert(tmp1.begin(), 0);
}
}
else size = tmp1.size();
std::vector<int> returnValue(size + 1);
// if a digit + a digit exceeds 10 this gets stored to be added to the next comparison
if (LNtmp1.sign == '+' && LNtmp2.sign == '+')
{
//adds all numbers together from right to left
for (int i = size - 1; i >= 0; i--)
{
//checks whether the addition exceeds 10 to store in "storedTen"
if (tmp1.at(i) + tmp2.at(i) + storedTen > 9)
{
temp = tmp1.at(i) + tmp2.at(i) + storedTen - 10;
storedTen = true;
}
else
{
temp = tmp1.at(i) + tmp2.at(i) + storedTen;
storedTen = false;
}
returnValue.at(i + 1) = temp;
}
//should the last addition have been larger than 10 the end number will have to be increased by one more digit
//e.g. 5+5 = 10
if (storedTen == 1) returnValue[0] = 1;
returnNum.setValue(returnValue);
}
else if (LNtmp1.sign == '-' && LNtmp2.sign == '-')
{
returnNum.sign = '-';
//adds all numbers together from right to left
for (int i = size - 1; i >= 0; i--)
{
//checks whether the addition exceeds 10 to store in "storedTen"
if (tmp1.at(i) + tmp2.at(i) + storedTen > 9)
{
temp = tmp1.at(i) + tmp2.at(i) + storedTen - 10;
storedTen = true;
}
else
{
temp = tmp1.at(i) + tmp2.at(i) + storedTen;
storedTen = false;
}
returnValue.at(i + 1) = temp;
}
if (returnValue.at(0) == 0) returnValue.erase(returnValue.begin());
if (storedTen == 1) returnValue.insert(returnValue.begin(), 1);
returnNum.setValue(returnValue);
}
//case for when the signs are different
else
{
if (LNtmp1.compare(LNtmp2) == 0) return zero; // -x + x = 0
if (LNtmp1.compare(LNtmp2) == 1) returnNum.sign = LNtmp1.sign;
else returnNum.sign = LNtmp2.sign;
//for more information on how this part works check: https://www.youtube.com/watch?v=PS5p9caXS4U
//turns the in the video described method, summand which will function as the subtrahend and changes it as required
//with the difference that it doesn't reduce it by 10 at the end since we will invert the result
if (LNtmp1.sign == '-')
{
for (int i = 0; i < size; i++)
{
//in either case if the digit is 0 it must not be inverted due to us using negative numbers
if (i == size - 1)
{
tmp1.at(i) = (10 - tmp1.at(i));
break;
}
tmp1.at(i) = (9 - tmp1.at(i));
}
}
else
{
if (LNtmp2 > LNtmp1) returnNum.changeSign();
largeNum tmp = LNtmp1;
LNtmp1 = LNtmp2;
LNtmp2 = tmp;
for (int i = 0; i < size; i++)
{
if (i == size - 1)
{
tmp1.at(i) = (10 - tmp1.at(i));
break;
}
tmp1.at(i) = (9 - tmp1.at(i));
}
}
//adds newly created vector with the original summand
for (int i = size - 1; i >= 0; i--)
{
//checks whether the addition exceeds 10 to store in "storedTen"
if (tmp1.at(i) + tmp2.at(i) + storedTen > 9)
{
temp = tmp1.at(i) + tmp2.at(i) + storedTen - 10;
storedTen = true;
}
else
{
temp = tmp1.at(i) + tmp2.at(i) + storedTen;
storedTen = false;
}
returnValue.at(i + 1) = temp;
}
if (storedTen == true) returnValue.insert(returnValue.begin(), 1);
while (returnValue.at(0) == 0 && returnValue.size() != 1) returnValue.erase(returnValue.begin());
//in case the first summand is negative and smaller than the second or the second summand is the first digit gets removed
//otherwise the first digit has to stay due to negative numbers
//method doesn't work for negative numbers so we adapt it
//hereby we take the invert of the values
//since we only want to do 10-.at(i) we check whether it's already been done
if (returnNum.sign == '-')
{
bool addedTen = false;
for (int i = (returnValue.size() - 1); i >= 0; i--)
{
if (returnValue.at(i) == 0 && addedTen == false) continue;
if (addedTen == false)
{
returnValue.at(i) = 10 - returnValue.at(i);
addedTen = true;
}
//inverts the result because of negativity
else
{
returnValue.at(i) = 9 - returnValue.at(i);
}
if (addedTen == false)returnValue.at(returnValue.size() - 1)++;
}
}
if (returnNum.sign == '+')
{
if (tmp1.size() > 1 && tmp2.size() > 1) returnValue.insert(returnValue.begin(), 1);
returnValue.erase(returnValue.begin());
}
if (returnNum.sign == '+') returnValue.erase(returnValue.begin());
}
//gets rid of all 0s in front;
while (returnValue.at(0) == 0 && returnValue.size() != 1) returnValue.erase(returnValue.begin());
returnNum.setValue(returnValue);
return returnNum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment