Ir ao conteúdo
  • Cadastre-se

Junior12332

Membro Júnior
  • Posts

    11
  • Cadastrado em

  • Última visita

posts postados por Junior12332

  1. Estou tentando fazer um trabalho da faculdade que é para criar uma agenda, e estou na hora de remover um contato da agenda. Quando tem 2 contatos e eu mando remover o ultimo ele remove tudo. O que faço?

    Obrigado!

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    typedef struct contato {
        char nome[100];
        char fone[15];
        struct contato *ant, *prox;
    }Tcontato;
    typedef struct indice {
        char letra;
        struct indice *ant, *prox;
        Tcontato *inicio;
    }Tindice;
    Tcontato *busca_contato(Tcontato *L, char nome[]);
    Tindice *busca_indice(Tindice *L, char nome[]);
    void inserir(Tindice **L, char nome[]);
    void remover(Tindice **L, char nome[]);
    void exibir_telefone(Tindice *L, char nome[]);
    void exibir_lista_letra(Tindice *L, char letra);
    void alterar_telefone(Tindice *L, char nome[]);
    void exibir_lista(Tindice *L);
    void del_lista(Tindice *L);
    int main() {
        Tindice *agenda = NULL;
        char nome[100];
        char letra;
        int op;
        do {
            printf("1 - Inserir.\n2 - Remover.\n3 - Exibir Telefone de um contato.\n4 - Alterar o telefone.\n5 - Exibir a agenda.\n6 - Exibir agenda com letra.\n7 - Sair do programa.\n.Digite uma das opções: ");
            scanf("%d", &op);
            switch (op) {
            case 1: printf("Digite o nome: ");
                scanf("%s", &nome);
                inserir(&agenda, nome);
                break;
            case 2: printf("Digite o nome: ");
                scanf("%s", &nome);
                remover(&agenda, nome);
                break;
            case 5:
                exibir_lista(agenda);
                break;
            case 7:
                //del_lista(&agenda);
                break;
            }
        } while (op != 7);
        return 0;
    }
    void exibir_lista(Tindice *L) {
        Tindice *aux;
        Tcontato *aux1;
        if (L == NULL) {
            printf("LISTA VAZIA.\n");
        }
        else {
            aux = L;
            do {
                printf("Letra: %c\n", aux->letra);
                aux1 = aux->inicio;
                do {
                    printf("Nome: %s\nTelefone %s\n\n", aux1->nome, aux1->fone);
                    aux1 = aux1->prox;
                } while (aux1 != aux->inicio);
                aux = aux->prox;
            } while (aux != L);
        }
    }
    Tcontato *busca_contato(Tcontato *L, char nome[]) {
    }
    Tindice *busca_indice(Tindice *L, char nome[]) {
        Tindice *aux;
        Tcontato *aux1;
    }
    void remover(Tindice **L, char nome[]) {
        Tindice *aux = *L;
        Tcontato *aux1;
        if (*L == NULL) {
            printf("LISTA VAZIA.\n");
        }
        else {
            do {
                if (nome[0] < aux->letra) {
                    printf("Contato nao existe.\n");
                    return;
                }
                if (nome[0] == aux->letra) {
                    aux1 = aux->inicio;
                    do {
                        if (strcmp(nome, aux1->nome) < 0) {
                            printf("Contato nao existe.\n");
                            return;
                        }
                        else if (strcmp(nome, aux1->nome) == 0) {
                            if (aux1->prox == aux->inicio && aux1->ant == aux->inicio) {
                                if (aux->ant == *L && aux->prox == *L) {
                                    free(aux1);
                                    free(aux);
                                    *L = NULL;
                                    printf("Contato deletado da agenda.\n");
                                    return;
                                }
                                else if (aux == *L) {
                                    *L = (*L)->prox;
                                    aux->ant->prox = *L;
                                    (*L)->ant = aux->ant;
                                    free(aux1);
                                    free(aux);
                                    printf("Contato deletado da agenda.\n");
                                    return;
                                }
                                else if (aux->prox == *L) {
                                    aux->ant->prox = *L;
                                    (*L)->ant = aux->ant;
                                    free(aux1);
                                    free(aux);
                                    printf("Contato deletado da agenda.\n");
                                    return;
                                }
                                else {
                                    aux->ant->prox = aux->prox;
                                    aux->prox->ant = aux->ant;
                                    free(aux1);
                                    free(aux);
                                    printf("Contato deletado da agenda.\n");
                                    return;
                                }
                            }
                            else if (aux1 == aux->inicio) {
                                aux->inicio = aux->inicio->prox;
                                aux1->ant->prox = aux->inicio;
                                aux->inicio->ant = aux1->ant;
                                free(aux1);
                                printf("Contato deletado da agenda.\n");
                                return;
                            }
                            else if (aux1->prox == aux->inicio) {
                                aux1->ant->prox = aux->inicio;
                                aux->inicio->ant = aux1->ant;
                                free(aux1);
                                printf("Contato deletado da agenda.\n");
                                return;
                            }
                            else {
                                aux1->ant->prox = aux1->prox;
                                aux1->prox->ant = aux1->ant;
                                free(aux1);
                                printf("Contato deletado da agenda.\n");
                                return;
                            }
                        }
                        aux1 = aux1->prox;
                    } while (aux1 != aux->inicio);
                }
                aux = aux->prox;
            } while (aux != *L);
            printf("Contato nao existe.\n");
        }
    }
    void inserir(Tindice **L, char nome[]) {
        Tindice *aux = *L;
        Tindice *novo;
        Tcontato *novo1;
        Tcontato *aux1;
        char tel[15];
        if (*L == NULL) {
            novo = (Tindice *)malloc(sizeof(Tindice));
            novo->letra = nome[0];
            novo->ant = novo;
            novo->prox = novo;
            *L = novo;
            novo1 = (Tcontato *)malloc(sizeof(Tcontato));
            strcpy(novo1->nome, nome);
            printf("Digite o telefone: ");
            scanf("%s", &tel);
            strcpy(novo1->fone, tel);
            novo1->ant = novo1;
            novo1->prox = novo1;
            (*L)->inicio = novo1;
            printf("Contato registrado.\n");
        }
        else {
            do {
                if (nome[0] == aux->letra) {
                    aux1 = aux->inicio;
                    do {
                        if (strcmp(nome, aux1->nome) == 0) {
                            printf("Contato ja existente.\n");
                            return;
                        }
                        else if (strcmp(nome, aux1->nome) < 0) {
                            novo1 = (Tcontato*)malloc(sizeof(Tcontato));
                            strcpy(novo1->nome, nome);
                            printf("Digite o telefone: ");
                            scanf("%s", &tel);
                            strcpy(novo1->fone, tel);
                            if (aux1 == aux->inicio) {
                                aux1->ant->prox = novo1;
                                novo1->ant = aux1->ant;
                                novo1->prox = aux1;
                                aux1->ant = novo1;
                                aux->inicio = novo1;
                                printf("Contato registrado.\n");
                                return;
                            }
                            else {
                                aux1->ant->prox = novo1;
                                novo1->ant = aux1->ant;
                                novo1->prox = aux1;
                                aux1->ant = novo1;
                                printf("Contato registrado.\n");
                                return;
                            }
                        }
                        aux1 = aux1->prox;
                    } while (aux1 != aux->inicio);
                    novo1 = (Tcontato*)malloc(sizeof(Tcontato));
                    strcpy(novo1->nome, nome);
                    printf("Digite o telefone: ");
                    scanf("%s", &tel);
                    strcpy(novo1->fone, tel);
                    aux1 = aux1->ant;
                    novo1->prox = aux1->prox;
                    novo1->ant = aux1;
                    aux1->prox->ant = novo1;
                    aux1->prox = novo1;
                    printf("Contato registrado.\n");
                    return;
                }
                else if (nome[0] < aux->letra) {
                    novo = (Tindice *)malloc(sizeof(Tindice));
                    novo->letra = nome[0];
                    if (aux == *L) {
                        aux->ant->prox = novo;
                        novo->ant = aux->ant;
                        novo->prox = aux;
                        aux->ant = novo;
                        *L = novo;
                        novo1 = (Tcontato *)malloc(sizeof(Tcontato));
                        strcpy(novo1->nome, nome);
                        printf("Digite o telefone: ");
                        scanf("%s", &tel);
                        strcpy(novo1->fone, tel);
                        novo1->ant = novo1;
                        novo1->prox = novo1;
                        novo->inicio = novo1;
                        printf("Contato registrado.\n");
                        return;
                    }
                    else {
                        aux->ant->prox = novo;
                        novo->ant = aux->ant;
                        novo->prox = aux;
                        aux->ant = novo;
                        novo1 = (Tcontato *)malloc(sizeof(Tcontato));
                        strcpy(novo1->nome, nome);
                        printf("Digite o telefone: ");
                        scanf("%s", &tel);
                        strcpy(novo1->fone, tel);
                        novo1->ant = novo1;
                        novo1->prox = novo1;
                        novo->inicio = novo1;
                        printf("Contato registrado.\n");
                        return;
                    }
                }
                aux = aux->prox;
            } while (aux != *L);
            novo = (Tindice*)malloc(sizeof(Tindice));
            novo->letra = nome[0];
            aux = aux->ant;
            novo->prox = aux->prox;
            novo->ant = aux;
            aux->prox->ant = novo;
            aux->prox = novo;
            novo1 = (Tcontato*)malloc(sizeof(Tcontato));
            strcpy(novo1->nome, nome);
            printf("Digite o telefone: ");
            scanf("%s", &tel);
            strcpy(novo1->fone, tel);
            novo1->ant = novo1;
            novo1->prox = novo1;
            novo->inicio = novo1;
            printf("Contato registrado.\n");
        }
    }

     

  2. Galera, eu estou tentando fazer esse codigo para inserir numeros no vetor de forma ordenada, mas não sei porque não está funcionando. É como se não estivesse chamando o procedimento. O que eu faço?

    Obrigado!

    #include <stdio.h>
    
    void inserir(int vet1[], int *qtd, int numero);
    
    int main() {
        int vet_par[50];
        int vet_impar[50];
        char op;
        int n;
        int qtd_par = 0;
        int qtd_impar = 0;
        do {
            printf("1 - Inserir valor.\n4 - Encerrar programa.\nDigite uma das opções.");
            op = getchar();
            fflush(stdin);
            switch (op) {
            case '1':
                printf("Digite o valor a ser inserido: ");
                scanf("%d", &n);
                if (n % 2 == 0) {
                    inserir(vet_par, &qtd_par, n);
                }
                else {
                    inserir(vet_impar, &qtd_impar, n);
                }
                break;
            }
        } while (op != '4');
        system("pause");
        return 0;
    }
    
    void inserir(int vetor[], int *qtd, int numero) {
        int i;
        int achou = 0;
        int j;
        if (*qtd == 50) {
            printf("Vetor cheio.");
        }
        else {
            for (i = 0; i < *qtd; i++) {
                if (vetor[ i ] == numero) {
                    printf("Valor ja existente no vetor.");
                    achou = 1;
                    break;
                }
                else if (vetor[ i ] > numero) {
                    break;
                }
                if (achou == 0) {
                    for (j = *qtd; j > i; j--) {
                        vetor[j] = vetor[j - 1];
                    }
                    vetor[ i ] = numero;
                    (*qtd)++;
                    printf("%d Insercao efetuada.\n", numero);
                }
            }
        }
    }

     

  3. Não sei se estou colocando a pergunta no lugar certo, sou novo no fórum, mas estou com uma dúvida na linguagem C, sobre vetores.

    #include  <stdio.h>

    int main() {
        int vetor[3];
        int subtracao[3];
        int maior_subtracao = -2000000000;
        int i;
        printf("Digite 3 numeros: ");
        for (i = 0; i < 3; i++) {
            scanf("%d", &vetor);
        }
        for (i = 0; i < 3 ; i++) {
            subtracao = vetor - vetor[i+1];
        }
        for (i = 0; i < 3; i++) {
            if (subtracao > maior_subtracao) {
                maior_subtracao = subtracao;
            }
        }
        printf("%d\n", maior_subtracao);

        system("pause");
        return 0;

    }

    O programa está aparecendo números errados, e não sei porquê.

    Alguém pode dizer onde está o erro, por favor?

    Agradeço!!!

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...