Advertisement
Guest User

boraembora

a guest
Sep 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. #define TF 50
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<ctype.h>
  6. #include<iostream>
  7. #include<stdbool.h>
  8.  
  9. struct indice // vetor de indice, que contem chave e posicao do cliente (ordenado)
  10. {
  11. int chave, posicao;
  12. };
  13.  
  14. struct funcionario // arquivos de dados, que contem matricula, nome, salario e departamento (desordenado)
  15. {
  16. int matricula;
  17. char nome[10];
  18. float salario;
  19. char depto[10];
  20. };
  21.  
  22. class Indexado
  23. {
  24. public:
  25.  
  26. struct indice vetIndice[TF];
  27. int tl;
  28. Indexado();
  29. void criaIndice();
  30. funcionario leFuncionario();
  31. void exibe(struct funcionario f);
  32. void insere();
  33. int BuscaBinIndice(int num);
  34. void exibeIndice();
  35. void relatorioDesord();
  36. void relatorioOrd();
  37. void BuscaIndexadoArq(int num);
  38. };
  39.  
  40. Indexado::Indexado()
  41. {
  42. tl=0;
  43. int i;
  44. for(i=0; i<TF; i++)
  45. {
  46. vetIndice[i].chave=0;
  47. vetIndice[i].posicao=-1;
  48. }
  49. }
  50.  
  51. funcionario Indexado::leFuncionario() // leiura de um unico funcionario
  52. {
  53. struct funcionario f;
  54. printf("Entre com a matricula: \n");
  55. scanf("%d", &f.matricula);
  56. printf("Entre com o nome: \n");
  57. fflush(stdin);
  58. gets(f.nome);
  59. printf("Entre com o salario: \n");
  60. scanf("&f", &f.salario);
  61. printf("Entre com o departamento: \n");
  62. fflush(stdin);
  63. gets(f.depto);
  64. return(f);
  65. }
  66.  
  67. void Indexado::exibeIndice()
  68. {
  69. int i;
  70. printf("TL= %d\n", tl);
  71. for(i=0; i<tl; i++)
  72. printf("i= %d chave= %d posicao= %d\n", i, vetIndice[i].chave, vetIndice[i].posicao);
  73. }
  74.  
  75. void Indexado::exibe(struct funcionario f)
  76. {
  77. printf("-_-_-_-_-_-\n");
  78. printf("Matricula= %d\n", f.matricula);
  79. printf("Nome= %d\n", f.nome);
  80. printf("Salario= $%2f\n", f.salario);
  81. printf("Depto= %s\n", f.depto);
  82. printf("_-_-_-_-_-_\n");
  83. }
  84.  
  85. void Indexado::criaIndice()
  86. {
  87. struct funcionario f;
  88. FILE *arq= fopen("funcionario.dat", "r");
  89. if(arq==NULL)
  90. {
  91. printf("Erro: arquivo inexistente\n");
  92. tl=0;
  93. }
  94. else
  95. {
  96. tl=0;
  97. fread(&f, sizeof(funcionario), 1, arq);
  98. while(!feof(arq))
  99. {
  100. vetIndice[tl].chave= f.matricula;
  101. vetIndice[tl].posicao= tl;
  102. tl++;
  103. fread(&f, sizeof(funcionario), 1, arq);
  104. }
  105. fclose(arq);
  106. printf("Leu o arquivo e criou o indice desordenado\n");
  107. indice aux;
  108. int fim, j;
  109. for(fim=tl; fim>0; fim--)
  110. for(j=0; j<fim; j++)
  111. {
  112. if(vetIndice[j].chave > vetIndice[j+1].chave)
  113. {
  114. aux= vetIndice[j];
  115. vetIndice[j]= vetIndice[j+1];
  116. vetIndice[j+1]= aux;
  117. }
  118. }
  119. printf("Vetor de indice ordenado\n");
  120. }
  121.  
  122. int Indexado::BuscaBinIndice(int num)
  123. {
  124. int inicio= 0, fim= tl-1, meio= (inicio+fim)/2;
  125. while((inicio<fim)&&(vetIndice[meio].chave!=num))
  126. {
  127. if(num > vetIndice[meio].chave)
  128. inicio= meio+1;
  129. else
  130. fim= meio;
  131. meio= (inicio+fim)/2;
  132. }
  133. if(num > vetIndice[meio].chave)
  134. return(meio+1);
  135. else
  136. return(meio);
  137. }
  138.  
  139. void Indexado::BuscaIndexadoArq(int num)
  140.  
  141.  
  142. void Indexado::relatorioDesord()
  143. {
  144. struct funcionario f;
  145. FILE *arq= fopen("funcionario.dat", "r");
  146. fread(&f, sizeof(funcionario), 1, arq);
  147. while(!feof(arq))
  148. {
  149. exibe(f);
  150. fread(&f, sizeof(funcionario), 1, arq);
  151. }
  152. fclose(arq);
  153. }
  154.  
  155. void Indexado::relatorioOrd()
  156. {
  157. int i, endereco;
  158. struct funcionario f;
  159. FILE *arq= fopen("funcionario.dat","r");
  160. printf("TL= %d relatorio ordenado\n");
  161. for(i=0; i<tl; i++)
  162. {
  163. endereco= vetIndice[i].posicao;
  164. fseek(arq, endereco*sizeof(funcionario), SEEK_SET);
  165. fread(&f, sizeof(funcionario), 1, arq);
  166. exibe(f);
  167. }
  168. fclose(arq);
  169. }
  170.  
  171. main()
  172. {
  173. Indexado i= Indexado();
  174. int opcao=0;
  175. int num;
  176. while(opcao!=7)
  177. {
  178. printf("1- Cria o indice\n");
  179. printf("2- Exibe relatorio desordenado\n");
  180. printf("3- Exibe vetor de indice\n");
  181. printf("4- Insere novo funcionario\n");
  182. printf("5- Exibe relatorio ordenado\n");
  183. printf("6- Localiza um funcionario a partir de sua matricula\n");
  184. printf("7- Sair\n");
  185. printf("Entre com sua opcao: \n");
  186. scanf("%d", &opcao);
  187. switch(opcao)
  188. {
  189. case 1: i.criaIndice();
  190. break;
  191. case 2: i.relatorioDesord();
  192. break;
  193. case 3: i.exibeIndice();
  194. break;
  195. case 4: i.insere();
  196. break;
  197. case 5: i.relatorioOrd();
  198. break;
  199. case 6: printf("Entre com a matricula: \n");
  200. scanf("%d", &num);
  201. i.BuscaIndexadoArq(num)funcionario;
  202. break;
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement