2016-12-09 6 views
0

私は印刷データは、再帰的に

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); 

    } 

    } 

} 

あなたは上記のコードはひどいと不完全でわかりますが、以下のelseブロックは、私は、端末A可能性が出力に印刷する方法であれば、それは私の印刷機能

ザ・の使用を示しファイルまたは一連のファイル。印刷機能とこれがelseブロックであるかどうかはテストされ、動作することが証明されています。 inOutTypeはグローバル変数で、outputFileNameも同様です。 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]..... 

方法を記述し

if(inOutType == 1 || inOutType == 2){ 
printLineToOutPut(output, outputFileName, inOutType); 

}else{ 
    printToAInstanceFile(output, fileIndex); 

    } 

はすべて私が編集にやった印刷機能ではそう

for(int j=0; j<maxNumberOfPoints; j++) 
     printStringOfChildern(listOfListOfPoints[i], listOfListOfPoints[i][j], i); 
+1

最初に、 'strcat()'の仕組みや 'sprintf()'が安全でないような基本を学びます。また、グローバル変数を避けてください!また、デバッグの重要なスキルを学びます。 –

+0

は、誰かがすぐにそれを理解できるかどうかを見たいと思っています。今すぐ書いています。私が行ったように文字列のサイズを計算しなければ、strcatとsprintfは安全ではありません。この場合、グローバル変数の使用は簡単です。私はコードの90%を残しました。 @iharob – holycatcrusher

+1

ドット '.'と矢印' - > '演算子の周囲にはスペースを入れないでください。それらは非常に緊密に束縛されており、その周りのスペースはせいぜい常套的であり、コードの作成者は初心者のCプログラマであることを示唆している。 (英語では、複数の「子」は「childern」ではなく「children」です) –

答えて

2

のように実行されます出力がどのように表示されるかの明確な例がない場合、ここにはコードのバージョンが示されています。正確にどのくらいの時間が必要かを正確に計算する時間を費やすのではなく、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 

他のデータは必要ありません。 xyおよびparentのメンバーは使用されません。無関係な部分を削除することは、MCVE(Minimal, Complete, Verifiable Example)の作成の一部です。

私は 'fileNum'引数を無視しました。印刷は単に標準的な出力にのみ行われます。必要に応じて、ファイルに印刷を元に戻すだけでも簡単です。私はまた、[…]に配列を囲んでデータを報告するためにJSONに触発された記法を使用し、{…}内部の階層の頂点を除いて1点を使用しました。

#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()を完全にうまく使用して、データのフォーマットを行い、プログラムのサイズをさらに縮小することができます。

+0

良い入力、私はあなたに仕事のポイントと情報を与えます。割り当てには構造体の作成方法が必要です。私はfprintfへの提案が好きです。次回はもう少し言葉で言いますが、プログラム全体が2000行+であるため、可能な限り小さなコードを含めるようにしました。また、ファイルのリスト、単一のファイル、またはstdoutに印刷する必要があります。私はすべてそれを脂っこいやり方で扱った。それが書かれている方法のために印がつけられておらず、それは5時間で期限切れです – holycatcrusher

関連する問題