-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathb3k.c
102 lines (86 loc) · 2.18 KB
/
b3k.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <string.h>
#include <stdio.h>
/* Pictures of zero, largest positive and largest negative numbers. */
static const char *pat = "┌───┐\n"
"│ │\n"
"└───┘\n"
"├─┴─┤\n"
"┤ ├\n"
"├─┬─┤\n"
"┌┬┬┬┐\n"
"├ ┤\n"
"└┴┴┴┘\n";
/* Precomputed number of digits in a picture and byte size of it. */
#define SZD 8
#define SZP 42
/* Trits description in order from low to high values. */
struct ds {
int pos, len, exp, val;
} digit[SZD] = {
{ 32, 3 },
{ 26, 6 },
{ 16, 8 },
{ 0, 6 },
{ 6, 3 },
{ 9, 7 },
{ 21, 5 },
{ 35, 7 }
};
/* A number to convert and signed one to keep if it was negative. */
static int src = 0, inv = 1;
/* A buffer for the result. */
static char res[SZP+1];
/* Use it like 'echo -42 | ./b3k' */
int main (int q, char * argv[])
{
scanf("%d", &src);
/* Split the value into its modulus and signed multiplier. */
if (src < 0)
src *= inv = -1;
int r;
struct ds *p;
for (r = 0; r < SZD; ++r) {
p = &digit[r];
p->exp = r ? (3 * digit[r-1].exp) : 1;
p->val = 0;
}
/* Truncate to the largest allowed positive. */
p = &digit[SZD - 1];
r = 3 * p->exp / 2;
if (src > r)
src = r;
else
r = src;
/* Like 'echo "obase=3; $src" | bc'. */
do {
while (r < p->exp)
--p;
r -= p->exp;
++p->val;
} while (r);
/* Convert to the balanced form. */
while (p < &digit[SZD]) {
if (r)
++p->val;
r = p->val > 1;
if (r)
p->val -= 3;
p->val *= inv;
++p;
}
/* Restore the sign. */
src *= inv;
/* Show the result. */
for (p = &digit[0]; p < &digit[SZD]; ++p) {
r = p->pos;
if (p->val)
r += SZP;
if (p->val < 0)
r += SZP;
strncpy(res + p->pos,
pat + r,
p->len);
}
printf("%s", res);
return 0;
}