Advertisement
tomasaccini

Untitled

Jul 15th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. /*
  2. Escriba un programa ISO C que procese el archivo de números datos.txt sobre
  3. sí mismo. El procesamiento consite en convertir los números encontrados
  4. (de 1 o más cifras decimales) a octal.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <math.h>
  12.  
  13. int decimal_a_octal(int decimal) {
  14.     int octal = 0;
  15.     int contador = 1;
  16.     while (decimal >= 1) {
  17.         int resto = (decimal % 8);
  18.         octal += resto * contador;
  19.         decimal = decimal / 8;
  20.         contador *= 10;
  21.     }
  22.     return octal;
  23. }
  24.  
  25. int obtener_cifras_de_un_numero(int n) {
  26.     int cifras = 0;
  27.     while (n >= 1) {
  28.         cifras++;
  29.         n = n / 10;
  30.     }
  31.     return cifras;
  32. }
  33.  
  34. int leer_reversa(FILE* f, int* seek_lectura) {
  35.     fseek(f, *seek_lectura, SEEK_SET);
  36.     char c = fgetc(f);
  37.     if (ftell(f) == 1) {
  38.         *seek_lectura = -1;
  39.     } else {
  40.         fseek(f, -2, SEEK_CUR);
  41.         *seek_lectura = ftell(f);
  42.     }
  43.     if (c == ' ') return -1;
  44.     if (c >= 48 && c <= 57)
  45.         return (int)(c - 48);
  46.     return -2;
  47. }
  48.  
  49. void escribir_valor_reversa(FILE* f, int* seek_escritura, int valor) {
  50.     int cifras = obtener_cifras_de_un_numero(valor);
  51.     fseek(f, *seek_escritura - cifras, SEEK_SET);
  52.     fprintf(f, "%d", valor);
  53.     fseek(f, *seek_escritura - cifras - 1, SEEK_SET);
  54.     *seek_escritura = ftell(f);
  55. }
  56.  
  57. void escribir_espacio_reversa(FILE* f, int* seek_escritura) {
  58.     fseek(f, *seek_escritura, SEEK_SET);
  59.     fputc(' ', f);
  60.     fseek(f, -2, SEEK_CUR);
  61.     *seek_escritura = ftell(f);
  62. }
  63.  
  64. int main() {
  65.     FILE* f = fopen("datos.txt", "r");
  66.     if (!f) return -1;
  67.     int new_size = 0;
  68.     int decimal_actual = 0;
  69.     char c;
  70.  
  71.     int seek_lectura = 0;
  72.     int seek_escritura = 0;
  73.     while ( (c = fgetc(f)) != EOF ) {
  74.         if (c == ' ') {
  75.             int octal = decimal_a_octal(decimal_actual);
  76.             new_size += obtener_cifras_de_un_numero(octal) + 1;
  77.             decimal_actual = 0;
  78.         } else if (c >= 48 && c <= 57){
  79.             decimal_actual *= 10;
  80.             decimal_actual += (int)(c - 48);
  81.         }
  82.         seek_lectura++;
  83.     }
  84.     seek_lectura -= 2;
  85.     int octal = decimal_a_octal(decimal_actual);
  86.     new_size += obtener_cifras_de_un_numero(octal);
  87.     seek_escritura = new_size;
  88.     fclose(f);
  89.     truncate("datos.txt", new_size);
  90.  
  91.     f = fopen("datos.txt", "r+");
  92.     if (!f) return -1;
  93.     decimal_actual = 0;
  94.     printf("DEBUG: antes del while seek_lectura : %d\n", seek_lectura);
  95.     while ( seek_lectura != -1 ) {
  96.         c = leer_reversa(f, &seek_lectura);
  97.         printf("DEBUG: seek_lectura : %d - c: %d\n", seek_lectura, (int)c);
  98.         if (c == -1) {
  99.             // espacio
  100.             printf("DEBUG: Decimal: %d, octal: %d\n", decimal_actual, decimal_a_octal(decimal_actual));
  101.             escribir_valor_reversa(f, &seek_escritura, decimal_a_octal(decimal_actual));
  102.             escribir_espacio_reversa(f, &seek_escritura);
  103.             decimal_actual = 0;
  104.         } else if (c >= 0 && c <= 9) {
  105.             decimal_actual += c * (pow(10, obtener_cifras_de_un_numero(decimal_actual)));
  106.         }
  107.     }
  108.     printf("DEBUG: Decimal: %d, octal: %d\n", decimal_actual, decimal_a_octal(decimal_actual));
  109.     escribir_valor_reversa(f, &seek_escritura, decimal_a_octal(decimal_actual));
  110.     fclose(f);
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement