Advertisement
Guest User

G2

a guest
Nov 14th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.98 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct alunos{
  6.     char nome[30], telefone[30], email[30];
  7.     int codigo;
  8.     float g1, g2;
  9. } Aluno;
  10.  
  11. typedef struct nodo{
  12.     Aluno dados;
  13.     struct nodo *elo;
  14. } Nodo;
  15.  
  16. typedef struct descritor{
  17.     Nodo *primeiro;
  18.     Nodo *ultimo;
  19.     int nElementos;
  20. } Descritor;
  21.  
  22. void CriaLista(Descritor *d){
  23.     d->primeiro = NULL;
  24.     d->nElementos = 0;
  25.     d->ultimo = NULL;
  26. }
  27.  
  28. void IncluirInicio(Descritor *d, Aluno dados){
  29.     Nodo *n;
  30.    
  31.     n = (Nodo*) malloc (sizeof(Nodo));
  32.     if(n == NULL)
  33.         printf("\nLISTA CHEIA OU INEXISTENTE.");
  34.     else{
  35.         n->dados = dados;
  36.         n->elo = d->primeiro;
  37.         d->primeiro = n;
  38.             if(d->nElementos == 0){
  39.                 d->ultimo = n;
  40.                 d->nElementos++;
  41.             }
  42.         printf("\nELEMENTO INCLUIDO COM SUCESSO!");
  43.     }
  44. }
  45.  
  46. void IncluirFinal(Descritor *d, Aluno dados){
  47.     Nodo *n;
  48.    
  49.     n = (Nodo*) malloc(sizeof(Nodo));
  50.     if(n == NULL)
  51.         printf("\nLISTA CHEIA OU INEXISTENTE.");
  52.     else{
  53.         n->dados = dados;
  54.         n->elo = NULL;
  55.         if(d->nElementos == 0){
  56.             d->primeiro = n;
  57.         }else{
  58.             d->ultimo->elo = n;
  59.             d->ultimo = n;
  60.             printf("\nELEMENTO INCLUIDO COM SUCESSO!");
  61.         }
  62.         d->nElementos++;
  63.     }
  64. }
  65.  
  66. void IncluirAposNome(Descritor *d, Aluno dados, char nomeProcurado[30]){
  67.     Nodo *n, *novoNodo, *aux;
  68.     int cont;
  69.     Aluno al;
  70.    
  71.     if(d->primeiro == NULL)
  72.         printf("\nLISTA VAZIA OU INEXISTENTE.");
  73.     else{
  74.         n = d->primeiro;
  75.         while(n != NULL){
  76.             cont = strcmp(nomeProcurado,n->dados.nome);
  77.             if(cont == 0){
  78.                 break;
  79.             }
  80.             n = n->elo;
  81.         }
  82.         if(cont == 0){
  83.             al = dados;
  84.             novoNodo = (Nodo*) malloc (sizeof(Nodo));
  85.             if(novoNodo == NULL){
  86.                 printf("\nFALTA DE MEMORIA.");
  87.             }else{
  88.                 novoNodo->dados = al;
  89.                 if(novoNodo->elo == NULL){
  90.                     n->elo = novoNodo;
  91.                     d->ultimo = novoNodo;
  92.                     novoNodo->elo = NULL;
  93.                 }else{
  94.                     aux = n->elo;
  95.                     n->elo = novoNodo;
  96.                     novoNodo->elo = aux;
  97.                 }
  98.                 d->nElementos++;
  99.             }
  100.         }else{
  101.             printf("\nNOME NAO ENCONTRADO.");
  102.         }
  103.     }
  104. }
  105.  
  106. void ConsultarPorNome(Descritor *d, Aluno dados, char nomeProcurado[30]){
  107.     Nodo *n;
  108.    
  109.     if(d->primeiro == NULL){
  110.         printf("\nLISTA VAZIA OU INEXISTENTE.");
  111.     }else{
  112.         n = d->primeiro;
  113.         while(n != NULL){
  114.             if(strcmp(nomeProcurado,n->dados.nome)==0){
  115.                 printf("\nCodigo: %d", n->dados.codigo);
  116.                 printf("\nNome: %s", n->dados.nome);
  117.                 printf("\nE-mail: %s", n->dados.email);
  118.                 printf("\nTelefone: %s", n->dados.telefone);
  119.                 printf("\nNota G1: %.2f", n->dados.g1);
  120.                 printf("\nNota G2: %.2f", n->dados.g2);
  121.                 break;
  122.             }
  123.             n = n->elo;
  124.         }
  125.     }
  126. }
  127.  
  128. void ExcluirInicio(Descritor *d){
  129.     Nodo *n;
  130.    
  131.     if(d->primeiro == NULL){
  132.         printf("\nLISTA VAZIA OU INEXISTENTE.");
  133.     }else{
  134.         n = d->primeiro;
  135.         d->primeiro = n->elo;
  136.         free(n);
  137.         printf("\nELEMENTO REMOVIDO COM SUCESSO!");
  138.     }
  139. }
  140.  
  141. void ExcluirFinal(Descritor *d){
  142.     Nodo *n, *ant;
  143.    
  144.     if(d->primeiro == NULL){
  145.         printf("\nLISTA VAZIA OU INEXISTENTE.");
  146.     }else{
  147.         n = d->primeiro;
  148.         while(n->elo != NULL){
  149.             ant = n;
  150.             n = n->elo;
  151.         }
  152.         if(n == d->primeiro){
  153.             d->primeiro = NULL;
  154.             d->ultimo = NULL;
  155.         }else{
  156.             ant->elo = n->elo;
  157.             d->ultimo = ant;
  158.         }
  159.         free(n);
  160.         d->nElementos--;
  161.         printf("\nELEMENTO REMOVIDO COM SUCESSO!");
  162.     }
  163. }
  164.  
  165. void ExcluirPorCodigo(Descritor *d, int codigoProcurado){
  166.     Nodo *n, *aux, *final, *ant, *prox;
  167.    
  168.     if(d->primeiro == NULL){
  169.         printf("\nLISTA VAZIA");
  170.     }else{
  171.         n = d->primeiro;
  172.         final = d->ultimo;
  173.         if(codigoProcurado == n->dados.codigo){
  174.             ExcluirFinal(d);
  175.         }else if(codigoProcurado == final->dados.codigo){
  176.             ExcluirInicio(d);
  177.         }else{
  178.             aux = d->primeiro;
  179.             while(aux != NULL){
  180.                 if(codigoProcurado == aux->dados.codigo){
  181.                     break;
  182.                 }
  183.                 ant = aux;
  184.                 aux = aux->elo;
  185.             }
  186.             prox = aux->elo;
  187.             ant->elo = prox;
  188.             free(aux);
  189.             d->nElementos--;
  190.             printf("\nELEMENTO REMOVIDO COM SUCESSO!");
  191.         }
  192.     }  
  193. }  
  194.  
  195. void ListarRegistros(Descritor *d){
  196.     Nodo *n;
  197.    
  198.     if(d->primeiro == NULL){
  199.         printf("\nLISTA VAZIA");
  200.     }else{
  201.         printf("\nLISTA:");
  202.         n = d->primeiro;
  203.         while(n != NULL){
  204.             printf("\nCodigo: %d", n->dados.codigo);
  205.             printf("\nNome: %s", n->dados.nome);
  206.             printf("\nE-mail: %s", n->dados.email);
  207.             printf("\nTelefone: %s", n->dados.telefone);
  208.             printf("\nNota G1: %.2f", n->dados.g1);
  209.             printf("\nNota G2: %.2f", n->dados.g2);
  210.             printf("\n-----------------------\n");
  211.             n = n->elo;
  212.         }
  213.     }
  214. }
  215.  
  216. void ListarRegistrosComMedia(Descritor *d){
  217.     Nodo *n;
  218.    
  219.     if(d->primeiro == NULL){
  220.         printf("\nLISTA VAZIA");
  221.     }else{
  222.         printf("\nLISTA:");
  223.         n = d->primeiro;
  224.         while(n != NULL){
  225.             printf("\nCodigo: %d", n->dados.codigo);
  226.             printf("\nNome: %s", n->dados.nome);
  227.             printf("\nE-mail: %s", n->dados.email);
  228.             printf("\nTelefone: %s", n->dados.telefone);
  229.             printf("\nNota G1: %.2f", n->dados.g1);
  230.             printf("\nNota G2: %.2f", n->dados.g2);
  231.             printf("\nMedia final: %.2f",n->dados.g1 + n->dados.g2 /2);
  232.             printf("\n-----------------------\n");
  233.             n = n->elo;
  234.         }
  235.     }
  236. }
  237.  
  238. int main()
  239. {
  240.     int op=0;
  241.     char nomeProcurado[30];
  242.     Aluno a;
  243.     Descritor d;
  244.     int codigoProcurado;
  245.    
  246.     CriaLista(&d);
  247.     while(op != 10){
  248.         printf("\n\nESCOLHA UMA OPCAO");
  249.         printf("\n[1]INCLUIR NO FINAL");
  250.         printf("\n[2]INCLUIR NO INICIO");
  251.         printf("\n[3]INCLUIR APOS UM NOME");
  252.         printf("\n[4]CONSULTAR POR NOME");
  253.         printf("\n[5]EXCLUIR DO INICIO");
  254.         printf("\n[6]EXCLUIR DO FIM");
  255.         printf("\n[7]EXCLUIR POR CODIGO");
  256.         printf("\n[8]LISTAR TODOS OS REGISTROS");
  257.         printf("\n[9]LISTAR COM MEDIAS FINAIS");
  258.         printf("\n[10]SAIR");
  259.         printf("\nOPCAO => ");
  260.         scanf("%d", &op);
  261.         system("cls");
  262.        
  263.         switch(op){
  264.             case 1:
  265.                 printf("\nDIGITE OS DADOS DO ALUNO: ");
  266.                 printf("\nCODIGO: ");
  267.                 scanf("%d",&a.codigo);
  268.                 fflush(stdin);
  269.                 printf("\nNOME: ");
  270.                 gets(a.nome);
  271.                 printf("\nEMAIL: ");
  272.                 gets(a.email);
  273.                 printf("\nTELEFONE: ");
  274.                 gets(a.telefone);
  275.                 printf("\nNOTA DA G1: ");
  276.                 scanf("%f",&a.g1);
  277.                 fflush(stdin);
  278.                 printf("\nNOTA DA G2: ");
  279.                 scanf("%f",&a.g2);
  280.                
  281.                 IncluirFinal(&d, a);
  282.                 break;
  283.                
  284.             case 2:
  285.                 printf("\nDIGITE OS DADOS DO ALUNO: ");
  286.                 printf("\nCODIGO: ");
  287.                 scanf("%d",&a.codigo);
  288.                 fflush(stdin);
  289.                 printf("\nNOME: ");
  290.                 gets(a.nome);
  291.                 printf("\nEMAIL: ");
  292.                 gets(a.email);
  293.                 printf("\nTELEFONE: ");
  294.                 gets(a.telefone);
  295.                 printf("\nNOTA DA G1: ");
  296.                 scanf("%f",&a.g1);
  297.                 fflush(stdin);
  298.                 printf("\nNOTA DA G2: ");
  299.                 scanf("%f",&a.g2);
  300.                
  301.                 IncluirInicio(&d, a);
  302.                 break;
  303.                
  304.             case 3:
  305.                 printf("\nINFORME O NOME QUE DESEJA PROCURAR: ");
  306.                 fflush(stdin);
  307.                 gets(nomeProcurado);
  308.                
  309.                 printf("\nDIGITE OS DADOS DO ALUNO: ");
  310.                 printf("\nCODIGO: ");
  311.                 scanf("%d",&a.codigo);
  312.                 fflush(stdin);
  313.                 printf("\nNOME: ");
  314.                 gets(a.nome);
  315.                 printf("\nEMAIL: ");
  316.                 gets(a.email);
  317.                 printf("\nTELEFONE: ");
  318.                 gets(a.telefone);
  319.                 printf("\nNOTA DA G1: ");
  320.                 scanf("%f",&a.g1);
  321.                 fflush(stdin);
  322.                 printf("\nNOTA DA G2: ");
  323.                 scanf("%f",&a.g2);
  324.                
  325.                 IncluirAposNome(&d, a, nomeProcurado);
  326.                 break;
  327.            
  328.             case 4:
  329.                 printf("\nINFORME O NOME QUE DESEJA PROCURAR: ");
  330.                 fflush(stdin);
  331.                 gets(nomeProcurado);
  332.                
  333.                 ConsultarPorNome(&d, a, nomeProcurado);
  334.                 break;
  335.                
  336.             case 5:
  337.                 ExcluirInicio(&d);
  338.                 break;
  339.                
  340.             case 6:
  341.                 ExcluirFinal(&d);
  342.                 break;
  343.                
  344.             case 7:
  345.                 printf("\nINFORME O CODIGO QUE DESEJA REMOVER: ");
  346.                 scanf("%d",&codigoProcurado);
  347.                
  348.                 ExcluirPorCodigo(&d, codigoProcurado);
  349.                 break;
  350.                
  351.             case 8:
  352.                 ListarRegistros(&d);
  353.                 break;
  354.                
  355.             case 9:
  356.                 ListarRegistrosComMedia(&d);
  357.                 break;
  358.                
  359.             case 10:
  360.                 printf("\nPROGRAMA FINALIZADO...");
  361.                 exit(0);
  362.                
  363.             default:
  364.                 printf("\nOPCAO INVALIDA. DIGITE NOVAMENTE:");
  365.             }
  366.     }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement