Advertisement
Guest User

se liga no tamanho desse código

a guest
Mar 28th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. typedef struct vetor{
  6.  
  7. double valores[64];
  8.  
  9. char rotulo[256];
  10.  
  11. } vetor_referencia;
  12.  
  13. typedef struct noarvore{
  14.  
  15. int raiz;
  16.  
  17. double valores[64];
  18.  
  19. char rotulo[256];
  20.  
  21. struct noarvore *dir, *esq;
  22.  
  23. double distancia;
  24.  
  25. } arvore;
  26.  
  27. void distancia1(vetor_referencia *vetor, arvore *noreferencia){
  28.  
  29. double distancia = 0.0;
  30.  
  31. int i;
  32.  
  33. for(i = 0; i < 64; i++){
  34.  
  35. distancia += fabs(vetor->valores[i] - noreferencia->valores[i]);
  36. }
  37.  
  38. noreferencia->distancia = distancia;
  39.  
  40. }
  41.  
  42. void pegavetor(FILE *f, vetor_referencia *vetor){
  43.  
  44. int pos;
  45.  
  46. char c;
  47.  
  48. fscanf(f, "%s", vetor->rotulo);
  49.  
  50. while((c = fgetc(f)) == ' '){
  51.  
  52. fscanf(f, "%d", &pos);
  53.  
  54. c = fgetc(f);
  55.  
  56. fscanf(f, "%lf", &vetor->valores[pos]);
  57.  
  58. }
  59.  
  60. }
  61.  
  62. void insercao(arvore *noreferencia, arvore *no){
  63.  
  64. arvore *aux, *aux2;
  65.  
  66. aux = noreferencia;
  67.  
  68. while(aux != NULL){
  69.  
  70. aux2 = aux;
  71.  
  72. if(aux->distancia > no->distancia)
  73.  
  74. aux = aux->esq;
  75.  
  76. else
  77.  
  78. aux = aux->dir;
  79.  
  80. }
  81.  
  82. if(aux2->distancia > no->distancia)
  83.  
  84. aux2->esq = no;
  85.  
  86. else
  87.  
  88. aux2->dir = no;
  89.  
  90. }
  91.  
  92. void pegano(FILE *f, arvore *raiz, vetor_referencia *vetor){
  93.  
  94. int pos;
  95.  
  96. char c;
  97.  
  98. if(raiz->raiz == 1){
  99.  
  100. fscanf(f, "%s", raiz->rotulo);
  101.  
  102. while((c = fgetc(f)) == ' '){
  103.  
  104. fscanf(f, "%d", &pos);
  105.  
  106. c = fgetc(f);
  107.  
  108. fscanf(f, "%lf", &raiz->valores[pos]);
  109.  
  110. }
  111.  
  112. raiz->raiz = 10;
  113.  
  114. distancia1(vetor, raiz);
  115.  
  116. }
  117.  
  118. else{
  119.  
  120. arvore *no;
  121.  
  122. no = (arvore*)calloc(1, sizeof(arvore));
  123.  
  124. fscanf(f, "%s", no->rotulo);
  125.  
  126. while((c = fgetc(f)) == ' '){
  127.  
  128. fscanf(f, "%d", &pos);
  129.  
  130. c = fgetc(f);
  131.  
  132. fscanf(f, "%lf", &no->valores[pos]);
  133.  
  134. }
  135.  
  136. distancia1(vetor, no);
  137.  
  138. insercao(raiz, no);
  139.  
  140. }
  141.  
  142. }
  143.  
  144. void arv_imprimeordem (arvore *noreferencia){
  145.  
  146. if (noreferencia != NULL){
  147.  
  148. arv_imprimeordem(noreferencia->esq);
  149.  
  150. printf("%s,", noreferencia->rotulo);
  151.  
  152. arv_imprimeordem(noreferencia->dir);
  153.  
  154. }
  155.  
  156. }
  157.  
  158. void arv_imprimepre (arvore *noreferencia){
  159.  
  160. if (noreferencia != NULL){
  161.  
  162. printf("%s,", noreferencia->rotulo);
  163.  
  164. arv_imprimepre(noreferencia->esq);
  165.  
  166. arv_imprimepre(noreferencia->dir);
  167.  
  168. }
  169.  
  170. }
  171.  
  172. void arv_imprimepos (arvore *noreferencia){
  173.  
  174. if (noreferencia != NULL){
  175.  
  176. arv_imprimepos(noreferencia->esq);
  177.  
  178. arv_imprimepos(noreferencia->dir);
  179.  
  180. printf("%s,", noreferencia->rotulo);
  181.  
  182. }
  183.  
  184. }
  185.  
  186. int main(){
  187.  
  188. int qtvetores, i;
  189.  
  190. char nomedoarquivo[256];
  191.  
  192. vetor_referencia *vetor;
  193.  
  194. arvore *raiz;
  195.  
  196. vetor = (vetor_referencia*)calloc(1, sizeof(vetor_referencia));
  197.  
  198. raiz = (arvore*)calloc(1, sizeof(arvore));
  199.  
  200. raiz->dir = NULL;
  201.  
  202. raiz->esq = NULL;
  203.  
  204. raiz->raiz = 1;
  205.  
  206. scanf("%s", nomedoarquivo);
  207.  
  208. FILE *f;
  209.  
  210. f = fopen(nomedoarquivo, "r");
  211.  
  212. if(f == NULL) {
  213.  
  214. return 0;
  215. }
  216.  
  217. else{
  218.  
  219. fscanf(f, "%d", &qtvetores);
  220.  
  221. pegavetor(f, vetor);
  222.  
  223. for(i = 0; i < qtvetores - 1; i++){
  224.  
  225. pegano(f, raiz, vetor);
  226.  
  227. }
  228.  
  229. }
  230.  
  231. arv_imprimeordem(raiz);
  232.  
  233. printf("\n");
  234.  
  235. arv_imprimepre(raiz);
  236.  
  237. printf("\n");
  238.  
  239. arv_imprimepos(raiz);
  240.  
  241. fclose(f);
  242.  
  243. return 0;
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement