2016-11-08 7 views
0

Ich versuche, ein Programm zu erstellen, das String-Eingabe aus einer Textdatei erhält, den Inhalt in eine Liste einfügen, Wort für Wort. Ich muss auch die Nummern der Duplikate berechnen. Mein Programm funktioniert gut für die kleine Eingabe Textdatei (1 Zeile der Zeichenfolge). Aber wenn ich es mit einer größeren Textdatei füttere, stürzt es ab. Jede Hilfe wird großartig sein.C++ Linked List Knoten zählen (brauche Hilfe)

Hier ist mein Code:

#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

Das richtige Werkzeug, um Ihre Frage zu beantworten ist Ihr Debugger. Werfen Sie einen Blick auf [Wie kleine Programme zu debuggen] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Danh

+0

Erhalten Sie eine Fehlermeldung während des Absturzes? Jede Ausnahme wirft? –

+0

Ich habe einen Fehler erhalten, der besagt: Debug Assertion Failed! Zeile 56: Ausdruck c> = -1 && c <= 255 – LucianoLe

Antwort

0

Wenn Reihenfolge der Wörter ist nicht ein Problem, sollten Sie wirklich versuchen und eine Hash-Tabelle anstelle einer verknüpften Liste als Hash-Tabelle für die Verfolgung von Duplikaten geeignet ist. Dies führt zu O (1) einfügen (im Idealfall)

+0

Ich muss die Wörter tatsächlich in alphabetischer Reihenfolge ausgeben. Ich habe diesen Teil zur Arbeit tho :) – LucianoLe