0
我正在讀入一個名爲「Lincoln.txt」的二進制樹,並將它們按照字母順序放入我的樹中,我應該寫這個詞和將該單詞計數到名爲「index.txt」的文件。現在,我可以將它們讀入我的二叉樹中沒有問題,但我不太清楚如何將它們寫入「index.txt」。完成後我檢查我的文件,我得到的都是地址。你能幫我嗎?從文件讀入二進制樹並寫入文件
這裏是我的編碼:
的main.cpp
#include <iostream>
#include "NodeClass.h"
#include <fstream>
using namespace std;
int main()
{
string myString;
BinaryTree myTree("");
ifstream infile;
infile.open("Lincoln.txt");
while(infile)
{
infile >> myString;
myTree.insert(myString, myTree.root);
}
ofstream outFile;
outFile.open("index.txt");
myTree.print(myTree.root, outFile);
outFile.close();
}
NodeClass.cpp
#include <iostream>
#include "NodeClass.h"
using namespace std;
void BinaryTree::insert(ElementType data, TreeNode *&tree)
{
if (tree == NULL)
{
tree = new TreeNode(data);
}
else if(data < tree->data)
insert(data, tree->left);
else if(data > tree->data)
insert(data, tree->right);
else if(data == tree->data)
tree->count++;
}
void BinaryTree::display(TreeNode *tree, ostream& out)
{
if (tree != NULL)
{
display(tree->left, out);
out << tree->data<<" ";
display(tree->right, out);
}
}
void BinaryTree::print(TreeNode *tree, ofstream& outFile)
{
if (tree != NULL)
{
outFile << tree->left;
outFile << tree->data << " " << tree->count;
outFile << tree->right;
}
}
NodeClass.h
#include <iostream>
#include <string>
#include <fstream>
#include <fstream>
using namespace std;
#ifndef TREENODE_H
#define TREENODE_H
typedef std::string ElementType;
class BinaryTree
{
public:
class TreeNode
{
public:
ElementType data;
int count;
TreeNode *left;
TreeNode *right;
TreeNode(ElementType new_data)
{ data = new_data; left = NULL; right = NULL;};
};
TreeNode *root;
BinaryTree(ElementType root_data)
{ root = new TreeNode(root_data); };
~BinaryTree() { delete root;};
void insert(ElementType data, TreeNode *&tree);
void display(TreeNode *tree, ostream& out);
void print(TreeNode *tree, ofstream& outFile);
};
#endif TREENODE_H
Lincoln.txt
The Gettysburg Address
Gettysburg, Pennsylvania
November 19, 1863
Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in
Liberty, and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and
so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate
a portion of that field, as a final resting place for those who here gave their lives that that nation
might live. It is altogether fitting and proper that we should do this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground.
The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add
or detract. The world will little note, nor long remember what we say here, but it can never forget what
they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they
who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great
task remaining before us -- that from these honored dead we take increased devotion to that cause for
which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not
have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government
of the people, by the people, for the people, shall not perish from the earth.
寫index.txt之後,這是我得到:
00000000 -8421504510035D230
編輯:我的教授說我的打印功能應該是一個遞歸函數,但我不知道該怎麼做。
謝謝你,但我不明白爲什麼我得到的大量時試圖打印出我的點數! – Midge
因爲您還沒有在構造函數 – GoldRoger
哇中初始化'count'。 omg我是個白癡。非常感謝你的幫助。先生,祝你有美好的一天。 – Midge