2016-11-08 4 views
0

テキストファイルから文字列を取得し、その内容をリストに単語単位で挿入するプログラムを作成しようとしています。私はまた、重複の数を計算する必要があります。私のプログラムは、小さな入力テキストファイル(1行の文字列)に対してうまく動作します。しかし、大きなテキストファイルを送るたびにクラッシュします。どんな助けも素晴らしいでしょう。ここでC++リンクリストノードのカウント(ヘルプが必要)

は私のコードです:

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace std; 

class Bag 
{ 
    private: 
     struct BagNode 
     { 
      string dataValue; 
      int dataCount; 
      BagNode *next; 
      BagNode(string); 
     }; 

     BagNode *root; 
     string removePunctuations(string); 
     string toLower(string); 
     void insertData(string); 

    public: 
     Bag(); 
     void procesFile(string, string); 
     void removeData(string); 
     void traverse(); 
}; 

Bag::BagNode::BagNode(string _data) 
{ 
    dataValue.assign(_data); 
    dataCount=1; 
    next = NULL; 
} 

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

void Bag::procesFile(string ifile, string ofile) 
{ 
    ifstream infile; 
    infile.open(ifile.c_str()); 
    if (!infile.good()) 
    { 
     cout<<"Input file not opening."<<endl; 
     return; 
    } 

    string line; 
    while(getline(infile,line)) 
    { 
     stringstream lineStream(line); 
     string token = ""; 
     while(lineStream >> token) 
     { 
      insertData(removePunctuations(token)); 
     } 
    } 
    infile.close(); 
    traverse(); 
    cout<< endl <<"File processed successfully." << endl; 
} 

string Bag::removePunctuations(string data) 
{ 
    int length = data.size(); 
    for(int i = 0; i < length; i++) 
    { 
     if(ispunct(data[i])) 
     { 
      data.erase(i--, 1); 
      length = data.size(); 
     } 
    } 
    return data; 
} 

string Bag::toLower(string data) 
{ 
    for(int i = 0; data[i]; i++){ 
     data[i] = tolower(data[i]); 
    } 
    return data; 
} 

void Bag::insertData(string data) 
{ 
    BagNode *n = new BagNode(data); 
    if (root == NULL) 
    { 
     root = n; 
     return; 
    } 

    BagNode *temp = root; 
    BagNode *prev = NULL; 

    string tdata; 
    data.assign(toLower(data)); 
    while(temp != NULL) 
    { 
     tdata.assign(temp->dataValue); 
     tdata.assign(toLower(tdata)); 
     if (tdata.compare(data) == 0) 
     { 
      temp->dataCount++; 
      return; 
     } 
     else 
     { 
      if (data.compare(tdata) < 0) 
      { 
       if (temp == root) 
       { 
        n->next = temp; 
        root = n; 
        return; 
       } 
       else 
       { 
        n->next = temp; 
        prev->next = n; 
        return; 
       } 
      } 
     } 
     prev = temp; 
     temp = temp->next; 
    } 

    n->next = temp; 
    prev->next = n; 
} 

void Bag::removeData(string data) 
{ 
    BagNode *temp = root; 
    BagNode *prev = NULL; 

    if (root->dataValue.compare(data)==0) 
    { 
     if (root->dataCount > 1) 
      root->dataCount--; 
     else 
     { 
      delete root; 
      root = NULL; 
     } 
     cout<<"Data removed successfully."<<endl; 
     return; 
    } 
    while (temp != NULL) 
    { 
     if (temp->dataValue.compare(data)==0) 
     { 
      if (temp->dataCount > 1) 
       temp->dataCount--; 
      else 
      { 
       prev->next = temp->next; 
       delete temp; 
       temp = NULL; 
      } 
      cout<<"Data removed successfully."<<endl; 
      return; 
     } 
     prev = temp; 
     temp = temp->next; 
    } 
    cout<<"Data not found match."<<endl; 
} 

void Bag::traverse() 
{ 
    if (root == NULL) 
    { 
     cout<<"No data."<<endl; 
     return; 
    } 

    BagNode *temp = root; 
    while(temp != NULL) 
    { 
     if (temp->dataCount > 1) 
      cout << temp -> dataValue << "(" << temp->dataCount << ")" << endl; 
     else 
      cout << temp -> dataValue << endl; 
     temp = temp->next; 
    } 
} 

int main(int argc, char *argv[]) 
{ 
    bool outputConsole = false; 
    string infile, outfile = "\0"; 
    cout << "Welcome!" << endl; 
    int option = -1; 
    do{ 
     if (argc==1 || option == 1) 
     { 
      cout << "Enter the input file: "; 
      cin >> infile; 
      cout << "Enter the output file: "; 
      cin >> outfile; 
     } 
     else 
     { 
      infile.assign(argv[1]); 
      if (argc == 3) 
       outfile.assign(argv[2]); 
     } 

     Bag b; 
     b.procesFile(infile,outfile); 
     //b.traverse(); 

     cout<<endl<<"If you want to input another file press 1 or 2 to quit: "; 
     cin>>option; 
    }while (option != 2); 

    return 0; 
} 
+0

あなたの質問に答える正しいツールはデバッガーです。 [小さなプログラムをデバッグする方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Danh

+0

クラッシュ中にエラーメッセージが表示されますか?例外はスローされますか? –

+0

デバッグアサーションに失敗しました。 行56:式c> = -1 && c <= 255 – LucianoLe

答えて

0

単語の順序は問題ではない場合は、ハッシュテーブルは、重複を追跡するために適しているとして、あなたは本当に代わりに、リンクされたリストのハッシュテーブルを試してみて、使用する必要があります。これは、O(1)挿入操作につながります(理想的な状況で)

+0

私は実際にアルファベット順に単語を出力する必要があります。私は仕事をするためにその部分を持っている:) – LucianoLe