-
Notifications
You must be signed in to change notification settings - Fork 1
/
abacus.c
233 lines (191 loc) · 3.87 KB
/
abacus.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* abacus program
*
* prints abacus output in screen
*
* eg:
* ./a.out 33.442
*
* 33.442:
* -----------------
* 00000000000000000
*
* --+--+--+--+--+--
* 00000
* 00000000000000000
* 00000000000 00000
* 0000000 00000000
* 000000000 000000
* -----------------
*
*/
#include <stdio.h>
#define BEAD_DIGITS 17
#define BEAD_GROUP 3
#define DECIMAL_START 9
#define BEAD "0"
#define NO_BEAD " "
#define HEAVEN_VALUE 5
/* bead set position */
enum {
BEAD_BIT_HEAVEN0 = 0x40,
BEAD_BIT_HEAVEN1 = 0x20,
BEAD_BIT_EARTH0 = 0x10,
BEAD_BIT_EARTH1 = 0x08,
BEAD_BIT_EARTH2 = 0x04,
BEAD_BIT_EARTH3 = 0x02,
BEAD_BIT_EARTH4 = 0x01,
};
/* bead bits */
enum {
BEAD_HEAVEN0 = 0x40, /* <5 - 10 00000 */
BEAD_HEAVEN1 = 0x20, /* >5 - 01 00000 */
BEAD_EARTH0 = 0x0F, /* 0 - 00 01111 */
BEAD_EARTH1 = 0x17, /* 1 - 00 10111 */
BEAD_EARTH2 = 0x1B, /* 2 - 00 11011 */
BEAD_EARTH3 = 0x1D, /* 3 - 00 11101 */
BEAD_EARTH4 = 0x1E /* 4 - 00 11110 */
};
/* bead arrangement for digit */
char digit_bit[] = {
/* 0 */ BEAD_HEAVEN0 | BEAD_EARTH0,
/* 1 */ BEAD_HEAVEN0 | BEAD_EARTH1,
/* 2 */ BEAD_HEAVEN0 | BEAD_EARTH2,
/* 3 */ BEAD_HEAVEN0 | BEAD_EARTH3,
/* 4 */ BEAD_HEAVEN0 | BEAD_EARTH4,
/* 5 */ BEAD_HEAVEN1 | BEAD_EARTH0,
/* 6 */ BEAD_HEAVEN1 | BEAD_EARTH1,
/* 7 */ BEAD_HEAVEN1 | BEAD_EARTH2,
/* 8 */ BEAD_HEAVEN1 | BEAD_EARTH3,
/* 9 */ BEAD_HEAVEN1 | BEAD_EARTH4
};
/* draws ------ */
void draw_line()
{
int i;
for (i = 0; i < BEAD_DIGITS; i++)
printf ("-");
printf ("\r\n");
}
/* draws ---*--- seperator */
void draw_seperator()
{
int i, j;
for (i = 0; i < (BEAD_DIGITS/BEAD_GROUP); i++)
{
for (j = 1; j < BEAD_GROUP; j++)
{
printf ("-");
}
printf ("+");
}
for (j = 1; j < BEAD_GROUP; j++)
{
printf ("-");
}
printf ("\r\n");
}
/* draw single row beads */
void draw_beads(int set_bit, char *bead_bits)
{
int i;
for (i = 0; i < BEAD_DIGITS; i++)
{
printf ((bead_bits[i] & set_bit)?BEAD:NO_BEAD);
}
printf ("\r\n");
}
/* converts input number into abacus bead representation */
int set_bead_bits(char *bead_bits, char *num)
{
#define NO_DEC_POINT -1
int i, j, len, num_start_pos;
int dec_point = NO_DEC_POINT;
/* verify if it is valid number */
for (len = 0; num[len] != '\0'; len++)
{
if (num[len] == '.')
{
if (dec_point != NO_DEC_POINT)
{
return -1;
}
dec_point = len;
}
else if (!(num[len] >= '0' && num[len] <= '9'))
{
return -1;
}
}
/* input number cant fit in the abacus */
if (len > BEAD_DIGITS)
{
return -1;
}
/* reset the beads */
for (i = 0; i < BEAD_DIGITS; i++)
{
bead_bits[i] = digit_bit[0];
}
num_start_pos = (dec_point == NO_DEC_POINT) ?
DECIMAL_START - len :
DECIMAL_START - dec_point;
/* currently we place the decimal abacus middle */
if (num_start_pos < 0)
{
return -1;
}
j = 0;
i = num_start_pos;
for (j = 0; j < len; j++)
{
/* skip . */
if (num[j] == '.')
continue;
bead_bits[i++] = digit_bit[num[j] & 0x0F];
}
return 0;
}
/* draws abacus */
int draw_abacus(char *num)
{
int i;
char bead_bits[BEAD_DIGITS];
printf ("\r\n\n %s:\r\n", num);
if (set_bead_bits(bead_bits, num) != 0)
{
printf ("\r Invalid Input:%s\r\n", num);
return -1;
}
/* Draw heading */
draw_line();
/* Draw heaven */
draw_beads (BEAD_BIT_HEAVEN0, bead_bits);
draw_beads (BEAD_BIT_HEAVEN1, bead_bits);
/* Draw seperator */
draw_seperator();
/* Draw earth */
draw_beads (BEAD_BIT_EARTH0, bead_bits);
draw_beads (BEAD_BIT_EARTH1, bead_bits);
draw_beads (BEAD_BIT_EARTH2, bead_bits);
draw_beads (BEAD_BIT_EARTH3, bead_bits);
draw_beads (BEAD_BIT_EARTH4, bead_bits);
/* Draw footer */
draw_line();
return 0;
}
/* start of main pgm */
int main(int argc, char *argv[])
{
int i;
if (argc <= 1)
{
printf ("\r Usage: %s <num1> <num2> .. <num>\r\n", argv[0]);
return -1;
}
for (i = 1; i < argc; i++)
{
draw_abacus(argv[i]);
}
return 0;
}