Skip to content

Commit f5ac4f8

Browse files
committed
simplify code
1 parent 4add853 commit f5ac4f8

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

chapter03/3-5.c

+34-40
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,70 @@
22
* Exercise 3-5. Write the function itob(n,s,b) that converts the integer n
33
* into a base b character representation in the string s. In particular,
44
* itob(n,s,16) formats s as a hexadecimal integer in s.
5+
*
56
* By Faisal Saadatmand
67
*/
78

9+
#include <limits.h>
810
#include <stdio.h>
911
#include <string.h>
1012

13+
#define MAXLEN 1000
14+
1115
/* functions */
1216
void reverse(char []);
13-
void itob(int, char [], int);
17+
void itob(unsigned, char [], int);
1418

15-
/* reverse function: reverse string s in place */
19+
/* reverse: reverse string s in place */
1620
void reverse(char s[])
1721
{
1822
int c, i, j;
1923

20-
for (i = 0, j = strlen(s) - 1; i < j; i++, j --) {
24+
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
2125
c = s[i];
2226
s[i] = s[j];
2327
s[j] = c;
24-
--j;
2528
}
2629
}
2730

28-
/* itob: convert number into a base base character representation in the string s */
29-
void itob(int number, char convertedNumber[], int base)
31+
/* itob: converts the integer n into a base b character representation in the
32+
* string s. */
33+
void itob(unsigned n, char s[], int b)
3034
{
31-
int i, sign;
32-
unsigned int uNumber, nextDigit;
33-
const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
34-
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
35-
36-
sign = 0;
37-
if (number < 0) /* record sign */
38-
sign = 1;
35+
int i, sign, digit;
36+
const char baseDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
37+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
3938

40-
uNumber = (unsigned) number; /* work in two's complement number system */
41-
42-
if (sign > 0 && base == 10) /* convert back to decimal for base 10 */
43-
uNumber = ~uNumber + 1;
39+
if (b == 10 && (sign = n) < 0)
40+
n = -n; /* or you can use ~n + 1 */
4441

4542
i = 0;
46-
do { /* generate digits in reverse order */
47-
nextDigit = uNumber % base; /* get next digit */
48-
uNumber /= base;
49-
convertedNumber[i] = baseDigits[nextDigit];
50-
++i;
51-
} while (uNumber != 0);
43+
do { /* generate digits in reverse order */
44+
digit = n % b; /* get next digit */
45+
s[i++] = baseDigits[digit];
46+
} while ((n /= b) > 0);
5247

53-
if (sign > 0 && base == 10) /* add sign symbol for base 10 */
54-
convertedNumber[i++] = '-';
48+
if (b == 10 && sign < 0) /* add sign symbol for base 10 */
49+
s[i++] = '-';
5550

56-
convertedNumber[i] = '\0';
51+
s[i] = '\0';
5752

58-
reverse(convertedNumber);
53+
reverse(s);
5954
}
6055

6156
int main(void)
6257
{
63-
int number, base;
64-
char stringNumber[64];
65-
66-
printf("Enter an integer to convert: ");
67-
scanf("%i", &number);
68-
69-
printf("Enter base: ");
70-
scanf("%i", &base);
71-
72-
itob(number, stringNumber, base);
73-
74-
printf("%s\n", stringNumber);
58+
int i;
59+
char str[MAXLEN];
60+
61+
printf("converting %i and %i to\n", INT_MIN, INT_MAX);
62+
for (i = 2; i <= 16; ++i) {
63+
printf("Base% i:\n", i);
64+
itob(INT_MIN, str, i);
65+
printf(" %s\n", str);
66+
itob(INT_MAX, str, i);
67+
printf(" %s\n", str);
68+
}
7569

7670
return 0;
7771
}

0 commit comments

Comments
 (0)