2017-03-11 7 views
-1

ユーザー入力文とファイル内の単語を比較するプログラムを作成しています。私は、文章中の単語のどれかがファイル内にあるかどうかをプログラムに教えてほしい。stringstreamとifstreamを使用して、ファイル内の単語とユーザー入力を比較します。

私はgetl​​ineを使用してユーザー入力文を取得しています。次にistringstreamを使用して文章を単語に切り詰めて、個々の単語をファイル内のすべての単語と比較しています。私のアプローチは、whileループを使用してファイル内のすべての単語を繰り返し、ユーザーが入力した文の現在の単語とファイル内の現在の単語を比較することです。現在のファイルの単語が現在の文の単語と同じ場合は、boolをtrueに設定し、そうでなければ、ファイルの次の単語にスキップして、その単語を次の単語と比較します。

istringstreamとifstreamの両方を取得して、whileループの次の単語にジャンプする方法がわかりません。どうすれば次の単語に反復することができますか?

編集:私はベクトルを使用せずにこれを行う必要があり、配列、など私の最初のアプローチは、ベクターを使用していたが、それは許可されていません。

あなたは次の操作を行うことができ
int main() { 
    string sentence; 
    string fileWord; 

    cout << "Input your sentence : " << endl; 
    getline(cin, sentence); 

    istringstream iss(sentence); 
    string sentenceWord; 

    ifstream adjectiveFile; 
    adjectiveFile.open("/project1.cpp/words/adjectives"); 

    while(iss >> sentenceWord && adjectiveFile >> fileWord) { 
     if (sentenceWord == fileWord) { 
      cout << "The word " << sentenceWord << " is in the file" << endl; 
      bool adjectiveInSent = true; 
     } else { 

     } 
    } 

    adjectiveFile.close(); 

    return 0; 
} 
+0

あなたはそのようにそれを行うことはできません。ファイル全体をstd :: setに読み込み、セット内のstringstreamから抽出した単語を参照します。 –

+0

'bool adjectiveInSent = true;'とは何ですか? –

+0

は後で私は、ユーザーが文を入力するかどうかを確認する必要があり、プログラム内のローカル変数のスコープとは_laterz_はありません –

答えて

0

a)のSTLコンテナ内のファイルから単語が std::set<std::string>

Bを言う保存)ユーザ入力を読み、今のためのあなたのstd::istringstream を使用して、それを抽出各単語は、a)で作成されたマップで容器のfindメソッドを使用して簡単な検索を抽出します。あなたはstd::istringstreamなし(限り、それはあなたがそれを使用する必要があり、別の愚かな制限はありません)ことを行うことができ規制について、編集用として

int main() { 
    string sentence; 

    cout << "Input your sentence : " << endl; 
    getline(cin, sentence); 

    istringstream iss(sentence); 
    std::set<string> lookUpWords; 
    std::string word; 
    while(iss >> word) { 
     if(lookUpWords.find(word) != lookUpWords.end()) { 
      lookUpWords.insert(word); 
     } 
    } 

    ifstream adjectiveFile("/project1.cpp/words/adjectives"); 

    string fileWord; 
    while(adjectiveFile >> fileWord) { 

     if (lookUpWords.find(fileWord) != lookUpWords.end()) { 
      cout << "The word " << sentenceWord << " is in the file" << endl; 
     } 
    } 

    adjectiveFile.close(); 

    return 0; 
} 

+0

マップキーは何ですか? –

+0

@πάνταῥεῖええ、私は 'std :: set'はちょうどいいと思います。 – P0W

+0

うーん、他の方法ラウンド? –

0

許容C++ソリューションは、次のようになります。

int main() { 
    string sentence; 

    cout << "Input your sentence : " << endl; 
    getline(cin, sentence); 

    ifstream adjectiveFile("/project1.cpp/words/adjectives"); 

    string fileWord; 
    while(adjectiveFile >> fileWord) { 

     if (sentence.find(fileWord) != std::string::npos) { 
      cout << "The word " << sentenceWord << " is in the file" << endl; 
     } 
    } 

    adjectiveFile.close(); 

    return 0; 
} 
+1

問題は、セット、配列、ベクトル、ライブラリなどを使用できないことです。 –

+0

@NickKinlen編集に関する私のコメントを読んでください。それはちょうど非常にばかげた制限です。 –

+0

@NickKinlen代替ソリューションを提供しました。 –

0

ここではストリームのみを使用するソリューションです。コメント欄に記されているように、それほど効率的ではありませんが、教師は教師であり、課題は課題です。 ;-)

アルゴリズムは、複合語のために正しく動作します。単純なstring.find()のように、 "fip"と "fipfop"は一致しません。

私はistringstreamadjectiveFileに使用しています。もちろん、ifstreamで置き換えることができます。

#include <string> 
#include <iostream> 
#include <sstream> 

using namespace std; 

int main() { 

    istringstream iss("bar fam fop fip"); 
    istringstream adjectiveFile("foo bar baz fup fam fiz fipfop"); 

    string fileWord, sentenceWord; 
    while(adjectiveFile >> fileWord) { 
     iss.clear(); // clear EOF 
     iss.seekg(0); // start from beginning 
     while(iss >> sentenceWord) { 
      if (sentenceWord == fileWord) { 
       cout << "The word " << sentenceWord << " is in the file" << endl; 
      } 
     } 
    } 

    return 0; 
} 

Live Demo

出力:

The word bar is in the file 
The word fam is in the file 
関連する問題