【发布时间】:2013-08-16 15:11:43
【问题描述】:
所以我有这个代码:
#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***
它向上扫描直到 * 符号。它处理数字并打印输出文件。现在我有两个问题。
- 如何扫描下一行数字(1 1 2 和 1 1 2)并再次重复该过程。我可以在 while 循环中使用哪些参数?
- 输出文本文件只打印最后一个元素(这可以在 postorder 函数中找到)。它应该打印:3 4 2 6 7 5 1 但相反,它只打印 1。但是当我使用 printf 时,它会打印正确的输出。
注意:整个代码有效。无需编辑二叉树之类的东西,它只是扫描整数并将输出写入文件,这让我抓狂。
请帮忙!
【问题讨论】:
-
fclose(ofp);对于初学者。
-
假设我想循环直到找到字符 *,那么代码是什么?
-
如果这些是行,那么也许使用 fgets()。 fgets() 一次读取一行。然后解析该行(查找 *),如果有一个或多个则将其丢弃。
-
@JamesReynaldMase,这是一个令人筋疲力尽的answer with examples。 “文件结尾”或EOF 是在
stdio.h中定义的常量,但您也可以查找'a'、'\0'或'*'。