Skip to content

Commit

Permalink
fix max function string comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Last-Cookie committed Dec 22, 2020
1 parent 0c21a81 commit a1e5deb
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions stringcalc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,31 @@ std::string Decimal::max(std::string str1, std::string str2) {
std::cout << "Error: Strings may only contain positive decimal numbers!" << "\n";
return "";
}

// Remove leading zeros on both strings
for (int i = 0; i < str1.length() - 1; i++) {
if (str1[0] == '0') {
str1.erase(0, 1);

// In case difference contains several zeros
i--;
}
else {
break;
}
}

for (int i = 0; i < str2.length() - 1; i++) {
if (str2[0] == '0') {
str2.erase(0, 1);

// In case difference contains several zeros
i--;
}
else {
break;
}
}

if (str1.length() > str2.length()) {
return str1;
Expand Down Expand Up @@ -513,6 +538,31 @@ std::string Binary::max_b(std::string str1, std::string str2) {
return "";
}

// Remove leading zeros on both strings
for (int i = 0; i < str1.length() - 1; i++) {
if (str1[0] == '0') {
str1.erase(0, 1);

// In case difference contains several zeros
i--;
}
else {
break;
}
}

for (int i = 0; i < str2.length() - 1; i++) {
if (str2[0] == '0') {
str2.erase(0, 1);

// In case difference contains several zeros
i--;
}
else {
break;
}
}

if (str1.length() > str2.length()) {
return str1;
}
Expand Down Expand Up @@ -750,6 +800,31 @@ std::string Hexadecimal::max_h(std::string str1, std::string str2) {
return "";
}

// Remove leading zeros on both strings
for (int i = 0; i < str1.length() - 1; i++) {
if (str1[0] == '0') {
str1.erase(0, 1);

// In case difference contains several zeros
i--;
}
else {
break;
}
}

for (int i = 0; i < str2.length() - 1; i++) {
if (str2[0] == '0') {
str2.erase(0, 1);

// In case difference contains several zeros
i--;
}
else {
break;
}
}

if (str1.length() > str2.length()) {
return str1;
}
Expand Down

0 comments on commit a1e5deb

Please sign in to comment.