c - 如何迭代获取特定的结构元素并将它们存储到 C 中的二维数组中?

标签 c string struct dijkstra adjacency-matrix

所以我有一个二维字符串数组,如下所示:

char str[12][100] = {
    "a = 2.b, 1.d",
    "b = 2.a, 1.e, 2.c",
    "c = 2.b, 1.f",
    "d = 1.a, 1.g",
    "e = 1.h, 1.b",
    "f = 1.i, 1.c",
    "g = 1.j, 1.d",
    "h = 1.k, 1.e",
    "i = 1.l, 1.f",
    "j = 1.g, 2.k",
    "k = 2.j, 1.h, 2.l",
    "l = 2.k, 1.i"
};

这些字符串表示 map 布局,其中点“a”连接到点“b”和“d”,并且连接到点“a”的点之间存在关联的距离(或权重)。以下是从所有这些字符串转换后的布局:

    a--(2)--b--(2)--c
    |       |       |
   (1)     (1)     (1)
    |       |       |
    d       e       f
    |       |       |
   (1)     (1)     (1)
    |       |       |
    g       h       i
    |       |       |
   (1)     (1)     (1)
    |       |       |
    j--(2)--k--(2)--l

我有一个结构,像这样:

struct stopPoints {
    int  weights[10];
    char connectingPoints[10];
};

我已成功获取每个字符串,并将每个字母和数字放入其自己的结构中。为此,我创建了一个结构数组,如下所示:struct stopPoints store[26];,然后我继续通过添加每个字符串的适当元素来迭代填充每个结构。例如,对于我的第一个字符串 "a = 2.b, 1.d" ,我将字母“a”、“b”和“d”放入 store[0].connectingPoints [0]、store[0].connectingPoints[1] 和 store[0].connectingPoints[2],。所以像这样:

store[0].connectingPoints[0] = 'a';
store[0].connectingPoints[1] = 'b';
store[0].connectingPoints[2] = 'd';

我还将这两个数字放入结构的“权重”元素中,如下所示:

store[0].weights[0] = 2;
store[0].weights[1] = 1;

我已经对所有 12 个字符串进行了测试,一切就绪。

现在,我想用我拥有的这些字符串创建一个邻接矩阵。邻接矩阵应该如下所示:

0 2 0 1 0 0 0 0 0 0 0 0                                        
2 0 2 0 1 0 0 0 0 0 0 0                                        
0 2 0 0 0 1 0 0 0 0 0 0                                        
1 0 0 0 0 0 1 0 0 0 0 0                                        
0 1 0 0 0 0 0 1 0 0 0 0                                        
0 0 1 0 0 0 0 0 1 0 0 0                                        
0 0 0 1 0 0 0 0 0 1 0 0                                        
0 0 0 0 1 0 0 0 0 0 1 0                                        
0 0 0 0 0 1 0 0 0 0 0 1                                        
0 0 0 0 0 0 1 0 0 0 2 0                                        
0 0 0 0 0 0 0 1 0 2 0 2                                        
0 0 0 0 0 0 0 0 1 0 2 0

再解释一下,第一行 [0](表示点“a”)在 [0][1] 索引(表示到点“b”的距离)中包含 2,在 [0] 中包含 1 ][3] 索引(表示到点“d”的距离)。

因此,第二行 [1] 在 [1][0] 和 [1][2] 索引中包含一个 2,因为点 b 连接到点 a 和点 c 以及索引 [1][5]包含 a 1 因为点 b 也连接到点 e。如您所见,每一行和每一列实际上按顺序代表字母 a-l(0=a、1=b、2=c 等)。

我已经初始化并填充了一个全 0 的 12x12 数组。我似乎无法将我的权重值正确填充到相应的索引中。这是我许多失败的方法之一:

int row2, col2, ugh=1;        
for (row2 = 0; row2 < 12; row2++){
    for (col2 = 0; col2 < 12; col2++){
        while(store[row2].connectingPoints[ugh] != NULL){
            adjmatrix[row2][col2] = store[row2].weights[col2];
            ugh++;
        }
}

以下是将前两个字符串硬编码到邻接矩阵中的方法:

        //a=2b, 1d;
        adjmatrix[0][0] = 0; //point a
        adjmatrix[0][1] = store[0].weights[0];
        adjmatrix[0][3] = store[0].weights[1];

        //b=2a, 2c, 1e;
        adjmatrix[1][0] = store[1].weights[0];
        adjmatrix[1][1] = 0; //point b
        adjmatrix[1][2] = store[1].weights[1];
        adjmatrix[1][4] = store[1].weights[2];

我只是不知道如何迭代地执行此操作。任何帮助将不胜感激。

最佳答案

char[][]adjmatrix 的转换可以通过一些字符串解析来完成,如下面的代码所示。

int adjmatrix[26][26] = {0}; //assuming 26 possible points (from question "struct stopPoints store[26]")

int i,j,k = 0;
int n = 12;

for (i = 0; i < n; ++i) {
    int l = strlen(str[i]);
    for (j = 0; j < l; ++j) {
        if (str[i][j] == '.') {
            adjmatrix[str[i][0] - 'a'][str[i][j+1] - 'a'] = getWeight(str[i], j);
            // (char) - 'a', gives an integer value for the small case alphabets (a->1, b->2, c->3 ...)
        }
    }
}

getWeight()

int getWeight(char s[], int j) {
    // returns the integer on left of '.' from the string
    int w = 0,i = 0;
    int m = 1;
    for (i = j-1; s[i] >= '0' && s[i] <= '9'; --i) {
        w += (s[i] - '0')*m;
        m *= 10;
    }
    return w;
}

如果您真的需要使用该结构,那么您可以通过使每个商店的 weights 数量等于 connectingPoints 数量来简化它。即,如果您要添加 store[0].connectingPoints[0] = 'a'; 让权重为

store[0].weights[0] = 0;  // weight of point a to point a
store[0].weights[1] = 2;  // weight of point a to point b
store[0].weights[2] = 1;  // weight of point a to point d

另一个例如:"c = 2.b, 1.f",(不确定代码中connectingPoints 的顺序是什么,但请注意要基于索引的点和权重的一种映射)

store[2].connectingPoints[0] = 'b';
store[2].connectingPoints[1] = 'c';
store[2].connectingPoints[2] = 'f';

store[2].weights[0] = 2;
store[2].weights[1] = 0;
store[2].weights[2] = 1;

如果您知道不存在自循环,则可以避免将它们存储在 connectedPoints 中并进一步向 weights 添加相应的条目。

在那种情况下,您可以使用这样的循环构建adjmatrix

struct stopPoints store[26];

for (i = 0; i < n; ++i) {
    int l = strlen(store[i].connectingPoints);
    for (j = 0; j < l; ++j) {
        adjmatrix[str[i][0] - 'a'][store[i].connectingPoints[j] - 'a'] = store[i].weights[j];
    }
}

关于c - 如何迭代获取特定的结构元素并将它们存储到 C 中的二维数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52954341/

相关文章:

c - 在 C 中的位域结构中添加额外的位

c++ - 使用向上(或向下)舍入将 long double 转换为 double

c - 如果使用已经在主机字节顺序中的整数调用 ntohl() 会发生什么?

c - 我可以使用什么预定义的宏来检测 clang?

c# - 将分隔的字符串插入List <int>

c++ - 与内存映射、c++、ERROR_NOT_ENOUGH_MEMORY 共享结构

c - strlen 没有返回正确的字符数

php - 如何在 url 中的第二个斜杠后获取字符串-使用 php?

python - 在没有嵌套 for 循环的情况下在 Python 中明智地连接两个字符串列表

c - 使用结构将内存分配给结构