// Problem 5.7 recursive pseudocode version // for y > 0 only pow (x, y) { if ( y == 1 ) return x; res = pow(x, y >> 1); if (y & 1) // y is odd return x * res * res; else return res * res; } // Problem 5.8 solution EPI string ConvertBase (const string& s, int b1, int b2) { bool neg = s.front() == '-'; int x = 0; for (size_t i = (neg == true ? 1 : 0); i < s.size(); ++i) { x *= b1; x += isdigit(s[i]) ? s[i] - '0' : s[i] - 'A' + 10; } string ans; while (x) { int r = r % b2; ans.push_back(r >= 10 ? 'A' + r - 10 : '0' + r); x /= b2; } if (ans.empty()) { // special case: s is 0. ans.push_back('0'); } if (neg) { // s is a negative number ans.push_back('-'); } reverse(ans.begin(), ans.end() ); return ans; }