Guest User

Untitled

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