ParanoidPanda

header.c

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