Alfoli

prog2

Sep 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define TF 50
  6. struct indice{
  7. int chave, posicao;
  8. }
  9. struct funcionario{
  10. int matricula;
  11. char nome[10];
  12. float salario;
  13. char depto[10];
  14. }
  15. class Indexado{
  16. struct indice vetIndice[TF];
  17. int tl;
  18. Indexado();
  19. void criaIndice();
  20. funcionario leFuncionario();
  21. void exibe (struct funcionario f);
  22. void insere();
  23. 1 int BuscaBinIndice(int, num);
  24. void exibeIndice();
  25. void relatorioDesord();
  26. void relatorioOrd();
  27. void BuscaIndexadaArquivo(int, num);
  28. };
  29. Indexado::Indexado(){
  30. tl=0;
  31. int i;
  32. for(i=0;i<TF;i++){
  33. vetIndice[i].chave=0;
  34. vetIndice[i].posicao=1;
  35. }
  36. }
  37. funcionario leFuncionario(){
  38. struct funcionario f;
  39. printf("\nEntre com a matricula: ");
  40. scanf("%d", &f.matricula);
  41. printf("\nEntre com o nome: ");
  42. fflush (stdlim);
  43. gets(f.nome);
  44. printf("\nEntre com salario: ");
  45. scanf("%f", &f.salario);
  46. fflush (stdlim);
  47. gets (f.dpto);
  48. return (f);
  49. }
  50. void Indexado::exibe(struct funcionario f){
  51. printf("\n----------------------\n");
  52. printf("\nMatricula: %d ", f.matricula);
  53. printf("\nNome: %s ", f.nome);
  54. printf("\nSalario: $%5.2f ", f.salario);
  55. printf("\nDepto %s", f.depto);
  56. printf("\n----------------------\n");
  57. }
  58. void Indexado::exibeIndice(){
  59. int i;
  60. printf("\n TL= %d", tl);
  61. for (int i=0; i<tl; i++)
  62. printf("\ni=%d, Chave=%d, posicao= %d", i, vetIndice[i].chave, vetIndice[i].posicao);
  63. }
  64. void Indexado:criaIndice(){
  65. struct funcionario f;
  66. FILE *arq=fopen("funcionario.dat". "r");
  67. if (arq==NULL){
  68. printf("\nERRO - Arquivo Inexistente.");
  69. tl=0;
  70. }
  71. else{
  72. tl=0;
  73. fread(&f, sizeof(funcionario), 1, arq);
  74. while(!feof(arq)){ //provavel erro "feof"
  75. vetIndice[tl].chave = f.matricula;
  76. vetIndice[tl].posicao = tl;
  77. tl++;
  78. fread(&f, sizeof(funcionario, 1, arq);
  79. }
  80. fclose(arq);
  81. printf("\n Leu o arquivo e criou o indice desordenado");
  82. indice aux;
  83. int fim, j;
  84. for(fim=tl; fim>0; fim--){
  85. for (j=0; j<fim; j++){
  86. if (vetIndice[j].chave>vetIndice[j+1].chave){
  87. aux = vetIndice[j];
  88. vetIndice[j] = vetIndice[j+1];
  89. vetIndice[j+1]=aux;
  90. }
  91. }
  92. }
  93. printf("\n Valor de indice ordenado.");
  94. }
  95. void Indexado::relatorioDesord(){
  96. struct funcionario.f;
  97. FILE *arq = fopen("funcionario.dat", "r");
  98. fread (&f, sizeof(funcionario), 1, arq);
  99. while (!feof(arq)){//provavel erro feof
  100. exibe (f);
  101. fread(&f, sizeof(funcionario), 1, arq);
  102. }
  103. fclose(arq);
  104. }
  105. void Indexado::relatorioOrd(){
  106. int i;
  107. struct funcionario f; int endereco;
  108. FILE *arq fopen ("\funcionario.dat", "r");
  109. printf("\n TL= &d, relatorio ordenado.", tl);
  110. for (i=0;i<tl;i++){
  111. endereco = vetIndice[i].posicao;
  112. fseek(arq, endereco* sizeof(funcionario), SEEK_SET);
  113. fread(%f), sizeof (funcionario), 1, arq;
  114. exibe(f);
  115. }
  116. fclose(arq);
  117. }
  118. void Indexado::insere(){
  119. struct funcionario f;
  120. printf ("\nEntre com novo funcionario: ");
  121. f=leFuncionario();
  122. FILE *arq = fopen ("funcionario.dat", "ab");
  123. fseek(arq, tl, *sizeof(funcionario, SEEK_END);
  124. fwrite(&f, sizeof(funcionario), 1, arq);
  125. if (tl==0)
  126. posicaoideal=0;
  127. else posicaoideal = BuscaBinIndice(f.matricula);
  128. for (i=tl; i>posicaoideal; i--)
  129. vetIndice[i]=vetIndice [i-1];
  130. vetIndice[posicaoideal].chave=f.matricula;
  131. vetIndice[posicaoideal].posicao=tl;
  132. tl++;
  133. fclose(arq);
  134. }
  135. void Indexado::BuscaBinIndice(int num){
  136. int meio, inicio, fim;
  137. inicio = 0;
  138. fim=tl-1;
  139. meio=(inicio+fim)/2;
  140. while((inicio<fim)&&(vetIndice[meio].chave!=num)){
  141. if (num > vetIndice[meio].chave)
  142. inicio = meio+1;
  143. else fim = meio;
  144. meio = (inicio + fim)/2;
  145. }
  146. if (num> vetIndice[meio].chave)
  147. return (meio+1);
  148. else return (meio);
  149. }
  150. void Indexado::BuscaIndexodoArquivo(int num){
  151.  
  152. }
  153. main(){
  154. Indexado i= Indexado();
  155. int opcao = 0;
  156. int num;
  157. while (opcao !=7){
  158. printf("\n1- Cria o indice.");
  159. printf("\n2- Exibe relatorio desordenado");
  160. printf("\n3- Exibe vetor de indice");
  161. printf("\n4-Insere novo funcionario");
  162. printf("\n5- Exibe relatorio ordenado");
  163. printf("\n6- Localiza um funcionario a partir da sua matricula.");
  164. printf("\n7- Sair.\nOpcao? ");
  165. scanf("%d")
  166. }
  167. }
Add Comment
Please, Sign In to add comment