Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- typedef struct Aluno{
- int matricula;
- char nome[30];
- int idade;
- }Aluno;
- int Buscar(Aluno L[],int n,int matricula,int *PosIns);
- void InserePosicao(Aluno L[],int *n,Aluno x,int PosIns);
- void Insere(Aluno L[],int n,Aluno vetor,int PosIns,int matricula);
- int main(void)
- {
- Aluno vetor;
- int matricula = 6;
- int n = 3;
- int PosIns = 0;
- int tamVet = 3; // simulação com vetor de 3 posições
- int busca;
- Aluno L[tamVet];
- L[0].matricula = 1; //Testando o erro "Chave Existente"
- L[1].matricula = 5; //Testando o erro "Chave Existente"
- L[2].matricula = 9; //Testando o erro "Chave Existente"
- // busca = Buscar(L,n,matricula,&PosIns);
- // InserePosicao(L,&n,vetor,PosIns);
- Insere(L,n,vetor,PosIns,matricula);
- }
- int Buscar(Aluno L[],int n,int matricula,int *PosIns){
- int sup,inf,m;
- inf = 0;
- sup = (n-1);
- // printf("INF: %d\nSUP: %d\nPOSINS: %d\n",inf,sup,*PosIns);
- while(inf<=sup){
- m = ((inf+sup)/2);
- if(L[m].matricula == matricula){
- // printf("M: %d\n",m);
- return m;
- }
- else if(L[m].matricula < matricula){
- // printf("INF: %d\n",inf);
- inf = (m+1);
- }
- else{
- sup = (m-1);
- // printf("SUP: %d\n",sup);
- }
- }
- *PosIns = inf;
- printf("POSINS: %d\n\n",*PosIns);
- return -1;
- }
- void InserePosicao(Aluno L[],int *n,Aluno x,int PosIns){
- int j;
- for(j=*n;j>=PosIns;j--){
- L[j+1] = L[j];
- }
- L[PosIns] = x;
- *n = *n + 1;
- }
- void Insere(Aluno L[],int n,Aluno vetor, int PosIns,int matricula){
- int i;
- i = Buscar(L,n,matricula,&PosIns);
- if(i == -1){
- InserePosicao(L,&n,vetor,PosIns);
- }
- else{
- printf("Chave Existente\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement