Miquel_Fuster

Convertir un numero de [1, 100] a cadena. Sin uso de condicionales.

Jan 26th, 2022 (edited)
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef void (*apf[])(unsigned char, char *);
  5.  
  6. void dummy(unsigned char num, char *cadena) {
  7.     (void)num;
  8.     (void)cadena;
  9. }
  10.  
  11. void unidades(unsigned char num, char *cadena) {
  12.     char *numero[] = {
  13.         "", "uno", "dos", "tres", "cuatro", "cinco",
  14.         "seis", "siete", "ocho", "nueve"
  15.     };
  16.     strcat(cadena, numero[num%10]);
  17. }
  18.  
  19. void decena(unsigned char num, char *cadena) {
  20.     char *numero[] = {
  21.         "diez", "once", "doce", "trece", "catorce", "quince",
  22.         "dieciseis", "diecisiete", "dieciocho", "diecinueve"
  23.     };
  24.     strcpy(cadena, numero[num%10]);
  25. }
  26.  
  27. void conector_veintena(unsigned char num, char *cadena) {
  28.     char *conector[] = {
  29.         "", "\bi", "\bi", "\bi", "\bi", "\bi",
  30.         "\bi", "\bi", "\bi", "\bi"
  31.     };
  32.     strcat(cadena, conector[num%10]);
  33.     unidades(num, cadena);
  34. }
  35.  
  36. void conector_xtena(unsigned char num, char *cadena) {
  37.     char *conector[] = {
  38.         "", " y ",  " y ", " y ", " y ",
  39.         " y ", " y ", " y ", " y ", " y "
  40.     };
  41.     strcat(cadena, conector[num%10]);
  42.     unidades(num, cadena);
  43. }
  44.  
  45. void conector(unsigned char num, char *cadena) {
  46.     (apf) {
  47.         dummy, dummy, conector_veintena, conector_xtena, conector_xtena, conector_xtena,
  48.         conector_xtena, conector_xtena, conector_xtena, conector_xtena
  49.     }[num/10](num, cadena);
  50. }
  51.  
  52. void xtenas(unsigned char num, char *cadena) {
  53.     char *numero[] = {
  54.         "", "", "veinte", "treinta", "cuarenta", "cincuenta",
  55.         "sesenta", "setenta", "ochenta", "noventa"
  56.     };
  57.     strcpy(cadena, numero[num/10]);
  58.     conector(num, cadena);
  59. }
  60.  
  61. void centena_0(unsigned char num, char *cadena) {
  62.     (apf) {
  63.         unidades, decena, xtenas, xtenas, xtenas,
  64.         xtenas, xtenas, xtenas, xtenas, xtenas
  65.     }[num/10](num, cadena);
  66. }
  67.  
  68. void centena_10(unsigned char num, char *cadena) {
  69.     char *numero[] = {
  70.         "cien", "", "", "", "",
  71.         "", "", "", "", ""
  72.     };
  73.     strcpy(cadena, numero[num%10]);
  74. }
  75.  
  76. void centena_1(unsigned char num, char *cadena) {
  77.     (apf) {
  78.         centena_10, dummy, dummy, dummy, dummy,
  79.         dummy, dummy, dummy, dummy, dummy
  80.     }[num/10](num, cadena);
  81. }
  82.  
  83. char* num2str(unsigned char num) {
  84.     static char numero[25];
  85.     numero[0] = '\0';
  86.     (apf){centena_0, centena_1, dummy}[num/100](num, numero);
  87.     return numero;
  88. }
  89.  
  90. int main() {
  91.     for(int i=1; i<=100; ++i)
  92.         printf("%s\n", num2str(i));
  93. }
Add Comment
Please, Sign In to add comment