Guest User

Untitled

a guest
Apr 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. typedef struct livro{
  5. char nome[10];
  6. char autor[20];
  7. char ano[5];
  8. char descricao[20];
  9. char palavrachave[10];
  10. }livro;
  11.  
  12. typedef struct sBiblioteca{
  13. livro l;
  14. struct sBiblioteca *seguinte;
  15. }*Biblioteca, Nodobiblioteca;
  16.  
  17. Biblioteca inserir (Biblioteca b, char *nome,char* ano, char* descricao, char* palavrachave, char*autor){
  18. Biblioteca novonodo = (Biblioteca) malloc(sizeof(Nodobiblioteca));
  19. livro novolivro;
  20. strcpy(novolivro.nome,nome);
  21. strcpy(novolivro.autor,autor);
  22. strcpy(novolivro.palavrachave,palavrachave);
  23. strcpy(novolivro.descricao,descricao);
  24. strcpy(novolivro.ano,ano);
  25. novonodo->l = novolivro;
  26. novonodo->seguinte = b;
  27. return novonodo;
  28. }
  29.  
  30. Biblioteca remover (Biblioteca b, char *nome){
  31. Biblioteca x,y;
  32. if (x){
  33. if(!strcmp(x->l.nome,nome)){
  34. y = x;
  35. free (y);
  36. return x->seguinte;}
  37. for(x=b;!x->seguinte;x=x->seguinte){
  38. if(!strcmp(x->seguinte->l.nome,nome)){
  39. y = x->seguinte;
  40. x->seguinte = x->seguinte->seguinte;
  41. free(y);
  42. return b;
  43. }
  44. }
  45. printf("ERRO, Livro Não Encontrado\n");
  46. return b;
  47. }
  48.  
  49. void ListarLivros (Biblioteca b){
  50. if(!b)
  51. printf("Não existem livros");
  52. else
  53. while (b){
  54. printf("%s,%s",b->l.nome, b->l.autor);
  55. b=b->seguinte;
  56. }
  57. }
  58.  
  59. void Lista_Livro (Biblioteca b, char *nome){
  60. if(!b) printf("Erro. Não existe o livro não Encontrado");
  61. if(!strcmp(b->l.nome, nome))
  62. printf("%s,%s,%s,%s,%s",b->l.nome, b->l.autor, b->l.ano, b->l.descricao, b->l.palavrachave);
  63. else
  64. Lista_Livro(b->seguinte, nome);
  65. }
  66.  
  67. Biblioteca altera (Biblioteca b, char *nome){
  68. if(b)
  69. {
  70. if(!strcmp(b->l.nome, nome))
  71. b->l = alteraLivro(b->l);
  72. else
  73. b->seguinte = altera (b->seguinte, nome);
  74. return b;
  75. }
  76. printf("Erro, não existe esse livro\n");
  77. return NULL;
  78. }
  79.  
  80. livro alteraLivro (livro l){
  81. char nome[10];
  82. char autor[20];
  83. char ano[5];
  84. char descricao[20];
  85. char palavrachave[10];
  86.  
  87. printf("Digite o nome do livro\n");
  88. scanf("%s",nome);
  89. strcpy(l.nome,nome);
  90. printf("Digite o nome do autor\n");
  91. scanf("%s",autor);
  92. strcpy(l.autor,autor);
  93. printf("Digite o ano\n");
  94. scanf("%s",ano);
  95. strcpy(l.ano,ano);
  96. printf("Digite uma descrição\n");
  97. scanf("%s",descricao);
  98. strcpy(l.descricao,descricao);
  99. printf("Digite uma palavra-chave");
  100. scanf("%s",palavrachave);
  101. strcpy(l.palavrachave,palavrachave);
  102. return l;
  103. }
  104.  
  105. int main(){
  106. int x = -1;
  107. Biblioteca b = NULL;
  108. Biblioteca z;
  109. char nome[10];
  110. char autor[20];
  111. char ano[5];
  112. char descricao[20] ;
  113. char palavrachave[10];
  114. FILE *ficheiro;
  115. ficheiro = fopen("biblio.dat", "rw");
  116.  
  117. if (ficheiro == NULL) {
  118. printf("Ficheiro não disponivel\n");
  119. return 0;
  120. }
  121.  
  122. while(! feof(ficheiro))
  123. {
  124. fscanf(ficheiro, "%s , %s , %s , %s , %s\n", nome,autor,ano,descricao,palavrachave);
  125. b = inserir (b,nome,ano,descricao,palavrachave,autor);
  126. }
  127.  
  128. while (x!=0){
  129. printf("Insira a opção:\n1-Inserir\n2-Remover\n3-Alterar\n4-Listar Geral\5-Listar Único\n0-Sair");
  130. scanf("%d\n",&x);
  131. switch(x){
  132. case(1):{
  133. printf("Nome ?\n");
  134. scanf("%s",nome);
  135. printf("Autor ?\n");
  136. scanf("%s",autor);
  137. printf("Ano ?\n");
  138. scanf("%s",ano);
  139. printf("Descricao ?\n");
  140. scanf("%s",descricao);
  141. printf("Palavrachave ?\n");
  142. scanf("%s",palavrachave);
  143. b = inserir (b,nome,ano,descricao,palavrachave,autor);
  144. break;}
  145. case(2):{
  146. printf("Nome ?\n");
  147. scanf("%s",nome);
  148. b = remover(b,nome);
  149. break;}
  150. case(3):{
  151. printf("Nome ?\n");
  152. scanf("%s",nome);
  153. b = altera (b,nome);
  154. break;}
  155. case(4):{
  156. ListarLivros (b);
  157. break;}
  158. case(5):{
  159. printf("Nome ?\n");
  160. scanf("%s",nome);
  161. Lista_Livro (b,nome);
  162. break;}
  163. case(0):{
  164. for (z=b;!z;z=z->seguinte)
  165. fprintf(ficheiro, "%s , %s , %s , %s , %s\n",z->l.nome,z->l.autor,z->l.ano,z->l.descricao,z->l.palavrachave);
  166. fclose(ficheiro);
  167. break;}
  168. default:{
  169. printf("Opção Incorrecta\n");
  170. break;}
  171. }
  172. }
  173. return 1;
  174. }
Add Comment
Please, Sign In to add comment