Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <string.h>
  5.  
  6. struct Libro {
  7. char titulo[100];
  8. char autor[100];
  9. int paginas;
  10.  
  11. struct Libro* siguiente;
  12. };
  13.  
  14. void imprimirLibro(struct Libro* lib) {
  15. printf("Titulo: '%s'\n", lib->titulo);
  16. printf("Nombre '%s'\n", lib->autor);
  17. printf("Paginas '%i'\n", lib->paginas);
  18. }
  19.  
  20. void escribirLibro(struct Libro* lib, FILE* f) {
  21. fputs((*lib).titulo, f);
  22. fputs(",", f);
  23. fputs(lib->autor, f);
  24. fputs(",", f);
  25. char paginas[17];
  26. itoa(lib->paginas, &paginas, /*base*/10);
  27. fputs(paginas, f);
  28. fputs("\n", f);
  29. }
  30.  
  31. int buscarComa(char* cadena, int inicio, int tamano) {
  32. for(int i = inicio; i < tamano; ++i) {
  33. if(cadena[i] == ',') {
  34. return i;
  35. }
  36. }
  37. return -1;
  38. }
  39.  
  40. struct Libro* crearLibro() {
  41.  
  42. struct Libro* lib = malloc(sizeof(struct Libro));
  43. lib->titulo[0] = '\0';
  44. lib->autor[0] = '\0';
  45. lib->paginas = 0;
  46. lib->siguiente = NULL;
  47.  
  48. return lib;
  49. };
  50.  
  51. struct Libro* deserealizarLibro(char* cadena, int tamano) {
  52.  
  53. struct Libro* lib = crearLibro();
  54.  
  55. assert(lib);
  56.  
  57. int coma1 = buscarComa(cadena, 0, tamano);
  58. strncpy(lib->titulo, cadena, coma1);
  59. lib->titulo[coma1] = '\0';
  60.  
  61. int coma2 = buscarComa(cadena, coma1+1, tamano);
  62. strncpy(lib->autor, cadena+coma1+1, coma2-coma1-1);
  63. lib->autor[coma2-coma1-1] = '\0';
  64.  
  65. char paginas[6];
  66. strncpy(paginas, cadena+coma2+1, tamano-coma2);
  67. paginas[tamano-coma2] = '\0';
  68. lib->paginas = atoi(paginas);
  69.  
  70. return lib;
  71. }
  72.  
  73. void imprimirLista(struct Libro* lista) {
  74. struct Libro* actual = lista;
  75. while(actual) {
  76. imprimirLibro(actual);
  77. actual = actual->siguiente;
  78. }
  79. }
  80.  
  81. struct Libro* agregarLibro() {
  82. struct Libro* lib = crearLibro();
  83. printf("Titulo:");
  84. gets(lib->titulo);
  85. printf("Autor:");
  86. gets(lib->autor);
  87. printf("Paginas:");
  88. scanf("%i", &lib->paginas);
  89.  
  90. char enter;
  91. scanf("%c", &enter);
  92. return lib;
  93. }
  94.  
  95. void agregarALista(struct Libro** cabeza, struct Libro* nuevo) {
  96. if(!cabeza) {
  97. cabeza = nuevo;
  98. } else {
  99. struct Libro* actual = *cabeza;
  100. while(actual->siguiente) {
  101. actual = actual->siguiente;
  102. }
  103. actual->siguiente = nuevo;
  104. }
  105. }
  106.  
  107. int main()
  108. {
  109. //struct Libro l1;
  110. //strcpy(l1.autor, "Tolstoy");
  111. //strcpy(l1.titulo, "La guerra y la paz");
  112. //l1.paginas = 1000;
  113.  
  114. FILE* archivo = fopen("helloworld.txt", "r");
  115.  
  116. if(!archivo) {
  117. printf("El archivo no se pudo abrir");
  118. return 1;
  119. }
  120.  
  121. int tamano;
  122. char linea[10000];
  123. int len;
  124.  
  125. struct Libro* lista = NULL;
  126.  
  127. while( fgets(linea, 10000, archivo) ) {
  128. struct Libro* lib = deserealizarLibro(linea, strlen(linea));
  129.  
  130. if(!lista) {
  131. lista = lib;
  132. } else {
  133. struct Libro* actual = lista;
  134. while(actual->siguiente) {
  135. actual = actual->siguiente;
  136. }
  137. actual->siguiente = lib;
  138. }
  139. }
  140. fclose(archivo);
  141.  
  142. int opcion = -1;
  143. do {
  144. printf("0) Salir, 1) Mostrar, 2) Agregar, 3) Quitar \n Opcion:");
  145. scanf("%d", &opcion);
  146. char enter;
  147. scanf("%c", &enter);
  148. switch(opcion) {
  149. case 1:
  150. imprimirLista(lista);
  151. break;
  152. case 2: {
  153. struct Libro* lib = agregarLibro();
  154. agregarALista(&lista, lib);
  155. }
  156. default:
  157. break;
  158. }
  159. } while(opcion != 0);
  160.  
  161. //escribirLibro(&l1, archivo);
  162.  
  163. return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement