Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define TF 50
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<ctype.h>
- #include<iostream>
- #include<stdbool.h>
- struct indice // vetor de indice, que contem chave e posicao do cliente (ordenado)
- {
- int chave, posicao;
- };
- struct funcionario // arquivos de dados, que contem matricula, nome, salario e departamento (desordenado)
- {
- int matricula;
- char nome[10];
- float salario;
- char depto[10];
- };
- class Indexado
- {
- public:
- struct indice vetIndice[TF];
- int tl;
- Indexado();
- void criaIndice();
- funcionario leFuncionario();
- void exibe(struct funcionario f);
- void insere();
- int BuscaBinIndice(int num);
- void exibeIndice();
- void relatorioDesord();
- void relatorioOrd();
- void BuscaIndexadoArq(int num);
- };
- Indexado::Indexado()
- {
- tl=0;
- int i;
- for(i=0; i<TF; i++)
- {
- vetIndice[i].chave=0;
- vetIndice[i].posicao=-1;
- }
- }
- funcionario Indexado::leFuncionario() // leiura de um unico funcionario
- {
- struct funcionario f;
- printf("Entre com a matricula: \n");
- scanf("%d", &f.matricula);
- printf("Entre com o nome: \n");
- fflush(stdin);
- gets(f.nome);
- printf("Entre com o salario: \n");
- scanf("&f", &f.salario);
- printf("Entre com o departamento: \n");
- fflush(stdin);
- gets(f.depto);
- return(f);
- }
- void Indexado::exibeIndice()
- {
- int i;
- printf("TL= %d\n", tl);
- for(i=0; i<tl; i++)
- printf("i= %d chave= %d posicao= %d\n", i, vetIndice[i].chave, vetIndice[i].posicao);
- }
- void Indexado::exibe(struct funcionario f)
- {
- printf("-_-_-_-_-_-\n");
- printf("Matricula= %d\n", f.matricula);
- printf("Nome= %d\n", f.nome);
- printf("Salario= $%2f\n", f.salario);
- printf("Depto= %s\n", f.depto);
- printf("_-_-_-_-_-_\n");
- }
- void Indexado::criaIndice()
- {
- struct funcionario f;
- FILE *arq= fopen("funcionario.dat", "r");
- if(arq==NULL)
- {
- printf("Erro: arquivo inexistente\n");
- tl=0;
- }
- else
- {
- tl=0;
- fread(&f, sizeof(funcionario), 1, arq);
- while(!feof(arq))
- {
- vetIndice[tl].chave= f.matricula;
- vetIndice[tl].posicao= tl;
- tl++;
- fread(&f, sizeof(funcionario), 1, arq);
- }
- fclose(arq);
- printf("Leu o arquivo e criou o indice desordenado\n");
- indice aux;
- int fim, j;
- for(fim=tl; fim>0; fim--)
- for(j=0; j<fim; j++)
- {
- if(vetIndice[j].chave > vetIndice[j+1].chave)
- {
- aux= vetIndice[j];
- vetIndice[j]= vetIndice[j+1];
- vetIndice[j+1]= aux;
- }
- }
- printf("Vetor de indice ordenado\n");
- }
- int Indexado::BuscaBinIndice(int num)
- {
- int inicio= 0, fim= tl-1, meio= (inicio+fim)/2;
- while((inicio<fim)&&(vetIndice[meio].chave!=num))
- {
- if(num > vetIndice[meio].chave)
- inicio= meio+1;
- else
- fim= meio;
- meio= (inicio+fim)/2;
- }
- if(num > vetIndice[meio].chave)
- return(meio+1);
- else
- return(meio);
- }
- void Indexado::BuscaIndexadoArq(int num)
- void Indexado::relatorioDesord()
- {
- struct funcionario f;
- FILE *arq= fopen("funcionario.dat", "r");
- fread(&f, sizeof(funcionario), 1, arq);
- while(!feof(arq))
- {
- exibe(f);
- fread(&f, sizeof(funcionario), 1, arq);
- }
- fclose(arq);
- }
- void Indexado::relatorioOrd()
- {
- int i, endereco;
- struct funcionario f;
- FILE *arq= fopen("funcionario.dat","r");
- printf("TL= %d relatorio ordenado\n");
- for(i=0; i<tl; i++)
- {
- endereco= vetIndice[i].posicao;
- fseek(arq, endereco*sizeof(funcionario), SEEK_SET);
- fread(&f, sizeof(funcionario), 1, arq);
- exibe(f);
- }
- fclose(arq);
- }
- main()
- {
- Indexado i= Indexado();
- int opcao=0;
- int num;
- while(opcao!=7)
- {
- printf("1- Cria o indice\n");
- printf("2- Exibe relatorio desordenado\n");
- printf("3- Exibe vetor de indice\n");
- printf("4- Insere novo funcionario\n");
- printf("5- Exibe relatorio ordenado\n");
- printf("6- Localiza um funcionario a partir de sua matricula\n");
- printf("7- Sair\n");
- printf("Entre com sua opcao: \n");
- scanf("%d", &opcao);
- switch(opcao)
- {
- case 1: i.criaIndice();
- break;
- case 2: i.relatorioDesord();
- break;
- case 3: i.exibeIndice();
- break;
- case 4: i.insere();
- break;
- case 5: i.relatorioOrd();
- break;
- case 6: printf("Entre com a matricula: \n");
- scanf("%d", &num);
- i.BuscaIndexadoArq(num)funcionario;
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement