Created
April 18, 2017 01:55
-
-
Save Folling/605c65c3967d6a95414bc63fca73c785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
largeNum operator+(largeNum& summand1, largeNum& summand2) { | |
largeNum returnNum; | |
//macros to shorten the code | |
std::vector<int> &tmp1 = summand1.getValue(); | |
std::vector<int> &tmp2 = summand2.getValue(); | |
int size; | |
bool storedTen = false; | |
int temp; | |
if (summand1 == *zero) return summand2; | |
if (summand2 == *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 (summand1.sign == '+' && summand2.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 (summand1.sign == '-' && summand2.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 (summand1.compare(summand2) == 0) return *zero; // -x + x = 0 | |
if (summand1.compare(summand2) == 1) returnNum.sign = summand1.sign; | |
else returnNum.sign = summand2.sign; | |
//for more information on how this part works check: https://www.youtube.com/watch?v=PS5p9caXS4U | |
int chosenMethod; | |
//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 (summand1.sign == '-') { | |
chosenMethod = 1; | |
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 { | |
chosenMethod = 2; | |
largeNum tmp; | |
if (summand2 > summand1) returnNum.changeSign(); | |
tmp = summand1; | |
summand1 = summand2; | |
summand2 = 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 (chosenMethod == 2) 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