Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int subInBase(const int base, const int first, const int second) {
- if (base < 1 || base > 10) {
- puts("bad base");
- return INT_MAX;
- }
- char sgn = 1;
- int cp_first, cp_second;
- int result = 0;
- if (first >= second) {
- cp_first = first;
- cp_second = second;
- } else {
- cp_first = second;
- cp_second = first;
- sgn = -1;
- }
- int carry = 0;
- int k = 1;
- while (cp_first > 0) {
- int sub = cp_first % 10 - cp_second % 10;
- if (sub < 0) {
- result += (sub + base) * k;
- carry = 1;
- } else {
- result += sub * k;
- carry = 0;
- }
- k *= 10;
- cp_first /= 10;
- cp_first -= carry;
- cp_second /= 10;
- }
- return result * sgn;
- }
Advertisement
Add Comment
Please, Sign In to add comment