-
Notifications
You must be signed in to change notification settings - Fork 0
/
e3-6.c
55 lines (41 loc) · 734 Bytes
/
e3-6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
void itoa (int n, char *s, int w);
void reverse(char *s);
int size(char *s);
int main(void) {
char *num = malloc(sizeof(char)*32);
int a = 1 << 31;
int b = ~a;
printf("%d\n", a);
itoa(a, num, 15);
printf(">> %s\n", num);
return 0;
}
void itoa (int n, char *s, int w) {
int sign = n;
n = (n > 0) ? -n : n;
do {
*s++ = -(n % 10) + '0';
--w;
} while ((n /= 10) < 0);
if(sign < 0)
*s++ = '-';
for(; w >= 0; --w)
*s++ = ' ';
*s = '\0';
reverse(s);
}
int size(char *s) {
int i = 0;
for(; *s++ != '\0'; ++i);
return i;
}
void reverse(char *s) {
char *end = s + size(s) - 1, temp = 0;
for(; s < end; ++s, --end) {
temp = *s;
*s = *end;
*end = temp;
}
}