Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- #define TF 50
- struct indice{
- int chave, posicao;
- }
- struct funcionario{
- int matricula;
- char nome[10];
- float salario;
- char depto[10];
- }
- class Indexado{
- struct indice vetIndice[TF];
- int tl;
- Indexado();
- void criaIndice();
- funcionario leFuncionario();
- void exibe (struct funcionario f);
- void insere();
- 1 int BuscaBinIndice(int, num);
- void exibeIndice();
- void relatorioDesord();
- void relatorioOrd();
- void BuscaIndexadaArquivo(int, num);
- };
- Indexado::Indexado(){
- tl=0;
- int i;
- for(i=0;i<TF;i++){
- vetIndice[i].chave=0;
- vetIndice[i].posicao=1;
- }
- }
- funcionario leFuncionario(){
- struct funcionario f;
- printf("\nEntre com a matricula: ");
- scanf("%d", &f.matricula);
- printf("\nEntre com o nome: ");
- fflush (stdlim);
- gets(f.nome);
- printf("\nEntre com salario: ");
- scanf("%f", &f.salario);
- fflush (stdlim);
- gets (f.dpto);
- return (f);
- }
- void Indexado::exibe(struct funcionario f){
- printf("\n----------------------\n");
- printf("\nMatricula: %d ", f.matricula);
- printf("\nNome: %s ", f.nome);
- printf("\nSalario: $%5.2f ", f.salario);
- printf("\nDepto %s", f.depto);
- printf("\n----------------------\n");
- }
- void Indexado::exibeIndice(){
- int i;
- printf("\n TL= %d", tl);
- for (int i=0; i<tl; i++)
- printf("\ni=%d, Chave=%d, posicao= %d", i, vetIndice[i].chave, vetIndice[i].posicao);
- }
- void Indexado:criaIndice(){
- struct funcionario f;
- FILE *arq=fopen("funcionario.dat". "r");
- if (arq==NULL){
- printf("\nERRO - Arquivo Inexistente.");
- tl=0;
- }
- else{
- tl=0;
- fread(&f, sizeof(funcionario), 1, arq);
- while(!feof(arq)){ //provavel erro "feof"
- vetIndice[tl].chave = f.matricula;
- vetIndice[tl].posicao = tl;
- tl++;
- fread(&f, sizeof(funcionario, 1, arq);
- }
- fclose(arq);
- printf("\n Leu o arquivo e criou o indice desordenado");
- 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("\n Valor de indice ordenado.");
- }
- void Indexado::relatorioDesord(){
- struct funcionario.f;
- FILE *arq = fopen("funcionario.dat", "r");
- fread (&f, sizeof(funcionario), 1, arq);
- while (!feof(arq)){//provavel erro feof
- exibe (f);
- fread(&f, sizeof(funcionario), 1, arq);
- }
- fclose(arq);
- }
- void Indexado::relatorioOrd(){
- int i;
- struct funcionario f; int endereco;
- FILE *arq fopen ("\funcionario.dat", "r");
- printf("\n TL= &d, relatorio ordenado.", tl);
- 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);
- }
- void Indexado::insere(){
- struct funcionario f;
- printf ("\nEntre com novo funcionario: ");
- f=leFuncionario();
- FILE *arq = fopen ("funcionario.dat", "ab");
- fseek(arq, tl, *sizeof(funcionario, SEEK_END);
- fwrite(&f, sizeof(funcionario), 1, arq);
- if (tl==0)
- posicaoideal=0;
- else posicaoideal = BuscaBinIndice(f.matricula);
- for (i=tl; i>posicaoideal; i--)
- vetIndice[i]=vetIndice [i-1];
- vetIndice[posicaoideal].chave=f.matricula;
- vetIndice[posicaoideal].posicao=tl;
- tl++;
- fclose(arq);
- }
- void Indexado::BuscaBinIndice(int num){
- int meio, inicio, fim;
- 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::BuscaIndexodoArquivo(int num){
- }
- main(){
- Indexado i= Indexado();
- int opcao = 0;
- int num;
- while (opcao !=7){
- printf("\n1- Cria o indice.");
- printf("\n2- Exibe relatorio desordenado");
- printf("\n3- Exibe vetor de indice");
- printf("\n4-Insere novo funcionario");
- printf("\n5- Exibe relatorio ordenado");
- printf("\n6- Localiza um funcionario a partir da sua matricula.");
- printf("\n7- Sair.\nOpcao? ");
- scanf("%d")
- }
- }
Add Comment
Please, Sign In to add comment