-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdimacs.c
100 lines (91 loc) · 1.99 KB
/
dimacs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "dimacs.h"
/*
compilar gcc test.c
./a.out < dimacs
*/
/**
* @brief chequea que la palabra este en forma correcta
*
* @param str toma un string
* @return devuelve un string continuando luego de edge o
* si falla NULL.
*/
char *parse_correct_form_edge(char *str)
{
char *r = NULL;
r = strtok(str, "p ");
if (strcmp(r, "edge") == 0)
{
r = strtok(NULL, " ");
return r;
}
else
{
printf("error :%s \n", r);
return NULL;
}
}
Lado_st *parse_p_edge_n_m(void)
{
char buffer[80];
char *readString = NULL;
u32 strToULong;
char *ptr = NULL;
bool flag = false;
Lado_st *Data = NULL;
readString = buffer;
Data = (Lado_st *)calloc(1, sizeof(struct Lado));
while (!flag)
{
if (fgets(readString, sizeof(buffer), stdin) == NULL)
return NULL;
if (readString[0] == 'p')
{
if ((readString = parse_correct_form_edge(readString)) == NULL)
{
return NULL;
}
strToULong = strtoul(readString, &ptr, 10);
Data->v = strToULong;
readString = strtok(NULL, " ");
strToULong = strtoul(readString, &ptr, 10);
Data->w = strToULong;
flag = true;
}
}
return Data;
}
Lado_st **parse_edge_from_dimacs(Lado_st *lados)
{
char buffer[80];
char *readStr;
char *token, *ptr;
u32 a, b, M = 0;
Lado_st **array_lados = NULL;
readStr = buffer;
M = lados->w;
array_lados = (Lado_st **)calloc(M, sizeof(Lado_st));
for (u32 i = 0; i < M; i++)
{
array_lados[i] = calloc(1, sizeof(struct Lado));
}
for (u32 i = 0; i < M; i++)
{
if (fgets(readStr, sizeof(buffer), stdin) == NULL)
return NULL;
if (readStr[0] == 'e')
{
token = strtok(readStr, "e ");
a = (unsigned int)strtoul(token, &ptr, 10);
token = strtok(NULL, " ");
b = (unsigned int)strtoul(token, &ptr, 10);
array_lados[i]->v = a;
array_lados[i]->w = b;
}
}
return array_lados;
}