-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbcd_arithmetic.c
More file actions
174 lines (130 loc) · 3.62 KB
/
bcd_arithmetic.c
File metadata and controls
174 lines (130 loc) · 3.62 KB
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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
//
// Store an arbitray length Binary Coded Decimal number
// bcd points to an array of size n_bcd
// each array element contains 1 decimal digit
//
typedef struct big_bcd {
unsigned char *bcd;
int n_bcd;
} big_bcd_t;
void bcd_print(big_bcd_t *number);
void bcd_free(big_bcd_t *number);
big_bcd_t *bcd_from_string(char *string);
big_bcd_t *expression(char ***tokens);
big_bcd_t *term(char ***tokens);
int main(int argc, char *argv[]) {
char **tokens = argv + 1;
// tokens points in turn to each of the elements of argv
// as the expression is evaluated.
if (*tokens) {
big_bcd_t *result = expression(&tokens);
bcd_print(result);
printf("\n");
bcd_free(result);
}
return 0;
}
// DO NOT CHANGE THE CODE ABOVE HERE
big_bcd_t *bcd_add(big_bcd_t *x, big_bcd_t *y) {
// PUT YOUR CODE HERE
return NULL;
}
big_bcd_t *bcd_subtract(big_bcd_t *x, big_bcd_t *y) {
// PUT YOUR CODE HERE
return NULL;
}
big_bcd_t *bcd_multiply(big_bcd_t *x, big_bcd_t *y) {
// PUT YOUR CODE HERE
return NULL;
}
big_bcd_t *bcd_divide(big_bcd_t *x, big_bcd_t *y) {
// PUT YOUR CODE HERE
return NULL;
}
// DO NOT CHANGE THE CODE BELOW HERE
// print a big_bcd_t number
void bcd_print(big_bcd_t *number) {
// if you get an error here your bcd_arithmetic is returning an invalid big_bcd_t
assert(number->n_bcd > 0);
for (int i = number->n_bcd - 1; i >= 0; i--) {
putchar(number->bcd[i] + '0');
}
}
// DO NOT CHANGE THE CODE BELOW HERE
// free storage for big_bcd_t number
void bcd_free(big_bcd_t *number) {
// if you get an error here your bcd_arithmetic is returning an invalid big_bcd_t
// or it is calling free for the numbers it is given
free(number->bcd);
free(number);
}
// convert a string to a big_bcd_t number
big_bcd_t *bcd_from_string(char *string) {
big_bcd_t *number = malloc(sizeof *number);
assert(number);
int n_digits = strlen(string);
assert(n_digits);
number->n_bcd = n_digits;
number->bcd = malloc(n_digits * sizeof number->bcd[0]);
assert(number->bcd);
for (int i = 0; i < n_digits; i++) {
int digit = string[n_digits - i - 1];
assert(isdigit(digit));
number->bcd[i] = digit - '0';
}
return number;
}
// simple recursive descent evaluator for big_bcd_t expressions
big_bcd_t *expression(char ***tokens) {
big_bcd_t *left = term(tokens);
assert(left);
if (!**tokens|| (***tokens != '+' && ***tokens != '-')) {
return left;
}
char *operator = **tokens;
(*tokens)++;
big_bcd_t *right = expression(tokens);
assert(right);
big_bcd_t *result;
if (operator[0] == '+') {
result = bcd_add(left, right);
} else {
assert(operator[0] == '-');
result = bcd_subtract(left, right);
}
assert(result);
bcd_free(left);
bcd_free(right);
return result;
}
// evaluate a term of a big_bcd_t expression
big_bcd_t *term(char ***tokens) {
big_bcd_t *left = bcd_from_string(**tokens);
assert(left);
(*tokens)++;
if (!**tokens || (***tokens != '*' && ***tokens != '/')) {
return left;
}
char *operator = **tokens;
(*tokens)++;
big_bcd_t *right = term(tokens);
assert(right);
big_bcd_t *result;
if (operator[0] == '*') {
result = bcd_multiply(left, right);
} else {
result = bcd_divide(left, right);
}
assert(result);
bcd_free(left);
bcd_free(right);
return result;
}