2016-12-01 133 views
-6

这是我到目前为止。现在我遇到的问题是在我的名为“word.txt”的文本文件中读入并插入单词串到树结构树中。我用“无效的init()”函数来测试树的功能和它,但使用该文本文件,多数民众赞成在我不知道该怎么阅读文本文件

任何想法,请

#include <iostream> 
using namespace std; 
#include <fstream> 
#include <iomanip> 
#include <string> 



class TrieNode { 
public: 
    // Initialize your data structure here. 
    TrieNode() { 
     value = 0; 
     for (int i=0;i<26;i++){ 
      children[i] = NULL; 
     } 
    } 
    int value; 
    TrieNode* children[26]; 
}; 

class Trie { 

private: 
    TrieNode*root; 
    int count; 


public: 
    Trie() { 
     root = new TrieNode(); 
     count = 0; 
    } 

    // Inserts a word into the trie. 
    void insert(string s) { 
     TrieNode *p = root; 
     long int len = s.size(); 
     for (int i=0;i<len;i++){ 
      int index = s[i] - 'a'; 
      if (! p->children[index]){ 
       p->children[index] = new TrieNode(); 
      } 
      p = p->children[index]; 
     } 
     count++; 
     p->value = count; 
    } 

    // Returns if the word is in the trie. 
    // -1 if not in trie and not prefix of anything in trie 
    // 0 if not in trie but is a prefix of something in trie 
    // 1 if in trie 
    int search(string key) { 
     TrieNode *p = root; 
     long int lenght = key.size(); 
     for (int i=0;i<lenght;i++){ 
      int index = key[i] - 'a'; 
      if (p->children[index]){ 
       p = p->children[index]; 
      } 
      else{ 
       return -1; 
      } 
     } 
     if (p->value!=0){ 
      return 1; 
     } 
     else{ 
      return 0; 
     } 

    } 

}; 


//Game class using a tree 
class GhostGame{ 
private: 
    string row; 
    ifstream fin; 
    string wordSoFar = ""; 
    string Player1,Player2; 
    Trie Tree; 
public: 
    void ReadFile(){ 
     ifstream fin("word.txt"); 

     while (!fin.eof()) { // read file till the end 
      fin>>row; 
      getline(fin,row); 
      cout << row << endl; 
      Tree.insert(row); 
     } 
     //fin.close(); 
    } 
    void init(){ 
     Tree.insert("ab"); 
     Tree.insert("acd"); 
    } 


    //start menu 
    void StartGame(){ 
     init(); 
     cout<<"========================="<<endl; 
     cout<<"Welcome to Ghost Game"<<endl; 
     cout<<"========================="<<endl; 

     //ReadFile(); 
     while(Tree.search(wordSoFar)!=1){ 
      cout<< "Player 1 Insert a letter => "; 
      cin>> Player1; 
      cout<<setw(60)<<"now = ["<< wordSoFar <<"]"<<endl; 
      wordSoFar +=Player1; 
      if(Tree.search(wordSoFar)==1){ 
       cout<< "Player 2 Wins "<<endl; 
       break; 
      } 

      cout<< "Player 2 Insert a letter => "; 
      cin>>Player2; 
      cout<<setw(60)<<"now = ["<< wordSoFar<<"]"<<endl; 
      wordSoFar += Player2; 
      if(Tree.search(wordSoFar)==1){ 
       cout<< "Player 1 Wins "<<endl; 
       break; 
      } 

     } 
    }}; 



// main driver 
int main() 
{ 

    GhostGame G1; 
    G1.StartGame(); 


    return 0; 
} 
+0

前....... 你试过什么了?你以前在哪里搜索过?这个网站不是为你做思想工作...... – itmuckel

+0

你在问人们做你的工作,而不是帮你。 你需要说出你所尝试的不仅仅是“为我而做” – kemis

+0

有一种我忘记的名字是为这种搜索而建立的。 – user4581301

回答

-1
  • 打开文件
  • 与fgets或得到线功能文件读入到临时字符串
  • 检查第一个字符...确保tolower的使用,比较

    void findString(char *filename, char ch) 
    { 
        char temp[100]; 
        FILE *infile = fopen(filename, "rw"); 
        while(infile != NULL) 
        { 
        fgets(temp,100,infile); 
    
        if(tolower(temp[0]) == ch) 
         cout << temp; 
        } 
        fclose(infile); 
    } 
    
1

使用正表达式为这样的任务,它们在头文件<regex>中。例如,一些关于正则表达式的好教程可以在Professional C++(Wrox)书中找到。

对于从<fstream>使用ifstream类的文件读取。使用