Kimossab

Adicionar/Remover perguntas de lista 2

Jun 8th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. PERGUNTA PerdirDados()
  2. {
  3.     PERGUNTA *P = (PERGUNTA)malloc(sizeof(PERGUNTA));
  4.     P->codigoperg = (char *)malloc(4);
  5.     P->enunciado = (char *)malloc(285);
  6.     P->LResp = (ListaRespostas *)malloc(sizeof(ListaRespostas));
  7.     P->LResp->inicio = (ELEMENTO_RESPOSTAS *)malloc(sizeof(ELEMENTO_RESPOSTAS));
  8.     P->LResp->fim = (ELEMENTO_RESPOSTAS *)malloc(sizeof(ELEMENTO_RESPOSTAS));
  9.     P->LResp->inicio->info = (Resposta *)malloc(sizeof(Resposta));
  10.     P->LResp->fim->info = (Resposta *)malloc(sizeof(Resposta));
  11.     P->LResp->inicio->info->Codigo = (char *)malloc(4);
  12.     P->LResp->fim->info->Codigo = (char *)malloc(4);
  13.     P->LResp->inicio->info->Resp_pergunta = (char *)malloc(248);
  14.     P->LResp->fim->info->Resp_pergunta = (char *)malloc(248);
  15.     P->LResp->inicio->prox = NULL;
  16.     P->LResp->fim->prox = NULL;
  17.     P->LResp->inicio->ant = NULL;
  18.     P->LResp->fim->ant = NULL;
  19.    
  20.     ELEMENTO_RESPOSTAS *E = P->LResp->inicio = P->LResp->fim, *AUX;
  21.  
  22.     printf("\nInsira o enunciado: ");
  23.     fgets(P->Enunciado);
  24.     printf("\nInsira a dificuldade: ");
  25.     scanf("%d", &P->dificuldade);
  26.     printf("\nInsira quantas respostas quer: ");
  27.     scanf("%d", &P->LResp->nel);
  28.     printf("\nInsira a resposta certa: ");
  29.     fgets(E->info->Resp_pergunta);
  30.     fprintf(E->info->Codigo, "R1");
  31.     for(int i = 1; i<P->LResp->nel; i++)
  32.     {
  33.         AUX = E;
  34.         E->prox = (ELEMENTO_RESPOSTAS *)malloc(sizeof(ELEMENTO_RESPOSTAS));
  35.         E=E->prox;
  36.         E->ant = AUX;
  37.         E->info->Codigo = (char *)malloc(4);
  38.         E->info->Resp_pergunta = (char *)malloc(248);
  39.         P-LResp->fim = E;
  40.         printf("\nInsira a proxima resposta: ");
  41.         fgets(E->Resp_Pergunta);
  42.         fprintf(E->info->Codigo, "R%d", i);
  43.     }
  44.     return P;
  45. }
  46.  
  47. void AdicionarPergunta(PERGUNTA *P, LISTAPERGUNTAS *LP)
  48. {
  49.     LP->fim->prox = (ELEMENTO_PERGUNTA *)malloc(sizeof(ELEMENTO_PERGUNTA));
  50.     ELEMENTO_PERGUNTA *AUX = LP->fim;
  51.     LP->fim = LP->fim->prox;
  52.     LP->fim->ant = AUX;
  53.     LP->fim->prox = NULL;
  54.     LP->fim->info = P;
  55.     LP->n_elementos++;
  56. }
  57.  
  58. void RemoverPergunta(char *Codigo, LISTAPERGUNTAS *LP)
  59. {
  60.     ELEMENTO_PERGUNTA *AUX = LP->inicio;
  61.     ELEMENTO_RESPOSTAS *AUX2, *AUX3;
  62.     while(AUX)
  63.     {
  64.         if(stricmp(AUX->info->codigoperg, Codigo) == 0)
  65.         {
  66.             if(AUX->prox)
  67.                 AUX->ant = AUX->prox;
  68.             else
  69.             {
  70.                 AUX->ant->prox = NULL;
  71.                 LP->fim = AUX->ant;
  72.             }
  73.             free(AUX->info->codigoperg);
  74.             free(AUX->info->enunciado);
  75.             AUX2 = AUX->LResp->inicio;
  76.             for(int i=0; i<AUX->LResp->nel; i++)
  77.             {
  78.                 free(AUX2->Codigo);
  79.                 free(AUX2->Resp_pergunta);
  80.                 AUX3 = AUX2;
  81.                 AUX2 = AUX2->prox;
  82.                 free(AUX3);
  83.             }
  84.             free(AUX->LResp->inicio);
  85.             free(AUX->LResp->fim);
  86.         }
  87.     }
  88.     LP->n_elementos--;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment