unsigned int BaseConverter::divide(const std::string& baseDigits, std::string& x, unsigned int y) { std::string quotient { }; size_t lenght = x.length(); for (size_t i = 0; i < lenght; ++i) { size_t j = i + 1 + x.length() - lenght; if (x.length() < j) break; unsigned int value = base2dec(baseDigits, x.substr(0, j)); quotient.push_back(baseDigits[value / y]); x = dec2base(baseDigits, value % y) + x.substr(j); } // calculate remainder unsigned int remainder = base2dec(baseDigits, x); // remove leading "zeros" from quotient and store in 'x' size_t n = quotient.find_first_not_of(baseDigits[0]); if (n != std::string::npos) { x = quotient.substr(n); } else { x.clear(); } return remainder; }
int main(void) { float num; int base; scanf("%f %d", &num, &base); char *bin = dec2base(num, base); printf("%s\n", bin); free(bin); return 0; }
char *dec2bin(int num) { return dec2base(num, 2); }
std::string BaseConverter::fromDecimal(unsigned int value) const { return dec2base(targetBaseSet_, value); }