Guest User

Untitled

a guest
Oct 23rd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int INDICE = 0;
  7. int CAPACIDADE = 5;
  8.  
  9. struct Contato {
  10. char nome[60];
  11. char telefone[15];
  12. char email[32];
  13. };
  14.  
  15. void insereContatoNaAgenda(Contato novaContato, Contato *&agenda){
  16. agenda[INDICE] = novaContato;
  17. INDICE++;
  18. if (INDICE >= CAPACIDADE) {
  19. CAPACIDADE*=2;
  20. agenda = (Contato *) realloc(agenda, sizeof(Contato)*CAPACIDADE);
  21. }
  22. }
  23.  
  24. void inicializarAgenda(Contato *&agenda){
  25. agenda = (Contato *) malloc(sizeof(Contato)*CAPACIDADE);
  26. }
  27.  
  28. Contato criaNovoContato(){
  29. Contato contato;
  30. cout << "Nome: ";
  31. cin.ignore();
  32. cin.getline(contato.nome,sizeof(contato.nome));
  33. cout << "Telefone: ";
  34. cin.ignore();
  35. cin.getline(contato.telefone,sizeof(contato.telefone));
  36. cout << "E-Mail: ";
  37. cin >> contato.email;
  38. return contato;
  39. }
  40.  
  41. void imprimeAgenda(Contato* agenda) {
  42. for (int i=0; i < INDICE; i++) {
  43. cout << "Entrada " << i << ":" << endl;
  44. cout << " Nome: " << agenda[i].nome << endl;
  45. cout << " Telefone: " << agenda[i].telefone << endl;
  46. cout << " E-Mail: " << agenda[i].email << endl;
  47. }
  48. }
  49.  
  50. void imprimeMenu() {
  51. system("cls");
  52. cout << R"( _ __
  53. / \ | ]
  54. / _ \ .--./) .---. _ .--. .--.| | ,--. 1.0
  55. / ___ \ / /'`\;/ /__\\[ `.-. |/ /'`\' | `'_\ :
  56. _/ / \ \_ \ \._//| \__., | | | || \__/ | // | |,
  57. |____| |____|.',__` '.__.'[___||__]'.__.;__]\'-;__/
  58. ( ( __)) )" << endl << endl << flush;
  59.  
  60. cout << "Menu:" << endl;
  61. cout << "1 - Inserir contato" << endl;
  62. cout << "2 - Listar contatos" << endl;
  63. cout << "3 - Estado do array" << endl;
  64. cout << "0 - Sair do programa" << endl;
  65. }
  66.  
  67. int main()
  68. {
  69. char menu = ' ';
  70. Contato *agenda = NULL;
  71. inicializarAgenda(agenda);
  72.  
  73. while (menu != 0) {
  74. imprimeMenu();
  75. cin >> menu;
  76.  
  77. switch (menu) {
  78. case '0':
  79. break;
  80. case '1': {
  81. Contato novoContato = criaNovoContato();
  82. insereContatoNaAgenda(novoContato, agenda);
  83. break;
  84. }
  85. case '2':
  86. imprimeAgenda(agenda);
  87. break;
  88. case '3':
  89. cout << "Elementos inseridos: " << INDICE << endl;
  90. cout << "Capacidade do array: " << CAPACIDADE << endl;
  91. break;
  92. default:
  93. cout << "Opção inválida!" << endl;
  94. break;
  95. }
  96. }
  97. cout << agenda[0].nome;
  98. free(agenda);
  99.  
  100. return 0;
  101. }
Add Comment
Please, Sign In to add comment