2011-11-23 6 views
0

사용자가 입력 한 텍스트 파일을 읽고, 사용자가 입력 한 텍스트 파일을 만들고, 사용자가 원하는 텍스트 파일의 이름을 지정하고, 임계 값에 입력 한 사용자 위에 단어를 정렬하는 텍스트 파일을 정렬하고 사용자가 지정한 출력 파일에 단어와 단어가 몇 번 발견되었는지 표시합니다. 나는 대부분의 코드는 완료하지만 컴파일러 오류를을 heres 샘플 출력, 오류 코드바이너리 검색 트리 오버로드 == 연산자 또는 일반적인 조언이 필요합니다.

샘플 출력 단어가 텍스트 파일에있는 단어입니다

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' 

MAIN.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; 
} 

StrType.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'; 
} 

은 내가 == 연산자를 오버로딩에 대해이 생각하지만 과부하에 아주 좋은 메신저. 누군가가 나를 어쨌든 도울 수 있다면 그것은 가능할 것입니다.

답변

1

당신은 과부하에 아니지만 에 대한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); 
} 
+2

이 올바르지 않습니다. 멤버로 정의 할 때는'bool operator == (struct WordType & other)'만 있습니다. 그리고 비교는 아마도'const'이어야하고'const' 인수를 취해야합니다. – arne

+0

@arne, 예, 내 실패, 감사합니다 – zebrilo

+0

그래도 그렇게해야합니다 둘 다 글로벌 및 구조 방법을 사용해야합니다 그리고 만약 그렇다면 글로벌 한 가지 가야합니까? – user1061266

0

당신은 "연산자 =="및 "연산자 <를 구현해야 "WordType 구조체에 대해. 이 같은

그들은이 될 수 멤버 함수 :이 같은

struct WordType 
{ 
    bool operator == (const WordType &other) 
    { 
     // implementation 
    } 

    bool operator < (const WordType &other) 
    { 
     // implementation 
    } 
}; 

또는 전역 함수 : 연산자 당신이 그들을 정의로

bool operator == (const WordType &left, const WordType &right) 
{ 
    // implementation 
} 

bool operator < (const WordType &left, const WordType &right) 
{ 
    // implementation 
} 
+0

다른 답변에 대한 설명을 읽었습니까? 당신이 member-operator를 찾으러 간다면 최대한의 유용성을 위해 그것들을'const'로 선언해야합니다. – xtofl

+0

확인을 해 주셔서 감사합니다. 또한 어디에 bool StrType :: operator == (const StrType & other) const { return (strcmp (letters, other.letters) == 0); } 내가 컴파일하려고하면 정말 큰 오류가 발생합니다. – user1061266

관련 문제