Ir para conteúdo

Brunow0

Members
  • Total de itens

    1
  • Registro em

  • Última visita

Reputação

0 Comum

Sobre Brunow0

  1. Brunow0

    Alocação de memória ajuda

    Preciso sabe se estou alocando memória corretamente no meu código. Conto com a ajuda de vocês! O exercício pede o seguinte: Crie um programa de cadastro simples de agenda (em memória) com inclusão de novas entradas e alteração, consulta, exclusão e pesquisa (ordenada) de entradas já existentes. #include <stdio.h> #include <stdlib.h> #include <string.h> int op=0,op1,qcad=0,i=0,x,y; char name[50]; struct contato { char nome[50]; char email[50]; char telefone[15]; }; void cadastrar (struct contato *c) { printf("\nDigite o nome: "); gets(c.nome); strupr(c.nome); printf("Digite o email: "); gets(c.email); printf("Digite o telefone: "); gets(c.telefone); i++; do { printf("\nDeseja cadastrar mais? \n1-SIM \n2-NAO\n"); scanf("%d%*c", &op1); fflush(stdin); if (op1==1) { c=(struct contato*)(realloc(c,(qcad+1)*sizeof(struct contato))); if (c==NULL) { printf("\n ERRO // MEMORIA INSUFICIENTE"); exit(1); } qcad++; cadastrar(c); } }while (op1!=2); } void altera (struct contato *c) { printf("Digite o nome: "); gets(name); strupr(name); for (x=0,y=-1;x<i;x++) { if (strcmp(name,c[x].nome)==0) { y=x; } } if(y<0) { printf("\nNOME NAO CADASTRADO\n\n"); system("pause"); } else { printf("\nDigite o novo nome: "); gets (c[y].nome); strupr(c[y].nome); fflush(stdin); printf("\nDigite o novo email: "); gets (c[y].email); fflush(stdin); printf("\nDigite o novo telefone: "); gets(c[y].telefone); fflush(stdin); } } void ordena (struct contato *c) { char nomeaux[50]; char emailaux[50]; char telefoneaux[15]; for (x=1;x<i;x++) { for (y=x;y>0;y--) { if (strcoll(c[y].nome, c[y-1].nome) < 0) { strcpy(nomeaux,c[y].nome); strcpy(emailaux,c[y].email); strcpy(telefoneaux,c[y].telefone); strcpy(c[y].nome,c[y-1].nome); strcpy(c[y].email,c[y-1].email); strcpy(c[y].telefone,c[y-1].telefone); strcpy(c[y-1].nome,nomeaux); strcpy(c[y-1].email,emailaux); strcpy(c[y-1].telefone,telefoneaux); } } } } void consulta(struct contato *c) { if (i==0) { printf("\nNENHUM CADASTRO\n\n"); system("pause"); } else { ordena(c); for (x=0;x<i;x++) { printf("\n NOME: %s ",c[x].nome); printf("\n EMAIL: %s ",c[x].email); printf("\n FONE: %s \n",c[x].telefone); system("pause"); } } } void exclui(struct contato *c) { for (x=0,y=-1;x<i;x++) { if (strcmp(name,c[x].nome)==0) { y=x; } } if(y<0) { printf("O nome nao existe\n"); } else { printf("Deseja excluir? \n1 - SIM \n2 - NAO\n"); scanf("%d%*c",&op1); if (op1==1) { strcpy(c[y].nome,c[i-1].nome); strcpy(c[y].email,c[i-1].email); strcpy(c[y].telefone,c[i-1].telefone); i--; c=(struct contato*)(realloc(c,(i)*sizeof(struct contato))); qcad--; printf("EXCLUIDO COM SUCESSO\n"); } } } void pesquisa(struct contato *c) { printf("Digite o nome: "); gets(name); strupr(name); for (x=0,y=-1;x<i;x++) { if (strcmp(name,c[x].nome)==0) { y=x; } } if(y<0) { printf("O nome nao existe\n"); system("pause"); } else { printf("\n NOME: %s ",c[y].nome); printf("\n EMAIL: %s ",c[y].email); printf("\n FONE: %s \n",c[y].telefone); exclui(c); system("pause"); } } void menu() { struct contato *c=(struct contato*)malloc(qcad*sizeof(struct contato)); if (c==NULL) { printf("\n ERRO // MEMORIA INSUFICIENTE"); exit(1); } do { system("cls"); op=-1; printf("******MENU******\n"); printf("1 - Cadastrar \n2 - Alterar \n3 - Consultar \n4 - Excluir \n5 - Pesquisar \n9 - Sair\n"); printf("****************\n"); scanf("%d%*c", &op); switch (op) { case 1: c=(struct contato*)(realloc(c,(qcad+1)*sizeof(struct contato))); if (c==NULL) { printf("\n ERRO // MEMORIA INSUFICIENTE"); exit(1); } qcad++; cadastrar(c); break; case 2: altera(c); break; case 3: consulta(c); break; case 4: printf("Digite o nome: "); gets(name); strupr(name); fflush(stdin); exclui(c); if (i==0) { c=(struct contato*)(realloc(c,(qcad+1)*sizeof(struct contato))); } system("pause"); break; case 5: pesquisa(c); if (i==0) { c=(struct contato*)(realloc(c,(qcad+1)*sizeof(struct contato))); } break; case 9: return 0; break; default: puts("Opcao invalida!!!\n\n\n"); system("pause"); } }while (op!=9); } int main() { menu(); return 0; }
×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.