gpt4 book ai didi

c++ - 无法从 txt 文件 C++ 读取输入

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:38 24 4
gpt4 key购买 nike

我无法从文本文件读取输入。我想要做的是从单个输入文件打印多个二叉树。我想为输入的每一行打印一个新的二叉树,但我不确定如何。目前它只是将整个文件作为一棵树来读取。

我的输入文件的一个例子是:

ABCDEFG

BHYTGFHJU

KIJUTTEDS

JHYGFOKJHSG

这是我认为问题所在的部分代码:

int main()
{
BinaryTree <string> BT;

string line;
ifstream myfile("input.txt");
if (myfile.is_open())
{
while(getline (myfile, line, ' '))
{
BT.InsertData(line);

cout << "Preorder: ";
BT.PrintPreorder();
cout << endl;
cout << "Inorder: ";
BT.PrintInorder();
cout << endl;
cout << "Postorder: ";
BT.PrintPostorder();
cout << endl;
cout << "Reverse Inorder: ";
BT.PrintReverseInorder();
cout << endl;

BT.PrintPrintTree();
cout << endl;

}
myfile.close();
}
return 0;
}

编辑:正如我在评论中所问,这是我的 BinaryTree 类代码。

template <class T>
class BinaryTree
{
private:
struct TreeNode
{
TreeNode *left;
TreeNode *right;
T data;
};
TreeNode *root;

public:

BinaryTree()
{
root = NULL;
}

void Inorder(TreeNode *n)
{
if(n != NULL)
{
Inorder(n -> left);
cout<< n -> data;
Inorder(n -> right);
}
}

void PrintInorder()
{
Inorder(root);
}

void Preorder(TreeNode *n)
{
if(n != NULL)
{
cout<< n -> data;
Preorder(n -> left);
Preorder(n -> right);
}
}

void PrintPreorder()
{
Preorder(root);
}

void Postorder(TreeNode *n)
{
if(n != NULL)
{
Postorder(n -> left);
Postorder(n -> right);
cout<<n -> data;
}
}

void PrintPostorder()
{
Postorder(root);
}

void ReverseInorder(TreeNode *n)
{
if(n != NULL)
{
ReverseInorder(n -> right);
cout<< n -> data;
ReverseInorder(n -> left);
}
}

void PrintReverseInorder()
{
ReverseInorder(root);
}

void PrintTree(TreeNode* n, int lev)
{
if (n != NULL)
{
PrintTree(n -> right, lev+1);
for (int i=0; i<lev; i++)
cout << "\t";
cout << n -> data << endl;
PrintTree(n -> left, lev+1);
}
}

void InsertData(T data)
{
TreeNode *t = new TreeNode;
TreeNode *parent;
t -> data = data;
t -> left = NULL;
t -> right = NULL;
parent = NULL;

//is this a new tree?
if (isEmpty())
root = t;
else
{
TreeNode *curr;
curr = root;
while(curr)
{
parent = curr;
if (t -> data > curr -> data)
curr = curr -> right;
else
curr = curr -> left;
}
if(t -> data < parent -> data)
parent -> left = t;
else
parent -> right =t;
}

}
void PrintPrintTree()
{
PrintTree(root, 0);
}


bool isEmpty()
{
return (root == NULL);
}

};

最佳答案

创建二叉树的 vector/数组并将其推回 while(getline (myfile, line, ' ')) 循环。

int main() {
vector <BinaryTree <string> > BT;
int iteration = 0;

string line;
ifstream myfile("input.txt");
if (myfile.is_open())
{
while(getline (myfile, line))
{
BinaryTree <string> temptree;
BT.push_back(temptree);
BT[iteration].InsertData(line);

cout << "Preorder: ";
BT[iteration].PrintPreorder();
cout << endl;
cout << "Inorder: ";
BT[iteration].PrintInorder();
cout << endl;
cout << "Postorder: ";
BT[iteration].PrintPostorder();
cout << endl;
cout << "Reverse Inorder: ";
BT[iteration].PrintReverseInorder();
cout << endl;

BT[iteration].PrintPrintTree();
cout << endl;
iteration++;

}
myfile.close();
}
return 0;
}

关于c++ - 无法从 txt 文件 C++ 读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com