-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-4.c
More file actions
71 lines (58 loc) · 979 Bytes
/
3-4.c
File metadata and controls
71 lines (58 loc) · 979 Bytes
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <u.h>
#include <libc.h>
#define MAXNEG -2147483648
void reverse(char s[]);
int itoa(int n, char s[]);
void
main(void)
{
print("KR problem 3-4\n");
vlong t = -214748364811LL;
print("t = %lld\n", t);
int temp = 1;
char buf[128];
itoa(temp, buf);
print("%s\n", buf);
exits(nil);
}
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
int itoa(int n, char s[])
{
/* a moment of barbarism */
if (n == MAXNEG) {
maxneg:
s[0] = '-';
s[1] = '2';
s[2] = '1';
s[3] = '4';
s[4] = '7';
s[5] = '4';
s[6] = '8';
s[7] = '3';
s[8] = '6';
s[9] = '4';
s[10] = '8';
s[11] = '\0';
return 0;
}
int i, sign;
if ((sign = n) < 0)
n = -n;
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = 0;
reverse(s);
return i;
}