2017-03-11 11 views
-1

사용자가 입력 한 문장을 파일의 단어와 비교하는 프로그램을 작성하고 있습니다. 문장에있는 단어가 파일에서 발견되면 프로그램에서 알려주길 원합니다.stringstream 및 ifstream을 사용하여 파일의 단어와 사용자 입력을 비교하십시오.

getline을 사용하여 사용자 입력 문장을 얻은 다음 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으로 읽어 들인 다음 문자열 스트림에서 추출한 단어를 찾으십시오. –

+0

'bool adjectiveInSent = true;는 무엇을해야합니까? –

+0

나중에 프로그램에서 사용자 입력 문장이 명사, 동사, 명사 등 특정 방식으로 구성되어 있는지 확인해야합니다. –

답변

0

당신은 다음을 수행 할 수 :

가) 사용자의 입력을 읽고 지금 당신의 std::istringstream 을 사용하여 압축을 풉니) std::set<std::string>

B 말하는 STL 컨테이너에 파일에서 단어를 저장 각 단어는 컨테이너에서 find 메소드를 사용하여 a)에서 형성된 맵에서 간단한 검색을 추출합니다.

+0

지도 키는 무엇이되어야합니까? –

+0

@ πάνταῥεῖ 그래, 나는'std :: set'만으로도 충분하다고 생각한다. – P0W

+0

흠, 다른 방법으로 라운드 ?? –

0

수용 가능한 C++ 솔루션은 다음과 같아야합니다

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; 
} 

제한에 대한 귀하의 편집에 관해서는 당신이 할 수있는 (한 즉, 당신이 그것을 사용해야하는 또 다른 바보 제한이 아니다) std::istringstream없이 그 :

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"이 일치하지 않습니다.

간편한 테스트를 위해 adjectiveFileistringstream을 사용하고 있습니다. 물론 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