Advertisement
Guest User

PROG2

a guest
Jan 16th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. /////////////////MAIN_CORR.C///////////////////
  2.  
  3.  
  4. #include <stdio.h>
  5. #include "fgraph.h"
  6.  
  7. void print_fgraph(struct fgraph *G);
  8.  
  9. int main(int argc, char *argv[]) {
  10. struct fgraph *G;
  11.  
  12. if(argc != 2) {
  13. fprintf(stderr,"Usage: fgraph <file>\n");
  14. return 1;
  15. }
  16. if((G = read_fgraph(argv[1])) == NULL) {
  17. fprintf(stderr,"Error reading %s\n",argv[1]);
  18. return 1;
  19.  
  20. }
  21.  
  22. print_fgraph(G);
  23. system("PAUSE");
  24. return 0;
  25. }
  26. /////////////////////////////////////////////////////
  27.  
  28.  
  29. ///////////////////////FGRAPH:C///////////////////////////////
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "fgraph.h"
  33.  
  34. #define N 1000
  35.  
  36. struct fgraph *read_fgraph(char *filename) {
  37. FILE *in = fopen(filename,"r");
  38. struct fgraph *G = NULL;
  39. unsigned int i, n, m;
  40.  
  41. if(in != NULL && fscanf(in,"%u %u\n",&n,&m) != EOF) {
  42. unsigned int id, id1, id2;
  43. char name[N];
  44.  
  45. for(i = 0; i < n && fscanf(in,"%u",&id) != EOF && fgets(name,N,in) != NULL; i++) {
  46. name[strlen(name)-1]='\0';
  47. insert_individual(&G,strcpy(calloc(strlen(name)+1,sizeof(char)),name),id);
  48. }
  49.  
  50. for(i = 0; i < m && fscanf(in,"%u %u",&id1,&id2) != EOF; i++)
  51. insert_friendship(G,id1,id2);
  52.  
  53. fclose(in);
  54. }
  55.  
  56. return G;
  57. }
  58.  
  59. static struct fgraph * individual_alloc(char *name, unsigned int id)
  60. {
  61. struct fgraph *tmp = (struct fgraph *)calloc(1, sizeof(struct fgraph));
  62.  
  63. if (tmp != NULL) {
  64. tmp->friends = NULL;
  65. tmp->id = id;
  66. tmp->name = name;
  67. tmp->next = NULL;
  68. tmp->prev = NULL;
  69. }
  70. return tmp;
  71. }
  72.  
  73. static struct list * friend_alloc(unsigned int id)
  74. {
  75. struct list *tmp = (struct list *)calloc(1, sizeof(struct list));
  76.  
  77. if (tmp != NULL) {
  78. tmp->id = id;
  79. tmp->next = NULL;
  80. tmp->prev = NULL;
  81. }
  82. return tmp;
  83. }
  84.  
  85.  
  86. static int in_inlist(struct fgraph **G, unsigned int id)
  87. {
  88. if ((G == NULL) || (*G == NULL)) {
  89. return 0;
  90. }
  91. else {
  92. struct fgraph *tmp = *G;
  93. while (tmp != NULL && tmp->id != id)
  94. tmp = tmp->next;
  95. return tmp != NULL;
  96. }
  97. }
  98.  
  99. static int in_inlist_friend(struct fgraph *G, unsigned int id)
  100. {
  101. struct list *tmp = (*G).friends;
  102. while (tmp != NULL && tmp->id != id)
  103. tmp = tmp->next;
  104. return tmp != NULL;
  105. }
  106.  
  107. static struct list * body_search(struct fgraph *G, unsigned int id) {
  108. while (id != G->id && G != NULL)
  109. G = G->next;
  110. return G;
  111.  
  112. }
  113. int insert_individual(struct fgraph **G, char *name, unsigned int id) {
  114.  
  115. struct fgraph *tmp = individual_alloc(name, id);
  116.  
  117. if ((G == NULL) || (*G == NULL))
  118. {
  119. *G = tmp;
  120. return *G == NULL;
  121. }
  122. else
  123. {
  124. if (!in_inlist(&(*G), id))
  125. {
  126. if (tmp != NULL) {
  127. tmp->next = *G;
  128. (*G)->prev = tmp;
  129. *G = tmp;
  130. }
  131. return tmp == NULL;
  132. }
  133. else
  134. return 0;
  135. }
  136. }
  137.  
  138. int insert_friendship(struct fgraph *G, unsigned int id1, unsigned int id2) {
  139.  
  140. if (in_inlist(&G, id1) && in_inlist(&G, id2))
  141. {
  142. if (!in_inlist_friend(G, id1) || !in_inlist_friend(G, id2))
  143. {
  144. struct fgraph *p1 = body_search(G, id1);
  145. struct fgraph *p2 = body_search(G, id2);
  146. struct list *tmp1 = friend_alloc(id1);
  147. struct list *tmp2 = friend_alloc(id2);
  148.  
  149. if (p1->friends == NULL)
  150. p1->friends = tmp2;
  151. else
  152. {
  153. //p to p per scorrere la lista
  154. struct list *ciclo1 = (*p1).friends;
  155. while (ciclo1->next != NULL)
  156. ciclo1 = ciclo1->next;
  157.  
  158. ciclo1->next = tmp2;
  159. (ciclo1->next)->prev = ciclo1;
  160. }
  161.  
  162.  
  163.  
  164. if (p2->friends == NULL)
  165. p2->friends = tmp1;
  166. else
  167. {
  168. //p to p per scorrere la lista
  169. struct list *ciclo2 = (*p2).friends;
  170. while (ciclo2->next != NULL)
  171. ciclo2 = ciclo2->next;
  172.  
  173. ciclo2->next = tmp1;
  174. (ciclo2->next)->prev = ciclo2;
  175. }
  176. return 0;
  177. }
  178. else
  179. return 0;
  180.  
  181. }
  182.  
  183. return 1;
  184. }
  185. //////////////////END//////////////////////
  186. ////////////////////////FGRAPH:H/////////////////////
  187. #ifndef FGRAPH_H
  188. #define FGRAPH_H
  189.  
  190. struct list {
  191. unsigned int id;
  192. struct list *next;
  193. struct list *prev;
  194. };
  195.  
  196. struct fgraph {
  197. char *name;
  198. unsigned int id;
  199. struct list *friends;
  200. struct fgraph *next;
  201. struct fgraph *prev;
  202. };
  203.  
  204. struct fgraph *read_fgraph(char *filename);
  205.  
  206. int insert_individual(struct fgraph **G, char *name, unsigned int id);
  207.  
  208. int insert_friendship(struct fgraph *G, unsigned int id1, unsigned int id2);
  209.  
  210.  
  211.  
  212. #endif
  213. ////////////////////////END////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement