我需要像这样递归地打印结构数组中的数据,
index, numberOfChildern, child[0]...child[numberOfChildern-1]
要注意的是,对于每个 child ,我还需要打印 index, numberOfChildern, child[0]...child[numberOfChildern-1]
。
typedef struct node point;
typedef point **pointsList;
结构:
struct node{
int index; //the order in the instance file
int x;//x coord
int y;//y coord
int parent;//parent in tree when added
int numChildern;//has val 0 -- 8 maybe, but doesn't matter the way I wrote it
int *child;
};
每个结构中的所有值都已正确设置,这是我开始编写以完成任务的不完整方法。
void printStringOfChildern(pointsList list, point *point, int fileNum){
if(point->numChildern == 0)
return;
//print the index
char index[calcNumberOfDigitsInAInt(point->index)+3];
sprintf(index, "%d", point->index);
strcat(index, ", ");
print(index, fileNum);
//print the number of childern
char chidern[calcNumberOfDigitsInAInt(point->numChildern)+3];
sprintf(chidern, "%d", point->numChildern);
strcat(chidern, ", ");
print(chidern, fileNum);
//print the childern
for(int i=0; i<point->numChildern; i++){
char child[calcNumberOfDigitsInAInt(point->child[i])+3];
//was child[i] now point->child[i]
sprintf(child, "%d", point->child[i]);
strcat(child, ", ");
print(child, fileNum);
//print out other childern and their data if there is any
int numChildern = numberOfChildern(list, point -> index);
if(numChildern > 0){
printStringOfChildern(list, list[point->child[i]], fileNum);
}
}
}
你会注意到上面的代码很糟糕而且不完整,但它显示了我的打印功能的使用
下面的 if else block 是我打印到输出的方式,它可能是一个文件或一系列文件的终端。打印功能和这个 if else block 已经过测试,并证明可以工作。 inOutType 是一个全局变量,outputFileName 也是。 fileIndex 仅在打印到多个文件的情况下使用,并且在打印到多个文件的情况下,无论有多少文件,都会调用此方法。这是打印函数
if(inOutType == 1 || inOutType == 2){
printLineToOutPut(output, outputFileName, inOutType);
}else{
printToAInstanceFile(output, fileIndex);
}
我在编辑中所做的就是更新样本,然后写下这个
Output format:
index, numberOfChildern, child[i]. If child[i] has children then index, numberOfChildern, indexOf(child[i]), numberOfChildernOf(child[i]), child[i]OfChild[i].....
方法是这样执行的
for(int j=0; j<maxNumberOfPoints; j++)
printStringOfChildern(listOfListOfPoints[i], listOfListOfPoints[i][j], i);
最佳答案
在没有输出应该是什么样子的清晰示例的情况下,这里显示了一个代码版本。请注意,我只是创建了 32 个字符长的缓冲区,而不是花时间计算它们必须到底有多长。这不是很多内存,但足以完成手头的任务。此外,没有数据——我发明了一些:
Index 0: 3 children
Index 1: 0 children
Index 2: 2 children
Index 4: 0 children
Index 5: 0 children
Index 3: 0 children
不需要任何其他数据;未使用问题中结构的 x
、y
和 parent
成员——消除不相关是创建 MCVE 的一部分( Minimal, Complete, Verifiable Example ).
我忽略了“fileNum”参数;打印简单且仅针对标准输出。如果需要,可以很容易地恢复打印到文件。我还使用了一种受 JSON 启发的表示法来报告数据,将数组包含在 […]
中,除了 {…}
中的层次结构顶部外,还包含单个点。
#include <stdio.h>
#include <string.h>
typedef struct node point;
typedef point **pointsList;
struct node
{
int index; // the order in the instance file
// int x;//x coord
// int y;//y coord
// int parent;//parent in tree when added
int numChildren;// has val 0 -- 8 maybe, but doesn't matter the way I wrote it
int *child;
};
static inline void print(const char *str)
{
printf("%s", str);
}
static void printStringOfChildren(pointsList list, point *point)
{
// print the index
char index[32];
sprintf(index, "%d", point->index);
strcat(index, ", ");
print(index);
// print the number of children
char children[32];
sprintf(children, "%d", point->numChildren);
strcat(children, ", ");
print(children);
// print the children
print("[");
for (int i = 0; i < point->numChildren; i++)
{
if (i > 0)
print(",");
print(" { ");
printStringOfChildren(list, list[point->child[i]]);
print(" }");
}
if (point->numChildren > 0)
print(" ");
print("]");
}
int main(void)
{
point *list[] =
{
&(point){ 0, 3, (int[]){ 1, 2, 3 } },
&(point){ 1, 0, 0 },
&(point){ 2, 2, (int[]){ 4, 5 } },
&(point){ 3, 0, 0 },
&(point){ 4, 0, 0 },
&(point){ 5, 0, 0 },
};
printStringOfChildren(list, list[0]);
putchar('\n');
return 0;
}
输出:
0, 3, [ { 1, 0, [] }, { 2, 2, [ { 4, 0, [] }, { 5, 0, [] } ] }, { 3, 0, [] } ]
还有许多其他方法可以进行输出。 sprintf()
和 strcat()
的整个业务都是假的——你完全可以在我的场景中使用 printf()
,或者 fprintf()
在更一般的情况下,格式化数据,进一步减小程序的大小。
关于c - 递归地打印结构数组中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41069604/