2009-05-30 81 views
1
#include <iostream> 
#include <string> 
#include <fstream> 

using namespace std ; 


string strWord(int index , string line) 
{ 
     int count = 0; 
     string word; 
     for (int i = 0 ; i < line.length(); i++) 
     { 
      if (line[i] == ' ') 
      { 
       if (line [i+1] != ' ') 
       { 

        count ++; 
        if ( count == index) 
        { 
         return word; 
        } 
        word =""; 
       } 
      } 
      else 
      { 
       word += line[i]; 
      }  
     } 
} 







int main () 
{ 
    ifstream inFile ; 
    inFile.open("new.txt"); 
    string line , id; 

    cout <<"Enter id : "; 
    cin >>id; 
    while(!inFile.eof()) 
    { 
         getline (inFile , line); 
         if (strWord (1, line) == id) 
         { 
          cout <<strWord (2 , line) <<endl; 
          break; 
         } 
    } 
    system("pause"); 
} 

問題是:有人可以向我解釋這我不明白它在做什麼我的意思是我得到的概念,但是每行是做什麼?C++代碼獲取文件行並讀取行的第二個單詞?

+1

請修正你的代碼的註釋每行前使用4個空格。這是功課嗎? – SingleNegationElimination 2009-05-30 02:09:31

+0

修復了格式。 @ H4cKL0rD,請記住在通過縮進四個空格將其粘貼到Stack Overflow時對其進行格式化。 – 2009-05-30 02:24:59

回答

3

你想在每一行

// function that returns a word from 'line' with position 'index' 
// note that this is not a zero based index, first word is 1, 
// second is 2 etc .. 
string strWord(int index, string line) 
{ 
    int count = 0; // number of read words 
    string word; // the resulting word 
    for (int i = 0 ; i < line.length(); i++) { // iterate over all characters in 'line' 
     if (line[i] == ' ') { // if this character is a space we might be done reading a word from 'line' 
      if (line[i+1] != ' ') { // next character is not a space, so we are done reading a word 
       count++; // increase number of read words 
       if (count == index) { // was this the word we were looking for? 
        return word; // yes it was, so return it 
       } 
       word =""; // nope it wasn't .. so reset word and start over with the next one in 'line' 
      } 
     } 
     else { // not a space .. so append the character to 'word' 
      word += line[i]; 
     }  
    } 
} 


int main() // main function of the program, execution starts here 
{ 
    ifstream inFile; // construct input file stream object 
    inFile.open("new.txt"); // associate the stream with file named "new.txt" 
    string line, id; // 

    cout << "Enter id : "; // write "Enter id :" to console 
    cin >> id; // read input from console and put the result in 'id' 

    while (!inFile.eof()) { // do the following as long as there is something to read from the file 
     getline(inFile, line); // read a line from the file and put the value into 'line' 
    if (strWord(1, line) == id) { // if the first word in 'line' equals 'id' .. 
     cout << strWord(2, line) << endl; // prints the second word in 'line' 
     break; // exits the while loop 
    } 
    } 

    system("pause"); // pause the program (should be avoided) 
} 
2

我沒有仔細閱讀,但它看起來像打印出第一個單詞是用戶輸入的第一個單詞的第二個單詞。

1

你有一個C++交互式調試器嗎?例如,如果將此代碼加載到Microsoft Visual C++中,則可以逐個執行程序中的一條語句,隨時檢查每個變量的值。這是學習一段新代碼在細節層面做什麼的好方法。

0

有一個叫做strWord的函數用來從一個句子的任何索引取出一個單詞並返回它。

主程序打開一個文件,其中包含每行中的一個id和另一個單詞,並保持它可以從中讀取。

然後它向用戶請求一個id,檢查id是否是文件中任何行的第一個單詞。如果是,那麼它會從該行取第二個字並打印出來。

就是這樣。詢問您是否遇到特定線路或線路問題。

或者按照Greg的建議,去找一個調試器。這將是理解任何邏輯的最簡單方法。

2

也許這可以幫助:

// return word[index] from line 
string strWord(int index , string line) // word index and actual line 
{ 
    int count = 0; // current word index 
    string word; // word buffer (return value) 

    for (int i = 0 ; i < line.length(); i++) // loop through each character in line 
    { 
    if (line[i] == ' ') // if current character is a space 
    { 
     if (line [i+1] != ' ') // but next char is not space, we got a word 
     { 
     count ++; // incr current word index counter in line 
     if ( count == index) // if count == looked for index return word 
     { 
      return word; 
     } 
     word =""; // otherwise reset word buffer 
     } 
    } 
    else // character is not space 
    { 
     word += line[i]; // add letter/digit to word buffer 
    }  
    } 
} 

比如當行以空格開始,所以這裏有一些假設有關上述算法失敗的數據需要像沒有處理,如果加以解決該索引無效或者內嵌爲空。如果函數失敗,也會丟失最後的返回值。

相關問題