Skip to content

Commit 18a23d0

Browse files
committed
3.6
1 parent d6e7371 commit 18a23d0

11 files changed

+197
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

chapter-3-control-flow/09.itoa.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <limits.h>
4+
5+
void itoa(int n, char s[]);
6+
void reverse(char[]);
7+
8+
9+
main()
10+
{
11+
int a = 0;
12+
char s[10];
13+
itoa(a, s);
14+
printf("%s\n", s);
15+
16+
a = INT_MIN;
17+
itoa(a, s);
18+
printf("%s\n", s);
19+
20+
a = INT_MAX;
21+
itoa(a, s);
22+
printf("%s\n", s);
23+
}
24+
25+
void itoa(int n, char s[])
26+
{
27+
int i, sign;
28+
int preN = n;
29+
int nIsMinNagetive = n != 0 && n == -n;
30+
31+
if ((sign = n) < 0) {
32+
if (nIsMinNagetive) {
33+
n += 1;
34+
n = -n;
35+
} else {
36+
n = -n;
37+
}
38+
}
39+
40+
i = 0;
41+
do {
42+
s[i++] = n % 10 + '0';
43+
} while ((n /= 10) > 0);
44+
45+
if (sign < 0)
46+
s[i++] = '-';
47+
48+
s[i] = '\0';
49+
reverse(s);
50+
51+
if (nIsMinNagetive)
52+
s[strlen(s) - 1] += 1;
53+
}
54+
55+
void reverse(char s[])
56+
{
57+
int c, i, j;
58+
59+
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
60+
c = s[i];
61+
s[i] = s[j];
62+
s[j] = c;
63+
}
64+
}

chapter-3-control-flow/10.itob.c

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <limits.h>
5+
6+
#define MAX_LEN 1024
7+
8+
void itob(int n, char s[], int b);
9+
void itob2(int n, char s[], int b);
10+
void reverse(char[]);
11+
12+
main()
13+
{
14+
int n = 255;
15+
char s[MAX_LEN];
16+
itob(n, s, 16);
17+
printf("n: %d, s: %s\n", n, s);
18+
19+
n = INT_MAX;
20+
itob(n, s, 16);
21+
printf("n: %d, s: %s\n", n, s);
22+
23+
n = 2048;
24+
itob(n, s, 2);
25+
printf("n: %d, s: %s\n", n, s);
26+
27+
n = 2048;
28+
itob(n, s, 8);
29+
printf("n: %d, s: %s\n", n, s);
30+
31+
n = INT_MIN;
32+
itob(n, s, 16);
33+
printf("n: %d, s: %s\n", n, s);
34+
}
35+
36+
void itob(int n, char s[], int b)
37+
{
38+
int i;
39+
int sign = n;
40+
41+
i = 0;
42+
do {
43+
char c;
44+
int m = abs(n % b);
45+
if (m < 10)
46+
c = m + '0';
47+
else
48+
c = m - 10 + 'a';
49+
s[i++] = c;
50+
} while (n /= b);
51+
52+
if (sign < 0)
53+
s[i++] = '-';
54+
55+
s[i] = '\0';
56+
reverse(s);
57+
}
58+
59+
void reverse(char s[])
60+
{
61+
int c, i, j;
62+
63+
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
64+
c = s[i];
65+
s[i] = s[j];
66+
s[j] = c;
67+
}
68+
}
69+

chapter-3-control-flow/11.itoa.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <limits.h>
5+
6+
#define MAX_LEN 1024
7+
8+
void itoa(int n, char s[], int minWidth);
9+
void reverse(char[]);
10+
11+
12+
main()
13+
{
14+
int a = 0;
15+
char s[MAX_LEN];
16+
itoa(a, s, 5);
17+
printf("a: %d, %s\n", a, s);
18+
19+
a = INT_MIN;
20+
itoa(a, s, 20);
21+
printf("INT_MIN: %d, %s\n", a, s);
22+
23+
a = INT_MAX;
24+
itoa(a, s, 15);
25+
printf("INT_MAX: %d, %s\n", a, s);
26+
27+
a = 1234567890;
28+
itoa(a, s, 5);
29+
printf("a: %d, %s\n", a, s);
30+
}
31+
32+
void itoa(int n, char s[], int minWidth)
33+
{
34+
int i;
35+
int sign = n;
36+
37+
i = 0;
38+
do {
39+
s[i++] = abs(n % 10) + '0';
40+
} while (n /= 10);
41+
42+
if (sign < 0)
43+
s[i++] = '-';
44+
45+
int needSpace = minWidth - strlen(s);
46+
while (needSpace-- > 0) {
47+
s[i++] = ' ';
48+
}
49+
50+
s[i] = '\0';
51+
reverse(s);
52+
}
53+
54+
void reverse(char s[])
55+
{
56+
int c, i, j;
57+
58+
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
59+
c = s[i];
60+
s[i] = s[j];
61+
s[j] = c;
62+
}
63+
}
64+

0 commit comments

Comments
 (0)