Advertisement
ParanoidPanda

code.c

Jan 8th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include "code.h"
  2.  
  3. int **graph;
  4. int countofver;
  5. int step, buf_b, buf_e, buf_v;
  6. int countoffiles;
  7. int *path, *flags;
  8.  
  9. void loadfrom(){
  10. countoffiles = 0;
  11. countofver = 0;
  12. step = 0;
  13. buf_b = 0;
  14. buf_e = 0;
  15. int countrow = 0;
  16. FILE *file = fopen("graph.txt", "r+");
  17. if (file == NULL){
  18. printf("Cannot open this file \n");
  19. exit(0);
  20. }
  21. else{
  22. //смотрим кол-во строк| кол-во строк = кол-во вершин
  23. char s[1024];
  24. fseek(file, 0, SEEK_SET);
  25. while (!feof(file)){
  26. fgets(s, 1024, file);
  27. if (parse_string(s)) {
  28. if (buf_b + 1 > countofver)
  29. countofver = buf_b + 1;
  30. if (buf_e + 1 > countofver)
  31. countofver = buf_e + 1;
  32. }
  33. }
  34. path = (int*)malloc(countofver* sizeof(int));
  35. flags = (int*)malloc(countofver * sizeof(int));
  36. for (int i = 0; i < countofver; i++){
  37. path[i] = 0;
  38. flags[i] = 0;
  39. }
  40. graph = (int **)malloc(countofver * sizeof(int *));
  41. for (int i = 0; i < countofver; i++)
  42. graph[i] = (int *)malloc(countofver * sizeof(int));
  43. fseek(file, 0, SEEK_SET);
  44. for (int i = 0; i < countofver; i++){
  45. for (int j = 0; j < countofver; j++){
  46. graph[i][j] = 0;
  47. }
  48. }
  49. while (!feof(file)){
  50. fgets(s, 1024, file);
  51. if (parse_string(s)) {
  52. graph[buf_b][buf_e] = buf_v;
  53. }
  54. }
  55. fclose(file);
  56. }
  57. }
  58.  
  59. void finder(int counter, int current){ //counter - число вершин в отрицательном цикле. current - текущая вершина
  60. if (graph[current][path[0]] < 0){
  61. path[counter] = current;
  62. flags[current] = 1;
  63. counter++;
  64. for (int i = 0; i < counter; i++){
  65. printf("%d ", path[i]);
  66. }
  67. printf("\n");
  68. saveto(path, counter);
  69. }
  70. else{
  71. path[counter] = current;
  72. flags[current] = 1;
  73. for (int i = 0; i < countofver; i++){
  74. if (graph[current][i] < 0 && flags[i] == 0){
  75. finder(counter + 1, i);
  76. }
  77. }
  78. }
  79. flags[current] = 0;
  80. current--;
  81. }
  82.  
  83. void deepfind(int current, int counter){
  84. if (counter <= countofver){
  85. for (int i = 0; i < countofver; i++){
  86. if (graph[current][i] < 0){
  87. finder(0, current);
  88. }
  89. else if (graph[current][i] != 0)
  90. deepfind(i, counter + 1);
  91. }
  92. }
  93. }
  94.  
  95. void saveto(int *path, int counter){
  96. countoffiles++;
  97. char numbe[10];
  98. itoa(countoffiles, numbe, 10);
  99. char name[15] = "graphviz";
  100. char ext[5] = ".txt";
  101. char *news = strcat(name, numbe);
  102. news = strcat(news, ext);
  103. FILE *file = fopen(news, "w+");
  104. fprintf(file, "digraph {\n");
  105. for (int i = 0; i < counter - 1; i++){
  106. fprintf(file, " %d -> %d [label = \" %d\"];\n", path[i], path[i + 1], graph[path[i]][path[i + 1]]);
  107. }
  108. fprintf(file, " %d -> %d [label = \" %d\"];\n", path[counter - 1], 0, graph[path[counter - 1]][path[0]]);
  109. fprintf(file, "}");
  110. fclose(file);
  111. }
  112.  
  113. int parse_string(char *s) {
  114. int i = 0;
  115. int j;
  116. while (s[i] >= '0' && s[i] <= '9')
  117. ++i;
  118. if (i == 0 || s[i] == '\0')
  119. return 0;
  120. char *buffer = (char*)malloc(i + 1);
  121. for (j = 0; j < i; j++)
  122. buffer[j] = s[j];
  123. buffer[i] = '\0';
  124. buf_b = atoi(buffer);
  125. free(buffer);
  126.  
  127. while ((s[i] < '0' || s[i] > '9') && s[i] != '\0')
  128. ++i;
  129. if (s[i] == '\0')
  130. return 0;
  131. int snd_begin = i;
  132. while (s[i] >= '0' && s[i] <= '9')
  133. ++i;
  134. if (s[i] == '\0')
  135. return 0;
  136. buffer = (char*)malloc(i - snd_begin + 1);
  137. for (j = snd_begin; j < i; j++)
  138. buffer[j - snd_begin] = s[j];
  139. buffer[i - snd_begin] = '\0';
  140. buf_e = atoi(buffer);
  141. free(buffer);
  142.  
  143. while ((s[i] < '0' || s[i] > '9') && s[i] != '\0')
  144. ++i;
  145. if (s[i] == '\0')
  146. return 0;
  147. snd_begin = i;
  148. while (s[i] >= '0' && s[i] <= '9')
  149. ++i;
  150. if (s[i] == '\0')
  151. return 0;
  152. int coef = 1;
  153. if (s[i - 2] == '-' || s[i - 1] == '-'){
  154. coef = -1;
  155. }
  156. buffer = (char*)malloc(i - snd_begin + 1);
  157. for (j = snd_begin; j < i; j++)
  158. buffer[j - snd_begin] = s[j];
  159. buffer[i - snd_begin] = '\0';
  160. buf_v = atoi(buffer);
  161. buf_v *= coef;
  162. free(buffer);
  163. return 1;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement