training

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

n2w.c (4385B)


      1 /* Exercise 8.38 */
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <ctype.h>
      6 
      7 #define SIZE 10
      8 
      9 void printw(double n);
     10 void pnumz(int num, int kind);
     11 
     12 double tonum(char *s);
     13 
     14 int main(void)
     15 {
     16    char number[SIZE] = { 0 };
     17    double n;
     18 
     19    printf("Give me a number: ");
     20    gets(number);
     21    while( (int)strlen(number) > SIZE ) {
     22       printf("Error: too many numbers\n");
     23 
     24       printf("Give me a number: ");
     25       gets(number);
     26    }
     27 
     28    n = tonum(number);
     29 
     30    printw(n);
     31 
     32    printf("\n");
     33 
     34    return 0;
     35 } /* E0F main */
     36 
     37 /* convert from "1,234.56" format to 1234.56 */
     38 double tonum(char *s)
     39 {
     40    int i, num[SIZE] = { 0 };
     41    double n = 0, x = 1;
     42    char string[SIZE], *tokp;
     43 
     44    strcpy(string, s);
     45 
     46    tokp = strtok(string, ",");
     47    while( tokp != NULL ) {
     48       for(i = 0; tokp[i] != '\0' && !ispunct((int)tokp[i]); i++) {
     49 	 num[(int)n++] = (int)tokp[i] - '0';
     50 	 x *= 10;
     51       }
     52 
     53       tokp = strtok(NULL, ",");
     54    }
     55    x /= 10;
     56    n = 0;
     57 
     58    for(i = 0; (int)x && i < SIZE * 2; i++) {
     59       n += num[i] * x;
     60       x /= 10;
     61    }
     62 
     63    x = 1;
     64 
     65    strtok(s, "."); /* Before '.' */
     66    tokp = strtok(NULL, "."); /* After the '.' */
     67    if(tokp != NULL) {
     68       for(i = 0; tokp[i] != '\0'; i++) {
     69 	 x *= 0.1;
     70          n += (tokp[i] - '0') * x;
     71       }
     72    }
     73 
     74    return n;
     75 
     76 } /* eof tonum() */
     77 
     78 /* print a number in letters (1) */
     79 void printw(double n)
     80 {
     81    int k = 4, x = 1000000000, num;
     82 
     83    do {
     84       num = (int)n / x % 1000;
     85 
     86       if(num) {
     87          pnumz((int)n / x % 1000, k);
     88       }
     89 
     90       --k;
     91 
     92    } while( x /= 1000 );
     93 
     94    /* Floating numbers */
     95    if(n - (int)n)
     96       printf(" virgola %.0f", (n - (int)n) * 100); 
     97 } /* eof printw() */
     98 
     99 /* print a number in letters (2) */
    100 void pnumz(int num, int kind)
    101 {
    102    switch(num / 100 % 10) {
    103       case 2:
    104 	 printf("due");
    105 	 break;
    106       case 3:
    107 	 printf("tre");
    108 	 break;
    109       case 4:
    110 	 printf("quattro");
    111 	 break;
    112       case 5:
    113 	 printf("cinque");
    114 	 break;
    115       case 6:
    116 	 printf("sei");
    117 	 break;
    118       case 7:
    119 	 printf("sette");
    120 	 break;
    121       case 8:
    122 	 printf("otto");
    123 	 break;
    124       case 9:
    125 	 printf("nove");
    126 	 break;
    127    }
    128 
    129    if( (num / 100 % 10) )
    130       printf("cento");
    131 
    132    switch(num / 10 % 10) {
    133       case 1:
    134 	 switch(num % 10) {
    135 	    case 0:
    136 	       printf("dieci");
    137 	       break;
    138 	    case 1:
    139 	       printf("un");
    140 	       break;
    141 	    case 2:
    142 	       printf("do");
    143 	       break;
    144 	    case 3:
    145 	       printf("tre");
    146 	       break;
    147 	    case 4:
    148 	       printf("quattor");
    149 	       break;
    150 	    case 5:
    151 	       printf("quin");
    152 	       break;
    153 	    case 6:
    154 	       printf("se");
    155 	       break;
    156 	    case 7:
    157 	       printf("diciassette");
    158 	       break;
    159 	    case 8:
    160 	       printf("diciotto");
    161 	       break;
    162 	    case 9:
    163 	       printf("diciannove");
    164 	       break;
    165 	 }
    166 
    167 	 if( (num % 10) < 7 && ( num % 10 ) )
    168 	    printf("dici");
    169 
    170 	 break;
    171       case 9:
    172 	 printf("nov");
    173 	 break;
    174       case 8:
    175 	 printf("ott");
    176 	 break;
    177       case 7:
    178 	 printf("sett");
    179 	 break;
    180       case 6:
    181 	 printf("sess");
    182 	 break;
    183       case 5:
    184 	 printf("cinqu");
    185 	 break;
    186       case 4:
    187 	 printf("quar");
    188 	 break;
    189       case 3:
    190 	 printf("trenta");
    191 	 break;
    192       case 2:
    193 	 printf("venti");
    194 	 break;
    195    }
    196 
    197    if( (num / 10 % 10) >= 4 )
    198       printf("anta");
    199 
    200    if( (num % 10 == 8 && num != 8) ||
    201        ((num % 10) == 1 && (num != 1 && num / 10 % 10 != 1) ) )
    202       printf("\b");
    203 
    204    if(num / 10 % 10 != 1) { /* If is not [10,19] */
    205       switch(num % 10) {
    206          case 1:
    207 	    if (kind == 4 && num == 1)
    208 	       printf("un");
    209 	    else if(kind == 3 && num == 1)
    210 	       printf("un");
    211 	    else if(kind == 2 && num == 1)
    212 	       printf("mille");
    213 	    else
    214 	       printf("uno");
    215 	    break;
    216          case 2:
    217 	    printf("due");
    218 	    break;
    219          case 3:
    220 	    printf("tre");
    221 	    break;
    222          case 4:
    223 	    printf("quattro");
    224 	    break;
    225          case 5:
    226 	    printf("cinque");
    227 	    break;
    228          case 6:
    229 	    printf("sei");
    230 	    break;
    231          case 7:
    232 	    printf("sette");
    233 	    break;
    234          case 8:
    235 	    printf("otto");
    236 	    break;
    237          case 9:
    238 	    printf("nove");
    239 	    break;
    240       }
    241    } /* end if */
    242 
    243    switch(kind) {
    244       case 4:
    245 	 if( !(num % 100) || num % 100 > 1 )
    246 	    printf("miliardi");
    247 	 else
    248 	    printf("miliardo");
    249 	 break;
    250       case 3:
    251 	 if( !(num % 100) || num % 100 > 1 )
    252 	    printf("milioni");
    253 	 else
    254 	    printf("milione");
    255 	 break;
    256       case 2:
    257 	 if(num > 1)
    258 	    printf("mila");
    259 	 break;
    260    }
    261 } /* eof pnumz() */
    262