#include <iostream> #include <string> #include <cmath> using namespace std; string int2bin8(unsigned char num) { char charArr[9]; itoa(num, charArr, 2); string res(charArr); res.insert(0, 8 - res.size(), '0'); return res; } string int2bin24(unsigned int num) { char charArr[33]; itoa(num, charArr, 2); string res(charArr); res.insert(0, 32 - res.size(), '0'); return res; } int main(int argc, char* argv[]) { double iA, iB; cout << "Dilene: "; cin >> iA; if (iA < 0) iA *= -1; cout << "Dilnuk: "; cin >> iB; if (iB < 0) iB *= -1; bool znakA = iA < 0, znakB = iB < 0; int oA = 0, oB = 0; if (iA >= 2) while (iA >= 2) { iA /= 2; oA++; } else if (iA < 1) while (iA < 1) { iA *= 2; oA--; } if (iB >= 2) while (iB >= 2) { iB /= 2; oB++; } else if (iB < 1) while (iB < 1) { iB *= 2; oB--; } int orR = oA - oB; string bin_A = "", bin_B = ""; double temp = iA - (int)iA; for (int i = 0; i < 23; i++) { temp *= 2; bin_A += to_string((int)temp); temp -= (int)temp; } temp = iB - (int)iB; for (int i = 0; i < 23; i++) { temp *= 2; bin_B += to_string((int)temp); temp -= (int)temp; } cout << " A: " << 1 << " " << int2bin8(oA + 127) << " " << bin_A << endl; cout << " B: " << 0 << " " << int2bin8(oB + 127) << " " << bin_B << endl; cout << endl; unsigned long long A, B, R; A = stoi("1" + bin_A, nullptr, 2); B = stoi("1" + bin_B, nullptr, 2); R = 0; //AAAAAAAAAAAAAAAAAAAAAAA cout << "Zsuv dilnuka do spivpadannya starshuh cufer\n"; int dilene_d = 0, dilnik_d = 0; while (A >> dilene_d != 0) dilene_d++; while (B >> dilnik_d != 0) dilnik_d++; int n = dilene_d - dilnik_d; B = B << n; n++; R = 0; cout << " Deline:\n " << int2bin24(A) << "\n Dilnuk:\n " << int2bin24(B) << "\n Chastka:\n " << int2bin24(R) << endl; while (n > 0) { cout << "Porivnyannya:\n"; A -= B; R *= 2; if ((long long)A >= 0) R++; cout << " Dilene:\n " << int2bin24(A) << "\n Dilnuk:\n " << int2bin24(B) << "\n Chastka:\n " << int2bin24(R) << endl; if ((long long)A < 0) { cout << "Vidnovlennya zalushky:\n"; A += B; cout << " Dilene:\n " << int2bin24(A) << "\n Dilnuk:\n " << int2bin24(B) << "\n Chastka:\n " << int2bin24(R) << endl; } n--; if (n <= 0) break; cout << "Zsuv dilnuka:\n"; B /= 2; cout << " Dilene:\n " << int2bin24(A) << "\n Dilnuk:\n " << int2bin24(B) << "\n Chastka:\n " << int2bin24(R) << endl; } //AAAAAAAAAAAAAAAAAAAAAAA while (R < 8388608) { cout << "Porivnyannya:\n"; A *= 2; R *= 2; A -= B; if ((long long)A >= 0) R++; cout << " Dilene:\n " << int2bin24(A) << "\n Dilnuk:\n " << int2bin24(B) << "\n Chastka:\n " << int2bin24(R) << endl; if ((long long)A < 0) { cout << "Vidnovlennya zalushky:\n"; A += B; cout << " Dilene:\n " << int2bin24(A) << "\n Dilnuk:\n " << int2bin24(B) << "\n Chastka:\n " << int2bin24(R) << endl; } } double res2show = R / pow(2, 23); if (res2show * pow(2, orR) != (iA * pow(2, oA))*(iB * pow(2, oB))) { orR--; } res2show *= pow(2, orR); cout << " C= -" << res2show << endl; return 0; } 

How to replace itoa() function in this code? The code itself must divide the floating point number.

    1 answer 1

    If I understand you correctly, then you need the following

     #include <string> #include <bitset> //... unsigned char c = 15; std::bitset<32> b(c); std::string res = b.to_string(); std::cout << res << std::endl; 

    The output to the console of this code snippet will be as follows.

     00000000000000000000000000001111 

    For example, the function int2bin8 might look like this.

     std::string int2bin8(unsigned char num) { std::bitset<8> b(num); return b.to_string(); } 

    Or you can even write in one line

     std::string int2bin8(unsigned char num) { return std::bitset<8>(num).to_string(); }