k&r习题3-5,机械工业那本中文答案不咋的,只给了处理10进制以下的参考,本答案摘自网上的那本英文习题解答
/* itob(n,s,b) * converts the integer n into a base b char */ void itob(int n, char s[], int b) { static char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int i, sign; if ( b < 2 || b > 36 ) { fprintf(stderr, "EX3_5: Cannot support base %d\n", b); exit(EXIT_FAILURE); } if ((sign = n) < 0) n = -n; i = 0; do { s[i++] = digits[n % b]; } while ((n /= b) > 0); if (sign < 0) s[i++] = ‘-‘; s[i]=‘\0‘; reverse(s); }
时间: 2024-10-31 01:47:06