Salut les C,

Je vous mets a disposition ma dernière humble création: hobdcalc.

IL s'agit d'un ensemble de fonctions permettant:

-) La Conversion:

-) convertir depuis l'entier vers les base 2, 8 et 16. Le résultat est un string représentant la valeur (maximal signed long long) sous forme:
-> binaire.
-> octale.
-> hexadécimale.

-) Convertir depuis un float vers les base 2, 8 et 16. Le résultat est un string représentant la valeur (maximal signed long double, précision maximale double (%.15Lf) ) sous forme:
-> binaire.
-> octale.
-> hexadécimale.

-) Le calcul direct d'entiers (arguments donner sous formes de strings (binaire, octal ou hexadécimale).
-) Addition.
-) Soustraction.
-) Multiplications.
-) Division.
Le résultat étant de type long long sauf pour la division (long double).

-) Le calcul direct de float (arguments donner sous formes de strings (binaire, octal ou hexadécimale).
-) Addition.
-) Soustraction.
-) Multiplications.
-) Division.
Le résultat étant de type long double.

Le fichiers hobdcalc.c contient une fonction main() et des fonctions de test vous permettant de vous familiariser avec les fonctions et de comprendre comment les utiliser.

Pour vous mettre l'eau a la bouche voici le contenus du fichiers hobdcalc.c
(vous remarquerez grâce aux include, la hiérarchisation de l'archive avec un dossier-fichier a chaque effet).

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
 
/*
 * hobdcalc 
 * Copyright (C) 2014 Bruggemann Eddie.
 * 
 * This file is part of hobdcalc.
 * hobdcalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * hobdcalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with hobdcalc. If not, see <http://www.gnu.org/licenses/>
 * 
 ************************************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <tgmath.h>
#include <ctype.h>
#include <limits.h>
#include <inttypes.h>
#include <stdbool.h>
#include <ctype.h>
#include <errno.h>
#include <sys/time.h>
 
#include "conversion/utils/conversion_utils.c"
 
#include "conversion/int_hex/calc_int_hex.c"
#include "conversion/int_bin/calc_int_bin.c"
#include "conversion/int_oct/calc_int_oct.c" 
 
 
#include "conversion/float_to_bin/float_bin.c"
#include "conversion/float_to_oct/float_oct.c"
#include "conversion/float_to_hex/float_hex.c"
 
#include "operations/int_result/calc_hex_int_operations.c"
#include "operations/int_result/calc_oct_int_operations.c"
#include "operations/int_result/calc_bin_int_operations.c"
 
#include "operations/float_result/calc_hex_float_operations.c"
#include "operations/float_result/calc_oct_float_operations.c"
#include "operations/float_result/calc_bin_float_operations.c"
 
 
/* hobdcalc can compute in the 
 * -) binar
 * -) octal
 * -) hexadecimal
 * bases without problems.
 * 
 * Arithmetics functions take the 2 operand as string
 * 
 * whose you can get with the conversion functions who take as first argument an value 
 * and as second a pointer to a buffer for containing the result (size 128).
 * 
 * The reverse functions take a string as argument and return the [integer|float] result.
 * 
 * The limits values are for:
 * integer: long long [-9223372036854775808 - 9223372036854775807].
 * floats : long double 15 digits precision.
 * 
 * You must link the type generic math file with:
 * $ gcc hobdcalc.c -lm
 * to compil the code.
 * 
 * You can use the test function:
 * for understanding the code. 
 * 
 * Note: I don't care about strtold() or strtoll() and the %x %o %a placeholders, only raw computing, because i made
 *       bad experience with it during developpment and testing.
 * 
 *       I hope this little calculator functions will be usefull for you.     
 * ******************************************************************************************************************/
 
void float_operations_test(void) ;
 
void integer_operations_test(void) ;
 
int main() {
 
  /** integer_operations_test() ; test integer operation assertion function */
  /** float_operations_test()   ; test float operation assertion function */
 
}
 
void float_operations_test(void) {
  long double op1,op2 ;
  struct timeval tv ;
  char *op1_str=malloc(128) ;
  char *op2_str=malloc(128) ;
  int c ;
 
  for (c=0 ; c < 10000 ; c++) {
    /** Loop for assertions for float operations */
    gettimeofday(&tv,NULL) ;   /** get an random seed */
 
    srand(tv.tv_usec / 4) ;    /** setting the first operand random seed */
    op1= - (double) (rand() % (9223372036854775807/2)) / ((rand() % (9223372036854775807/2))+1) ; /** We take a negativ value. */
 
    srand(tv.tv_usec / 3) ;   /** setting the second operand random seed */
    op2= (double) (rand() % (9223372036854775807/2)) / ((rand() % (9223372036854775807/2))+1) ;   /** We take a positiv value. */
 
 
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
 
    floattobinfloat(op1,op1_str) ; /** Convert the float op1 in an binar string op1_str */
    floattobinfloat(op2,op2_str) ; /** Convert the float op2 in an binar string op2_str */
 
 
 
    #ifdef DEBUG
    /** Check if the values are correct converted */
    printf("%.15Lf == %.15Lf\n",op1,binfloattofloat(op1_str)) ;
    printf("%.15Lf == %.15Lf\n",op2,binfloattofloat(op2_str)) ;
    #endif
 
    if (c % 10 == 0) {
      printf("iteration: %d\n",c) ;
    }
    if (op1 + op2 != binfloataddbinfloat(op1_str,op2_str) ) {
      printf("assertion float bin add error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,binfloataddbinfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 - op2 != binfloatsubbinfloat(op1_str,op2_str) ) {
      printf("assertion float bin sub error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,binfloataddbinfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 * op2 != binfloatmultbinfloat(op1_str,op2_str) ) {
      printf("assertion float bin mult error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,binfloataddbinfloat(op1_str,op2_str)) ;
      break ;
    }
    if ( (long double) op1 / (long double) op2 != binfloatdivbinfloat(op1_str,op2_str) ) {
      printf("assertion float bin div error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,binfloataddbinfloat(op1_str,op2_str)) ;
      break ;
    }
    else {
      #ifdef DEBUG
      /** printing all computing results. */
      printf("  %s\n+ %s\n= %.15Lf\n\n  %s \n- %s\n= %.15Lf\n\n  %s \n* %s\n= %.15Lf\n\n  %s \n/ %s\n= %.15Lf\n\n",op1_str,op2_str,binfloataddbinfloat(op1_str,op2_str),op1_str,op2_str,binfloatsubbinfloat(op1_str,op2_str),op1_str,op2_str,binfloatmultbinfloat(op1_str,op2_str),op1_str,op2_str,binfloatdivbinfloat(op1_str,op2_str)) ;
      #endif 
    }
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    floattooctfloat(op1,op1_str) ; /** Convert the float op1 in an octal string op1_str */
    floattooctfloat(op2,op2_str) ; /** Convert the float op2 in an octal string op2_str */
 
    #ifdef DEBUG
    /** Check if the values are correct converted */
    printf("%.15Lf == %.15Lf\n",op1,octfloattofloat(op1_str)) ;
    printf("%.15Lf == %.15Lf\n",op2,octfloattofloat(op2_str)) ;
    #endif
 
    if (op1 + op2 != octfloataddoctfloat(op1_str,op2_str) ) {
      printf("assertion float oct add error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,octfloataddoctfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 - op2 != octfloatsuboctfloat(op1_str,op2_str) ) {
      printf("assertion float oct sub error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,octfloatsuboctfloat(op1_str,op2_str)) ;
      break ;
    }
    if (op1 * op2 != octfloatmultoctfloat(op1_str,op2_str) ) {
      printf("assertion float oct mult error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,octfloatmultoctfloat(op1_str,op2_str)) ;
      break ;
    }
    if ( (long double) op1 / (long double) op2 != octfloatdivoctfloat(op1_str,op2_str) ) {
      printf("assertion float oct div error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,octfloatdivoctfloat(op1_str,op2_str)) ;
      break ;
    }
    else {
      #ifdef DEBUG
      /** printing all computing results. */
      printf("  %s\n+ %s\n= %.15Lf\n\n  %s \n- %s\n= %.15Lf\n\n  %s \n* %s\n= %.15Lf\n\n  %s \n/ %s\n= %.15Lf\n\n",op1_str,op2_str,octfloataddoctfloat(op1_str,op2_str),op1_str,op2_str,octfloatsuboctfloat(op1_str,op2_str),op1_str,op2_str,octfloatmultoctfloat(op1_str,op2_str),op1_str,op2_str,octfloatdivoctfloat(op1_str,op2_str)) ;
      #endif 
    }
 
 
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    floattohexfloat(op1,op1_str) ; /** Convert the float op1 in an hexadecimal string op1_str */
    floattohexfloat(op2,op2_str) ; /** Convert the float op2 in an hexadecimal string op2_str */
    op1_str[128]='\0' ;
    op2_str[128]='\0' ;
 
 
    #ifdef DEBUG
    /** Check if the values are correct converted */
    printf("%.15Lf == %.15Lf\n",op1,hexfloattofloat(op1_str)) ;
    printf("%.15Lf == %.15Lf\n",op2,hexfloattofloat(op2_str)) ;
    #endif
 
    if (op1 + op2 != hexfloataddhexfloat(op1_str,op2_str) ) {
      printf("assertion float hex add error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,hexfloataddhexfloat(op1_str,op2_str)) ;
      break ;
    }
 
    if (op1 - op2 != hexfloatsubhexfloat(op1_str,op2_str) ) {
      printf("assertion float hex sub error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,hexfloataddhexfloat(op1_str,op2_str)) ;
      break ;
    }
 
    if (op1 * op2 != hexfloatmulthexfloat(op1_str,op2_str) ) {
      printf("assertion float hex mult error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,hexfloataddhexfloat(op1_str,op2_str)) ;
      break ;
    }
    if ( (long double) op1 / (long double) op2 != hexfloatdivhexfloat(op1_str,op2_str) ) {
      printf("assertion float hex div error by iteration %d:\nop1: %.15Lf == %s\nop2: %.15Lf == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,op1+op2,hexfloataddhexfloat(op1_str,op2_str)) ;
      break ;
    }
    else {
      #ifdef DEBUG
      /** printing all computing results. */
      printf("  %s\n+ %s\n= %.15Lf\n\n  %s \n- %s\n= %.15Lf\n\n  %s \n* %s\n= %.15Lf\n\n  %s \n/ %s\n= %.15Lf\n\n",op1_str,op2_str,hexfloataddhexfloat(op1_str,op2_str),op1_str,op2_str,hexfloatsubhexfloat(op1_str,op2_str),op1_str,op2_str,hexfloatmulthexfloat(op1_str,op2_str),op1_str,op2_str,hexfloatdivhexfloat(op1_str,op2_str)) ;
      #endif 
    }
 
 
 
  }
  free(op1_str) ;
  free(op2_str) ;
 
  return  ;
}
 
void integer_operations_test(void) {
  int c ;
  long long op1,op2 ;
  char *op1_str, *op2_str ;
  op1_str=malloc(128) ;
 
  op2_str=malloc(128) ;
 
  struct timeval tv ;
 
  for (c=0 ; c < 10000 ; c++) {
    /** Loop for assertions for integer operations */
    gettimeofday(&tv,NULL) ;   /** get an random seed */
 
    srand(tv.tv_usec /4 ) ;    /** setting the first operand random seed */
    op1= ((long long ) rand() % LLONG_MAX) ; /** We take a positiv value. */
 
    srand(tv.tv_usec / 3 ) ;   /** setting the second operand random seed */
    op2=  -  ((long long) rand() % LLONG_MAX) ; /** We take a negativ value. */
 
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    inttobin(op1,op1_str) ; /** Convert the integer op1 in an binar string op1_str */
    inttobin(op2,op2_str) ; /** Convert the integer op2 in an binar string op2_str */
 
    #ifdef DEBUG
    /** Check if the values are correct converted */
    printf("%Li == %Li\n",op1,bintoint(op1_str)) ; 
    printf("%Li == %Li\n",op2,bintoint(op2_str)) ; 
    #endif
 
    if (! (op1 + op2 == binaddbin(op1_str,op2_str) ) ) {
      printf("assertion integer bin add error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,binaddbin(op1_str,op2_str)) ;
      break ;
    }
    if (! (op1 - op2 == binsubbin(op1_str,op2_str)) ) {
      printf("assertion integer bin sub error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,binsubbin(op1_str,op2_str)) ;
      break ;
    }
    if (! (op1 * op2 == binmultbin(op1_str,op2_str)) ) { 
      printf("assertion integer bin mult error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,binmultbin(op1_str,op2_str)) ;
      break ;
    }
    if (! ((long double) op1 / (long double) op2 == bindivbin(op1_str,op2_str)) ) {
      printf("assertion integer bin div error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,(long double)op1+op2,bindivbin(op1_str,op2_str)) ;
      break ;
    }
 
 
 
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ;
    inttooct(op1,op1_str) ; /** Convert the integer op1 in an octal string op1_str */ 
    inttooct(op2,op2_str) ; /** Convert the integer op2 in an octal string op2_str */
 
    #ifdef DEBUG
    /** Check if the values are correct converted */
    printf("%Li == %Li\n",op1,octtoint(op1_str)) ;
    printf("%Li == %Li\n",op2,octtoint(op2_str)) ;
    #endif
 
    printf("%Li == %Li\n%Li == %Li\n",op1,octtoint(op1_str),op2,octtoint(op2_str)) ;
 
    if (! (op1 + op2 == octaddoct(op1_str,op2_str) ) ) {
	printf("assertion integer oct add error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,octaddoct(op1_str,op2_str)) ;
	break ;
      }
    if (! (op1 - op2 == octsuboct(op1_str,op2_str)) ) {
      printf("assertion integer oct sub error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,octsuboct(op1_str,op2_str)) ;
      break ;
    }
    if (! (op1 * op2 == octmultoct(op1_str,op2_str)) ) { 
      printf("assertion integer oct mult error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,octmultoct(op1_str,op2_str)) ;
      break ;
    }
    if (! ((long double) op1 / (long double) op2 == octdivoct(op1_str,op2_str)) ) {
      printf("assertion integer oct div error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,(long double)op1+op2,octdivoct(op1_str,op2_str)) ;
      break ;
    }
 
 
 
    memset(op1_str,'\0',128) ;
    memset(op2_str,'\0',128) ; 
    inttohex(op1,op1_str) ; /** Convert the integer op1 in an hexadecimal string op1_str */
    inttohex(op2,op2_str) ; /** Convert the integer op2 in an hexadecimal string op2_str */
 
    #ifdef DEBUG
    /** Check if the values are correct converted */
    printf("%Li == %Li\n",op1,hextoint(op1_str)) ;
    printf("%Li == %Li\n",op2,hextoint(op2_str)) ;
    #endif
 
    if (! (op1 + op2 == hexaddhex(op1_str,op2_str) ) ) {
	printf("assertion integer hex add error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,hexaddhex(op1_str,op2_str)) ;
	break ;
    }
    if (! (op1 - op2 == hexsubhex(op1_str,op2_str)) ) {
      printf("assertion integer hex sub error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,hexsubhex(op1_str,op2_str)) ;
      break ;
    }
    if (! (op1 * op2 == hexmulthex(op1_str,op2_str)) ) { 
      printf("assertion integer hex mult error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %Li\nerror: %Li\n",c,op1,op1_str,op2,op2_str,op1+op2,hexmulthex(op1_str,op2_str)) ;
      break ;
    }
    if (! ((long double) op1 / (long double) op2 == hexdivhex(op1_str,op2_str)) ) {
      printf("assertion integer hex div error by iteration %d:\nop1: %Li == %s\nop2: %Li == %s\nresult: %.15Lf\nerror: %.15Lf\n",c,op1,op1_str,op2,op2_str,(long double)op1+op2,hexdivhex(op1_str,op2_str)) ;
      break ;
    }
 
 
 
  }
 
  free(op1_str) ;
  free(op2_str) ;
  return ;
 
}
Merci pour vos commentaires en espérant que ça vous soit utile.
Désolé pour la limitation a 8 bytes je n'ai pus faire mieux.