1

だから私はこのコードを持っています:

#include <stdio.h>
#include <stdlib.h>

struct tree
{
    char data;
    struct tree *left;
    struct tree *right;
};


int findNode(char temp[], int x, int y, char val);
struct tree *insert(int data);


struct tree *binarytree(char inorder[], char preorder[], int x, int y)
{
    static int index = 0;

    if (x > y)
        return NULL;

    struct tree *new = insert(preorder[index++]);


    if (x == y)
        return new;

    int inIndex = findNode(inorder, x, y, new->data);

    new->left = binarytree(inorder, preorder, x, inIndex - 1);
    new->right = binarytree(inorder, preorder, inIndex + 1, y);

    return new;
}

int findNode(char temp[], int x, int y, char val)
{
    int i;

    for (i = x; i <= y; i++)
    {
        if (temp[i] == val)
            return i;
    }
}


struct tree *insert(int data)
{
    struct tree *tree = (struct tree *)malloc(sizeof(struct tree));
    tree->data = data;
    tree->left = NULL;
    tree->right = NULL;

    return (tree);
}

void postorder(struct tree *tree)
{
    FILE *ofp;
    ofp = fopen("201262480.txt", "w");

    if (tree == NULL)
    {
        return;
    }
    else
    {
        postorder(tree->left);
        postorder(tree->right);
        fprintf(ofp, "%d ", tree->data);
    }

}

int main()
{
    int i = 0, j;
    int temp[100];
    char c, buffer[20];
    FILE *fp;
    fp = fopen("input.txt", "r");

    if (fp != NULL)
    {
        while (1 == fscanf(fp, "%d ", &temp[i]))
        {
            i++;
        }

        char inorder[i / 2];
        char preorder[i / 2];

        for (j = 0; j < i / 2; j++)
        {
            preorder[j] = temp[j];
            inorder[j] = temp[j + (i / 2)];
        }

        int length = sizeof(inorder) / sizeof(inorder[0]);
        struct tree *root = binarytree(inorder, preorder, 0, length - 1);
        postorder(root);
    }
    else
    {
        printf("Cannot open File!\n");
    }

    return 0;
}

そして、次のような入力ファイルがあります。

1 2 3 4 5 6 7
3 2 4 1 6 5 7
***
1 1 2
1 1 2
***end of input***

*記号までスキャンします。数値を処理し、出力ファイルを出力します。今、私には2つの問題があります。

  1. 数字の次の行 (1 1 2 と 1 1 2) をスキャンして、プロセスをもう一度繰り返すにはどうすればよいでしょうか。while ループに使用できる引数は何ですか?
  2. 出力テキスト ファイルは、最後の要素のみを出力します (これは postorder 関数で見つけることができます)。次のように表示されます: 3 4 2 6 7 5 1 しかし、代わりに 1 しか表示されません。しかし、printf を使用すると、正しい出力が表示されます。

注: コード全体が機能します。バイナリ ツリーなどを編集する必要はありません。整数をスキャンして出力をファイルに書き込むだけで、気が狂います。

助けてください!

4

1 に答える 1

0

aの代わりにアクセス モードを使用しwます。

w処置:出力操作用の空のファイルを作成してください。
関数を呼び出すたびに、ファイルpostorder()を上書きし201262480.txtます。そのため、出力は1次のとおりです。関数の最後の呼び出しがpostorder()上書きされ、そこに201262480.txt書き込ま1れます。

a:ファイルの末尾で出力用にファイルを開きます。
このアクセス モードを使用すると、期待どおりの出力が得られます。

void postorder(struct tree *tree)
{
    FILE *ofp;
    ofp = fopen("201262480.txt", "a");  // I've changed "w" to "a". 
                                        // Now the output will be proper. 
    if (tree == NULL)
    {
    fclose(ofp);
    return;
    }
    else
    {
        postorder(tree->left);
        postorder(tree->right);
        fprintf(ofp, "%d ", tree->data);
    }
    fclose(ofp);
}

次の出力が期待されます。

3 4 2 6 7 5 1 

アップデート:

postorder()プログラムを実行するたびに出力ファイルが上書きされるように、関数を修正しました。

コードは次のとおりです。

void postorder(struct tree *tree)
{
    unsigned int pointerValue = 0;  
    static int overwriteFile = 0; // The file hasn't been overwritten yet.
    FILE *ofp = NULL;
    ofp = fopen("201262480.txt", "a");

    fseek(ofp, 0L, SEEK_END);       // Moving the pointer to the end of file.
    pointerValue = ftell(ofp);          // Checking if it is not `0` (the file contains some data)
    if (pointerValue != 0) {
        if (overwriteFile == 0) {
            fclose(ofp);
            ofp = fopen("201262480.txt", "w");  // "w": Create an empty file for output operations.
            fclose(ofp);
            ofp = fopen("201262480.txt", "a");  // "a": Open file for output at the end of a file.   
            overwriteFile = 1;  // The file was overwritten. 
                                // So when you call this function next time, 
                                // it will not overwrite the file. 
                                // It only does it once when you run the program.
        }
    }

    if (tree == NULL)
    {
        return;
    }
    else
    {
        postorder(tree->left);
        postorder(tree->right);
        fprintf(ofp, "%d ", tree->data);
    }
    fclose(ofp);
}
于 2013-08-16T18:00:43.783 に答える