Advertisement
ThaisAlmeida

Hoje

Aug 18th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6. typedef struct {
  7.  
  8. char rua[30];
  9. char bairro[10];
  10. char cidade[10];
  11. } endereco;
  12.  
  13. typedef struct {
  14.  
  15. char nome[30];
  16. int idade;
  17. char telefone[10];
  18. endereco endereco;
  19. } AGENDA;
  20.  
  21. typedef struct {
  22.  
  23. char nome[30];
  24. int idade;
  25. char rua[30];
  26. char bairro[10];
  27. } SAIDA;
  28.  
  29. void Cadastro(AGENDA *agenda, FILE *arquivo, FILE *arquivo2, SAIDA *saida) {
  30.  
  31. char resposta, bairro[10], cidade[10];
  32. int i = 0, soma = 0, contatos = 0, media = 0;
  33.  
  34. do {
  35.  
  36. printf("Digite o nome:\n");
  37. gets(agenda->nome);
  38. fflush(stdin);
  39.  
  40. printf("Digite a idade:\n");
  41. scanf("%d", &agenda->idade);
  42. fflush(stdin);
  43.  
  44. printf("Digite o telefone:\n");
  45. gets(agenda->telefone);
  46. fflush(stdin);
  47.  
  48. printf("Digite a rua:\n");
  49. gets(agenda->endereco.rua);
  50. fflush(stdin);
  51.  
  52. printf("Digite o bairro:\n");
  53. gets(agenda->endereco.bairro);
  54. fflush(stdin);
  55.  
  56. printf("Digite a cidade:\n");
  57. gets(agenda->endereco.cidade);
  58. fflush(stdin);
  59.  
  60. printf("Deseja realizar um novo cadastro?(S/N)\n");
  61. scanf("%c", &resposta);
  62. fflush(stdin);
  63.  
  64. fwrite(agenda, sizeof(AGENDA), 1, arquivo);
  65.  
  66. } while (resposta == 's');
  67.  
  68. printf("Digite o nome da cidade que deseja pesquisar:\n");
  69. gets(cidade);
  70. fflush(stdin);
  71.  
  72. rewind(arquivo);
  73. while (!feof(arquivo)) {
  74. fread(agenda, sizeof(AGENDA), 1, arquivo);
  75. if (strcmp(agenda->endereco.cidade, cidade) == 0) {
  76.  
  77. strcpy(agenda->nome, saida->nome);
  78. saida->idade = agenda->idade;
  79. strcpy(agenda->endereco.rua, saida->rua);
  80. strcpy(agenda->endereco.bairro, saida->bairro);
  81. fwrite(saida, sizeof(SAIDA), 1, arquivo);
  82. i++;
  83. }
  84.  
  85. }
  86.  
  87. if (i == 0) {
  88. printf("Cidade inexistente");
  89. }
  90.  
  91. if (i > 50) {
  92. printf("Digite o nome do bairro:/n");
  93. gets(bairro);
  94. rewind(arquivo);
  95. while (!feof(arquivo)) {
  96. fread(agenda, sizeof(AGENDA), 1, arquivo);
  97. if (strcmp(agenda->endereco.bairro, bairro) == 0) {
  98. contatos++;
  99. soma = soma + agenda->idade;
  100. }
  101. }
  102.  
  103. media = soma / contatos;
  104. printf("A media eh %d:/n", media);
  105.  
  106. }
  107. }
  108.  
  109. int main() {
  110.  
  111. FILE *arquivo1;
  112. FILE *arquivo2;
  113. arquivo1 = fopen("AGENDA.CAD", "w+b");
  114. arquivo2 = fopen("SAIDA.DAT", "w+b");
  115.  
  116. AGENDA *agenda;
  117. agenda = malloc(sizeof(AGENDA));
  118.  
  119. SAIDA *saida;
  120. saida = malloc(sizeof(SAIDA));
  121.  
  122. Cadastro(agenda, arquivo1, arquivo2, saida);
  123.  
  124. fclose(arquivo1);
  125. fclose(arquivo2);
  126.  
  127. free(agenda);
  128. agenda = NULL;
  129. free(saida);
  130. saida = NULL;
  131. getche();
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement