このように、構造体の配列にデータを再帰的に出力する必要があります。
index, numberOfChildern, child[0]...child[numberOfChildern-1]
キャッチは、すべての子供のために、私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);
}
}
}
上記のコードはひどく不完全であることに気付くでしょうが、それは私の印刷機能の使用法を示しています
以下のifelseブロックは、出力に出力する方法です。出力は、ターミナルファイルまたは一連のファイルである可能性があります。印刷機能とthisif elseブロックがテストされ、機能することが証明されています。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を作成する部分であり、最小限の完全な、検証可能例)。
'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()
より一般的な場合には、まだ多くのプログラムのサイズを小さく、データをフォーマットします。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加