Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "code.h"
- int **graph;
- int countofver;
- int step, buf_b, buf_e, buf_v;
- int countoffiles;
- int *path, *flags;
- void loadfrom(){
- countoffiles = 0;
- countofver = 0;
- step = 0;
- buf_b = 0;
- buf_e = 0;
- int countrow = 0;
- FILE *file = fopen("graph.txt", "r+");
- if (file == NULL){
- printf("Cannot open this file \n");
- exit(0);
- }
- else{
- //смотрим кол-во строк| кол-во строк = кол-во вершин
- char s[1024];
- fseek(file, 0, SEEK_SET);
- while (!feof(file)){
- fgets(s, 1024, file);
- if (parse_string(s)) {
- if (buf_b + 1 > countofver)
- countofver = buf_b + 1;
- if (buf_e + 1 > countofver)
- countofver = buf_e + 1;
- }
- }
- path = (int*)malloc(countofver* sizeof(int));
- flags = (int*)malloc(countofver * sizeof(int));
- for (int i = 0; i < countofver; i++){
- path[i] = 0;
- flags[i] = 0;
- }
- graph = (int **)malloc(countofver * sizeof(int *));
- for (int i = 0; i < countofver; i++)
- graph[i] = (int *)malloc(countofver * sizeof(int));
- fseek(file, 0, SEEK_SET);
- for (int i = 0; i < countofver; i++){
- for (int j = 0; j < countofver; j++){
- graph[i][j] = 0;
- }
- }
- while (!feof(file)){
- fgets(s, 1024, file);
- if (parse_string(s)) {
- graph[buf_b][buf_e] = buf_v;
- }
- }
- fclose(file);
- }
- }
- void finder(int counter, int current){ //counter - число вершин в отрицательном цикле. current - текущая вершина
- if (graph[current][path[0]] < 0){
- path[counter] = current;
- flags[current] = 1;
- counter++;
- for (int i = 0; i < counter; i++){
- printf("%d ", path[i]);
- }
- printf("\n");
- saveto(path, counter);
- }
- else{
- path[counter] = current;
- flags[current] = 1;
- for (int i = 0; i < countofver; i++){
- if (graph[current][i] < 0 && flags[i] == 0){
- finder(counter + 1, i);
- }
- }
- }
- flags[current] = 0;
- current--;
- }
- void deepfind(int current, int counter){
- if (counter <= countofver){
- for (int i = 0; i < countofver; i++){
- if (graph[current][i] < 0){
- finder(0, current);
- }
- else if (graph[current][i] != 0)
- deepfind(i, counter + 1);
- }
- }
- }
- void saveto(int *path, int counter){
- countoffiles++;
- char numbe[10];
- itoa(countoffiles, numbe, 10);
- char name[15] = "graphviz";
- char ext[5] = ".txt";
- char *news = strcat(name, numbe);
- news = strcat(news, ext);
- FILE *file = fopen(news, "w+");
- fprintf(file, "digraph {\n");
- for (int i = 0; i < counter - 1; i++){
- fprintf(file, " %d -> %d [label = \" %d\"];\n", path[i], path[i + 1], graph[path[i]][path[i + 1]]);
- }
- fprintf(file, " %d -> %d [label = \" %d\"];\n", path[counter - 1], 0, graph[path[counter - 1]][path[0]]);
- fprintf(file, "}");
- fclose(file);
- }
- int parse_string(char *s) {
- int i = 0;
- int j;
- while (s[i] >= '0' && s[i] <= '9')
- ++i;
- if (i == 0 || s[i] == '\0')
- return 0;
- char *buffer = (char*)malloc(i + 1);
- for (j = 0; j < i; j++)
- buffer[j] = s[j];
- buffer[i] = '\0';
- buf_b = atoi(buffer);
- free(buffer);
- while ((s[i] < '0' || s[i] > '9') && s[i] != '\0')
- ++i;
- if (s[i] == '\0')
- return 0;
- int snd_begin = i;
- while (s[i] >= '0' && s[i] <= '9')
- ++i;
- if (s[i] == '\0')
- return 0;
- buffer = (char*)malloc(i - snd_begin + 1);
- for (j = snd_begin; j < i; j++)
- buffer[j - snd_begin] = s[j];
- buffer[i - snd_begin] = '\0';
- buf_e = atoi(buffer);
- free(buffer);
- while ((s[i] < '0' || s[i] > '9') && s[i] != '\0')
- ++i;
- if (s[i] == '\0')
- return 0;
- snd_begin = i;
- while (s[i] >= '0' && s[i] <= '9')
- ++i;
- if (s[i] == '\0')
- return 0;
- int coef = 1;
- if (s[i - 2] == '-' || s[i - 1] == '-'){
- coef = -1;
- }
- buffer = (char*)malloc(i - snd_begin + 1);
- for (j = snd_begin; j < i; j++)
- buffer[j - snd_begin] = s[j];
- buffer[i - snd_begin] = '\0';
- buf_v = atoi(buffer);
- buf_v *= coef;
- free(buffer);
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement