-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.c
204 lines (204 loc) · 4.25 KB
/
number.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
#include <stdbool.h>
typedef enum NumType{
NUM_INT,
NUM_DOUBLE
} NumType;
typedef struct Number{
NumType type; // 0 -> int 1 -> double
union{
double double_val;
int int_val;
};
} Number;
Number intNumber(int a){
Number r;
r.type=NUM_INT;
r.int_val=a;
return r;
}
Number doubleNumber(double a){
Number r;
r.type=NUM_DOUBLE;
r.double_val=a;
return r;
}
Number addNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return intNumber(a.int_val+b.int_val);
}else{ //NUM_DOUBLE
return doubleNumber(a.int_val+b.double_val);
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return doubleNumber(a.double_val+b.int_val);
}else{ //NUM_DOUBLE
return doubleNumber(a.double_val+b.double_val);
}
}
}
Number subNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return intNumber(a.int_val-b.int_val);
}else{ //NUM_DOUBLE
return doubleNumber(a.int_val-b.double_val);
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return doubleNumber(a.double_val-b.int_val);
}else{ //NUM_DOUBLE
return doubleNumber(a.double_val-b.double_val);
}
}
}
Number mulNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return intNumber(a.int_val*b.int_val);
}else{ //NUM_DOUBLE
return doubleNumber(a.int_val*b.double_val);
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return doubleNumber(a.double_val*b.int_val);
}else{ //NUM_DOUBLE
return doubleNumber(a.double_val*b.double_val);
}
}
}
Number divNumber(Number a,Number b,const char ** msg){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
Number c;
if (b.int_val==0){
msg[0]="divided by zero";
}else{
c.type=NUM_INT;
c.int_val=a.int_val/b.int_val;
}
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=NUM_DOUBLE;
c.double_val=a.int_val/b.double_val;
return c;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
Number c;
if (b.int_val==0){
msg[0]="divided by zero";
}else{
c.type=NUM_DOUBLE;
c.double_val=a.double_val/b.int_val;
}
return c;
}else{ //NUM_DOUBLE
Number c;
c.type=NUM_DOUBLE;
c.double_val=a.double_val/b.double_val;
return c;
}
}
}
Number modNumber(Number a,Number b,const char ** msg){
Number c;
if (a.type!=NUM_INT || b.type!=NUM_INT){
msg[0]="cannot use non-integer value in mod";
}else{
c.type=NUM_INT;
c.int_val=a.int_val%b.int_val;
}
return c;
}
bool leNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return a.int_val<b.int_val;
}else{ //NUM_DOUBLE
return a.int_val<b.double_val;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return a.double_val<b.int_val;
}else{ //NUM_DOUBLE
return a.double_val<b.double_val;
}
}
}
bool geNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return a.int_val>b.int_val;
}else{ //NUM_DOUBLE
return a.int_val>b.double_val;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return a.double_val>b.int_val;
}else{ //NUM_DOUBLE
return a.double_val>b.double_val;
}
}
}
bool nleNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return a.int_val>=b.int_val;
}else{ //NUM_DOUBLE
return a.int_val>=b.double_val;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return a.double_val>=b.int_val;
}else{ //NUM_DOUBLE
return a.double_val>=b.double_val;
}
}
}
bool ngeNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return a.int_val<=b.int_val;
}else{ //NUM_DOUBLE
return a.int_val<=b.double_val;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return a.double_val<=b.int_val;
}else{ //NUM_DOUBLE
return a.double_val<=b.double_val;
}
}
}
bool eqNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return a.int_val==b.int_val;
}else{ //NUM_DOUBLE
return a.int_val==b.double_val;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return a.double_val==b.int_val;
}else{ //NUM_DOUBLE
return a.double_val==b.double_val;
}
}
}
bool neqNumber(Number a,Number b){
if (a.type==NUM_INT){
if (b.type==NUM_INT){
return a.int_val!=b.int_val;
}else{ //NUM_DOUBLE
return a.int_val!=b.double_val;
}
}else{ //NUM_DOUBLE
if (b.type==NUM_INT){
return a.double_val!=b.int_val;
}else{ //NUM_DOUBLE
return a.double_val!=b.double_val;
}
}
}