ParanoidPanda

main

Jan 9th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include "source.h"
  2.  
  3. int main(){
  4. FILE *input = fopen("graph.txt", "r");
  5. if (input == NULL)
  6. printf("Cannot open this file \n");
  7. else{
  8. roots = (struct root *)realloc((void *)roots, (edges)*sizeof(struct root));
  9. int step = 0;
  10. int target = 0;
  11. char s[1024];
  12. while (!feof(input)){
  13. fgets(s, 1024, input);
  14. if (parse_string(s)) {
  15. roots = (struct root *)realloc((void *)roots, (edges = edges + 1)*sizeof(struct root));
  16. roots[edges - 1].start = buff_b;
  17. roots[edges - 1].end = buff_e;
  18. roots[edges - 1].v = buff_w;
  19. if (roots[edges - 1].start + 1 >= dots)
  20. dots = roots[edges - 1].start + 1;
  21. if (roots[edges - 1].end + 1 >= dots)
  22. dots = roots[edges - 1].end + 1;
  23. }
  24. }
  25. fclose(input);
  26. ostgraph = (struct root*)malloc((dots - 1)*sizeof(struct root));
  27. //сортировка
  28. struct root tmp;
  29. for (int i = 0; i < edges - 1; i++){
  30. for (int j = 0; j < edges - i - 1; j++){
  31. if (roots[j].v > roots[j + 1].v){
  32. tmp.v = roots[j].v;
  33. tmp.start = roots[j].start;
  34. tmp.end = roots[j].end;
  35. roots[j].v = roots[j + 1].v;
  36. roots[j].start = roots[j + 1].start;
  37. roots[j].end = roots[j + 1].end;
  38. roots[j + 1].v = tmp.v;
  39. roots[j + 1].start = tmp.start;
  40. roots[j + 1].end = tmp.end;
  41. }
  42. }
  43. }
  44. printf("Sorted graph:\n\n");
  45. for (int i = 0; i < edges; i++){
  46.  
  47. printf("begin : %d end: %d weight: %d \n", roots[i].start, roots[i].end, roots[i].v);
  48. }
  49. //сам алгоритм
  50. target = 0;
  51. int ttarget = 0;
  52. step = 0;
  53. for (int i = 0; i < edges; i++){
  54. for (int j = 0; j < dots; j++){
  55. if (roots[i].end == ostgraph[j].end){
  56. ttarget = 1;
  57. }
  58. if (roots[i].start == ostgraph[j].start){
  59. target = 1;
  60. }
  61. }
  62. if (target == 0 || ttarget == 0){
  63. ostgraph[step].start = roots[i].start;
  64. ostgraph[step].end = roots[i].end;
  65. ostgraph[step].v = roots[i].v;
  66. step++;
  67. }
  68. if (step == dots - 1)
  69. continue;
  70. target = 0;
  71. ttarget = 0;
  72. }
  73. printf("\n\nOur ost graph: \n\n");
  74. for (int i = 0; i < dots - 1; i++){
  75. printf("begin : %d end: %d weight: %d \n", ostgraph[i].start, ostgraph[i].end, ostgraph[i].v);
  76. }
  77. //графиз
  78. FILE *inspect = fopen("graphviz.dot", "w+");
  79. fprintf(inspect, "graph G{\n");
  80. target = 1;
  81. for (int i = 0; i < dots - 1; i++){
  82. fprintf(inspect, " %d -- %d [ label = \"%d\"];\n", ostgraph[i].start, ostgraph[i].end, ostgraph[i].v);
  83. }
  84. fprintf(inspect, "}");
  85. fclose(inspect);
  86. free(ostgraph);
  87. free(graph);
  88. }
  89.  
  90. system("pause");
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment