Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. typedef struct Aluno{
  4.    
  5.     int matricula;
  6.     char nome[30];
  7.     int idade;
  8.    
  9. }Aluno;
  10.  
  11. int Buscar(Aluno L[],int n,int matricula,int *PosIns);
  12. void InserePosicao(Aluno L[],int *n,Aluno x,int PosIns);
  13. void Insere(Aluno L[],int n,Aluno vetor,int PosIns,int matricula);
  14.  
  15.  
  16. int main(void)
  17. {
  18.     Aluno vetor;
  19.     int matricula = 6;
  20.     int n = 3;
  21.     int PosIns = 0;
  22.     int tamVet = 3; // simulação com vetor de 3 posições
  23.     int busca;
  24.     Aluno L[tamVet];
  25.     L[0].matricula = 1; //Testando o erro "Chave Existente"
  26.     L[1].matricula = 5; //Testando o erro "Chave Existente"
  27.     L[2].matricula = 9; //Testando o erro "Chave Existente"
  28. //  busca = Buscar(L,n,matricula,&PosIns);
  29. //  InserePosicao(L,&n,vetor,PosIns);
  30.     Insere(L,n,vetor,PosIns,matricula);
  31. }
  32.  
  33.  
  34. int Buscar(Aluno L[],int n,int matricula,int *PosIns){
  35.     int sup,inf,m;
  36.     inf = 0;
  37.     sup = (n-1);
  38. //  printf("INF: %d\nSUP: %d\nPOSINS: %d\n",inf,sup,*PosIns);
  39.     while(inf<=sup){
  40.         m = ((inf+sup)/2);
  41.         if(L[m].matricula == matricula){
  42.     //      printf("M: %d\n",m);
  43.             return m;  
  44.         }
  45.         else if(L[m].matricula < matricula){
  46.     //      printf("INF: %d\n",inf);
  47.             inf = (m+1);   
  48.         }
  49.         else{
  50.             sup = (m-1);
  51.     //      printf("SUP: %d\n",sup);   
  52.         }
  53.     }
  54.     *PosIns = inf;
  55.     printf("POSINS: %d\n\n",*PosIns);
  56.     return -1;
  57. }
  58.  
  59. void InserePosicao(Aluno L[],int *n,Aluno x,int PosIns){
  60.     int j;
  61.     for(j=*n;j>=PosIns;j--){
  62.         L[j+1] = L[j];
  63.     }
  64.     L[PosIns] = x;
  65.     *n = *n + 1;       
  66. }
  67.  
  68. void Insere(Aluno L[],int n,Aluno vetor, int PosIns,int matricula){
  69.     int i;
  70.     i = Buscar(L,n,matricula,&PosIns);
  71.     if(i == -1){
  72.         InserePosicao(L,&n,vetor,PosIns);
  73.     }
  74.     else{
  75.         printf("Chave Existente\n");   
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement