gpt4 book ai didi

c++ - 二叉搜索树重载 == 运算符或只需要一般建议

转载 作者:行者123 更新时间:2023-11-28 08:13:57 28 4
gpt4 key购买 nike

我正在开发一个程序,该程序读取用户输入的文本文件,创建用户输入的文本文件,命名用户想要的文本文件,然后对文本文件排序,在用户输入的上方排序单词在阈值中并显示单词及其在用户指定的输出文件中找到的次数。我已完成大部分代码,但出现编译器错误,这里是示例输出、错误和代码

示例输出

Enter name of input command file; press return.
history.in
Enter name of output file; press return.
history.out
Enter name of test run; press return.
sample
Enter the minimum size word to be considered.
5
Sample results (found in user specified output file):
sample
abacus 4
abstract 1
adding 1
addition 2
advances 1
after 3

其中单词是在文本文件中找到的单词,旁边的数字是找到的次数。

错误代码

C:\Users\kevin jack\Desktop\prog-4>g++ -o try main.cpp
main.cpp: In function `void Process(TreeNode*&, StrType)':
main.cpp:49: error: no match for 'operator==' in 'tree->TreeNode::info.WordType:
:word == s'
main.cpp:51: error: no match for 'operator<' in 's < tree->TreeNode::info.WordTy
pe::word'

主要.cpp

//main.cpp
#include <fstream>
#include "StrType.h"
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

struct WordType
{
public:
StrType word;
int count;
};

struct TreeNode
{
WordType info;
TreeNode* left;
TreeNode* right;
};

class ListType
{
public:
ListType();
void InsertOrIncrement (StrType string);
void Print(std::ofstream&) const;
private:
TreeNode* root;
};


ListType::ListType()
{
root=NULL;
}

void Process(TreeNode*& tree, StrType s)
{
if(tree == NULL)
{
tree = new TreeNode;
tree->info.word = s;
tree->info.count = 1;
tree->left = NULL;
tree->right = NULL;
}
else if (tree->info.word == s)
tree->info.count++;
else if (s < tree->info.word)
Process(tree->left, s);
else
Process(tree->right, s);
}

void ListType::InsertOrIncrement(StrType s)
{
Process(root, s);
}

void Print (TreeNode* tree, std::ofstream& outFile)
{
if (tree!= NULL)
{
Print(tree->left, outFile);
tree->info.word.PrintToFile(true, outFile);
outFile <<" "<< tree->info.count;
Print(tree->right, outFile);
}
}

void ListType::Print(std::ofstream& outFile) const
{
::Print(root, outFile);
}


int main()
{
using namespace std;
ListType list;
string inFileName;
string outFileName;
string outputLabel;
ifstream inFile;
ofstream outFile;
StrType string;
int minimumLenght;

cout<<"enter in imput file name."<<endl;
cin>>inFileName;
inFile.open(inFileName.c_str());

cout<<"enter name of output file."<<endl;
cin>>outFileName;
outFile.open(outFileName.c_str());

cout<<"enter name of test run."<<endl;
cin>>outputLabel;
outFile<< outputLabel << endl;

cout<<"enter the min word size."<<endl;
cin>>minimumLenght;

string.GetStringFile(true, ALPHA_NUM, inFile);
while(inFile)
{
if(string.LenghtIs() >= minimumLenght)
list.InsertOrIncrement(string);
string.GetStringFile(true, ALPHA_NUM, inFile);
}

list.Print(outFile);
outFile.close();
inFile.close();
return 0;
}

字符串类型.h

//StrType.h
#include <fstream>
#include <iostream>

const int MAX_CHARS=100;
enum InType{ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW};

class StrType
{
public:
void MakeEmpty();
void GetString(bool skip, InType charsAllowed);
void GetStringFile(bool skip, InType charsAllowed,
std::ifstream& inFile);
void PrintToScreen(bool newLine);
void PrintToFile(bool newLine, std::ofstream& outFile);
int LenghtIs();
void CopyString(StrType& newString);
private:
char letters[MAX_CHARS + 1];
};

void StrType::MakeEmpty()
{
letters[0] ='\0';
}

我认为这是关于重载 == 运算符,但我不太擅长重载。如果有人可以帮助我,那就太棒了。

最佳答案

你不是重载而是定义 operator==operator<为你的 class StrType您可以按如下方式进行:

class StrType  
{
public:
...
bool operator==(const StrType& other) const;
bool operator<(const StrType& other) const;
...
};

bool StrType::operator==(const StrType& other) const
{
return (strcmp(letters, other.letters) == 0);
}

bool StrType::operator<(const StrType& other) const
{
return (strcmp(letters, other.letters) < 0);
}

关于c++ - 二叉搜索树重载 == 运算符或只需要一般建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238000/

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