training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

numtab2.c (1288B)


      1 /* Esercizio 4.34 */
      2 
      3 #include <stdio.h>
      4 #include <math.h>
      5 
      6 int main()
      7 {
      8    int num, c_num;
      9    int base; /* start from base 2 (binary) */
     10    int i, j, p, check;
     11 
     12    for(num = 1; num <= 256; num++) {
     13       printf("%d%3s", num, " ");
     14 
     15       /* print equivalent in "base" */
     16       for(i = 1; i <= 4; i++) {
     17 
     18          /* dynamic base */
     19 	 if(i == 2) /* ignore base 4 */
     20 	    continue;
     21 	 base = pow(2, i);
     22 
     23          /* found the values order.. */
     24          for(j = 0; pow(base, j) <= num; j++) {
     25 	    p = pow(base, j);
     26          } /* ..and print the symbols one for one */
     27          c_num = num;
     28          for(j = p; j >= 1; j /= base) {
     29 
     30 	    if(base == 16 && c_num / j >= 10 && c_num / j <= 15) {
     31 	    
     32 	       switch(c_num / j) {
     33 		  case 10:
     34 		     printf("A");
     35 		     break;
     36 		  case 11:
     37 		     printf("B");
     38 		     break;
     39 		  case 12:
     40 		     printf("C");
     41 		     break;
     42 		  case 13:
     43 		     printf("D");
     44 		     break;
     45 		  case 14:
     46 		     printf("E");
     47 		     break;
     48 		  case 15:
     49 		     printf("F");
     50 
     51 	       } /* end switch (c_num / j) */
     52 
     53 	       continue;
     54 	    } /* end if (hex) */
     55 
     56             printf("%d", c_num / j);
     57 	    c_num %= j;
     58          } /* end for (j) */
     59 
     60 	 printf("%3s", " ");
     61       } /* end for (i) */
     62 
     63       printf("\n");
     64    } /* end for (num) */
     65 
     66    return 0;
     67 } /* E0F main */
     68