Baixe o app para aproveitar ainda mais
Prévia do material em texto
Problema envolvendo Struct 1. #include <stdio.h> 2. #include <string.h> 3. 4. #define TAM 100 5. 6. typedef struct { 7. char nome[101]; 8. char telefone[11]; 9. } contatos; 10. 11. 12. void Escrever_Contato(contatos agenda[TAM], int i){ 13. printf("Contato[%d]- Nome:%s Tel:%s\n", i, agenda[i].nome, agenda[i].telefone); 14. } 15. 16. 17. int Validar_Indice(int i){ 18. if ((i < TAM) && (0 < i)) return 1; 19. else { 20. printf("Índice digitado não existe [1,100]!\n"); 21. return 0; 22. } 23. } 24. 25. int Validar_Indice2(contatos agenda[TAM], int i){ 26. if(strlen(agenda[i].telefone) < 5) { 27. printf("Índice digitado está vazio!\n"); 28. return 0; 29. } 30. else return 1; 31. } 32. 33. int Verificar_Tel(char str2[11]){ 34. if (9 < strlen(str2) && strlen(str2) < 12) return 0; 35. else { 36. printf("Telefone não tem 10 ou 11 dígitos! Favor digite novamente.\n"); 37. return 1; 38. } 39. } 40. 41. int Validar_Contato(contatos agenda[TAM], int i){ 42. if(strlen(agenda[i].telefone) < 5) { 43. printf("Contato digitado não existe!\n"); 44. return 0; 45. } 46. else return 1; 47. } 48. 49. int Validar_Agenda(contatos agenda[TAM]){ 50. for(int i = 0; i < TAM; i++){ 51. if (strlen(agenda[i].telefone) > 5) 52. return 1; 53. } 54. printf("Agenda vazia!\n"); 55. return 0; 56. } 57. 58. 59. void Cadastrar_Nome(contatos agenda[TAM]){ 60. int i; 61. scanf("%d", &i); 62. while(Validar_Indice(i)==0){scanf("%d", &i);} 63. scanf("%s", agenda[i].nome); 64. do {scanf("%s", agenda[i].telefone);} 65. while (Verificar_Tel(agenda[i].telefone)); 66. printf("Contato cadastrado com sucesso!\n"); 67. } 68. 69. void Mostrar_Contatos(contatos agenda[TAM]){ 70. 71. for(int i = 0; i < TAM; i++){ 72. if (strlen(agenda[i].telefone) > 9) 73. printf("Contato[%d]- Nome:%s Tel:%s\n", i, agenda[i].nome, agenda[i].telefone); 74. 75. } 76. } 77. 78. void Mostrar_Contato(contatos agenda[TAM]){ 79. int i; 80. scanf("%d", &i); 81. if (Validar_Indice2(agenda, i)) 82. printf("Contato[%d]- Nome:%s Tel:%s\n", i, agenda[i].nome, agenda[i].telefone); 83. } 84. 85. void Apagar_Contato(contatos agenda[TAM]){ 86. int i; 87. scanf("%d", &i); 88. if (Validar_Indice(i)){ 89. if (strlen(agenda[i].telefone) > 9){ 90. *agenda[i].nome = '\0'; 91. *agenda[i].telefone = '\0'; 92. printf("Contato removido com sucesso!\n"); 93. Mostrar_Contatos(agenda); 94. } 95. else printf("Índice digitado está vazio!\n"); 96. } 97. } 98. 99. void Apagar_Contato_Nome(contatos agenda[TAM]){ 100. int i, j = 0; 101. char nome[101]; 102. scanf("%s", nome); 103. for(int f = 0; f < TAM; f++){ 104. if (strcmp(agenda[f].nome, nome) == 0){ 105. j = 1; 106. i = f; 107. } 108. } 109. if (j){ 110. *agenda[i].nome = '\0'; 111. *agenda[i].telefone = '\0'; 112. printf("Contato removido com sucesso!\n"); 113. Mostrar_Contatos(agenda); 114. } 115. else printf("Contato digitado não existe!\n"); 116. } 117. int main(){ 118. contatos agenda[100]; 119. for (int i = 0; i < TAM; i++){ 120. *agenda[i].nome = '\0'; 121. *agenda[i].telefone = '\0'; 122. } 123. for(int num = 1; num != 0;){ 124. scanf("%d", &num); 125. switch(num){ 126. case 1: 127. Cadastrar_Nome(agenda); 128. break; 129. 130. case 2: if(Validar_Agenda(agenda)) 131. Mostrar_Contatos(agenda); 132. break; 133. 134. case 3: if(Validar_Agenda(agenda)) 135. Mostrar_Contato(agenda); 136. break; 137. 138. case 4: 139. Apagar_Contato(agenda); 140. break; 141. case 5: Apagar_Contato_Nome(agenda); 142. break; 143. default: 144. if (num < 0 || num > 5){ 145. printf("Escolha valores entre 0 e 5!\n"); 146. } 147. break; 148. } 149. } 150. printf("Tchau!"); 151. 152. }
Compartilhar