Question: C++.I'm trying to do a substract and a divison function that substracts two binary strings numbers. The calcul has to be direct from string to
C++.I'm trying to do a substract and a divison function that substracts two binary strings numbers. The calcul has to be direct from string to string(no convertion allowed)
This is how my functions are looking already but they don't quite work. How can I make them work, or has anyone simpler functions?
string SoustracBin(string bin1, string bin2){ string sous; string b; b='1'; if(bin1==bin2){ sous='0'; ; } else { while (bin2.size() != bin1.size()) { bin2 = '0' + bin2; } for (char & i : bin2) { if (i == '0') { i = '1'; } else if (i == '1') { i = '0'; } } bin2 = SumBin(bin2, b); sous = SumBin(bin1, bin2); int i = 0; while (sous.size() > bin1.size()) { //resize du num binaire au resultat juste. i++; sous.erase(0, i); } i=1; while(sous[0]=='0') { // si il a un 0 au debut du binare, ca supprime sous.erase(0, i); } } return sous; } string divBin(string bin1, strig bin2){ string result=""; string remainder = ""; for (int i = bin1.size(); i>=0 ; i--) { remainder += bin1[i]; if (remainder.size() < bin2.size()) { result += "0"; continue; } else if (remainder.size() >= bin2.size()) { result += "1"; remainder = SoustracBin(remainder, bin2); } else result+='0'; } cout<
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
