从文本文件中读取的代码,删除停用词然后应用大小写折叠

时间:2013-12-05 19:36:21

标签: c++

我写了这段代码,它读取了一个名为example .text

的文本文件
I LOVE C++ 
THE CAR IS BLUE

然后从中删除停用词然后应用大小写折叠(将所有字母转换为小写) 问题是,当我运行程序时,最后一个单词(蓝色)被打印两次我不知道为什么!

#include <iostream> 
#include <cmath>
#include <fstream> 
#include <cstdlib> 
#include <cstring> 
using namespace std; 
void ChangeCase(char word[]) 
{ 
    for(size_t i = 0; i < strlen(word); i++) 
    { if( isupper(word[i])) word[i] = tolower(word[i]); 
    } }
    int main()
    { char filename[50]; 
    ifstream example; 
    cin.getline(filename , 50); 
    example.open("example.txt"); 
    if(!example.is_open())
    { 
        exit(EXIT_FAILURE); 
        } char word[50]; 
        example>>word; 
        while (example.good()) 
        { 
            if (std::strcmp(word, "a") != 0 &&std::strcmp(word, "able") != 0 &&std::strcmp(word, "about") != 0 &&std::strcmp(word, "across") != 0 &&std::strcmp(word, "after") != 0 &&std::strcmp(word, "all") != 0 &&std::strcmp(word, "almost") != 0 &&std::strcmp(word, "also") != 0 &&std::strcmp(word, "am") != 0 &&std::strcmp(word, "among") != 0 &&std::strcmp(word, "an") != 0 &&std::strcmp(word, "and") != 0 &&std::strcmp(word, "any") != 0 &&std::strcmp(word, "are") != 0 &&std::strcmp(word, "as") != 0 &&std::strcmp(word, "at") != 0 &&std::strcmp(word, "be") != 0 &&std::strcmp(word, "because") != 0 &&std::strcmp(word, "been") != 0 &&std::strcmp(word, "but") != 0 &&std::strcmp(word, "by") != 0 &&std::strcmp(word, "can") != 0 &&std::strcmp(word, "cannot") != 0 &&std::strcmp(word, "could") != 0 &&std::strcmp(word, "dear") != 0 &&std::strcmp(word, "did") != 0 &&std::strcmp(word, "do") != 0 &&std::strcmp(word, "dose") != 0 &&std::strcmp(word, "either") != 0 &&std::strcmp(word, "else") != 0 &&std::strcmp(word, "ever") != 0 &&std::strcmp(word, "every") != 0 &&std::strcmp(word, "for") != 0 &&std::strcmp(word, "from") != 0 &&std::strcmp(word, "get") != 0 &&std::strcmp(word, "got") != 0 &&std::strcmp(word, "had") != 0 &&std::strcmp(word, "has") != 0 &&std::strcmp(word, "have") != 0 &&std::strcmp(word, "he") != 0 &&std::strcmp(word, "her") != 0 &&std::strcmp(word, "hers") != 0 &&std::strcmp(word, "him") != 0 &&std::strcmp(word, "his") != 0 &&std::strcmp(word, "how") != 0 &&std::strcmp(word, "however") != 0 &&std::strcmp(word, "i") != 0 &&std::strcmp(word, "if") != 0 &&std::strcmp(word, "into") != 0 &&std::strcmp(word, "is") != 0 &&std::strcmp(word, "it") != 0 &&std::strcmp(word, "its") != 0 &&std::strcmp(word, "just") != 0 &&std::strcmp(word, "least") != 0 &&std::strcmp(word, "let") != 0 
&&std::strcmp(word, "like") != 0 &&std::strcmp(word, "likely") != 0 &&std::strcmp(word, "may") != 0 &&std::strcmp(word, "me") != 0 &&std::strcmp(word, "might") != 0 &&std::strcmp(word, "most") != 0 &&std::strcmp(word, "must") != 0 &&std::strcmp(word, "my") != 0 &&std::strcmp(word, " neither ") != 0 &&std::strcmp(word, "no") != 0 &&std::strcmp(word, "nor") != 0 &&std::strcmp(word, "not") != 0 &&std::strcmp(word, "of") != 0 &&std::strcmp(word,"“off") != 0 &&std::strcmp(word, "often") != 0 &&std::strcmp(word, "since") != 0 &&std::strcmp(word, "so") != 0 &&std::strcmp(word, "some") != 0 &&std::strcmp(word, "than") != 0 &&std::strcmp(word, "that") != 0 &&std::strcmp(word, "the") != 0 &&std::strcmp(word, "their") != 0 &&std::strcmp(word, "them") != 0 &&std::strcmp(word, "then") != 0 &&std::strcmp(word, "the") != 0 &&std::strcmp(word, "there") != 0 &&std::strcmp(word, "these") != 0 &&std::strcmp(word, "they") != 0 &&std::strcmp(word, "this") != 0 &&std::strcmp(word, "tis") != 0 &&std::strcmp(word, "to") != 0 &&std::strcmp(word, "too") != 0 &&std::strcmp(word, "twas") != 0 &&std::strcmp(word, "us") != 0 &&std::strcmp(word, "wants") != 0 &&std::strcmp(word, "was") != 0 &&std::strcmp(word, "we") != 0 &&std::strcmp(word, "were") != 0 &&std::strcmp(word, "what") != 0 &&std::strcmp(word, "when") != 0 &&std::strcmp(word, "where") != 0 &&std::strcmp(word, "which") != 0 &&std::strcmp(word, "while") != 0 &&std::strcmp(word, "who") != 0 &&std::strcmp(word, "whom") != 0 &&std::strcmp(word, "why") != 0 &&std::strcmp(word, "will") != 0 &&std::strcmp(word, "with") != 0 &&std::strcmp(word, "would") != 0 &&std::strcmp(word, "yet") != 0 &&std::strcmp(word, "you") != 0 &&std::strcmp(word, "your") != 0 &&std::strcmp(word, "on") != 0 &&std::strcmp(word, "only") != 0 &&std::strcmp(word, "or") != 0 &&std::strcmp(word, "other") != 0 &&std::strcmp(word, "our") != 0 &&std::strcmp(word, "own") != 0 &&std::strcmp(word, "rether") != 0 &&std::strcmp(word, "said") != 0 &&std::strcmp(word, "say") != 0 &&std::strcmp(word, "says") != 0 &&std::strcmp(word, "she") != 0 &&std::strcmp(word, "should") != 0 &&std::strcmp(word, "whom") != 0 &&std::strcmp(word, "why") != 0 &&std::strcmp(word, "will") != 0 
&&std::strcmp(word, "with") != 0 &&std::strcmp(word, "would") != 0 &&std::strcmp(word, "yet") != 0 &&std::strcmp(word, "you") != 0 &&std::strcmp(word, "your") != 0 ) 
cout<<word<<" ";
example>>word; 
ChangeCase(word); 
} ChangeCase(word); 
cout << word; 

system("PAUSE");
return 0; }

3 个答案:

答案 0 :(得分:0)

变化:

example >> word;
while (example.good())
{
     // ...
     example >> word;
}

while (example >> word)                                                        /*
       ^^^^^^^^^^^^^^^                                                         */
{
    // ...
}

您需要在检查流之前进行提取。如果你做相反的事情,即使流没有检查提取是否有效,循环也会运行(它只检查之前的提取是否有效)。

答案 1 :(得分:0)

你确实两次输出了最后一个单词。请考虑以下代码段

        while (example.good()) 
        { 
            if (std::strcmp(word, "a") != 0 &&std::strcmp(word, "able") != 0 &&std::strcmp(word, "about") != 0 &&std::strcmp(word, "across") != 0 &&std::strcmp(word, "after") != 0 &&std::strcmp(word, "all") != 0 &&std::strcmp(word, "almost") != 0 &&std::strcmp(word, "also") != 0 &&std::strcmp(word, "am") != 0 &&std::strcmp(word, "among") != 0 &&std::strcmp(word, "an") != 0 &&std::strcmp(word, "and") != 0 &&std::strcmp(word, "any") != 0 &&std::strcmp(word, "are") != 0 &&std::strcmp(word, "as") != 0 &&std::strcmp(word, "at") != 0 &&std::strcmp(word, "be") != 0 &&std::strcmp(word, "because") != 0 &&std::strcmp(word, "been") != 0 &&std::strcmp(word, "but") != 0 &&std::strcmp(word, "by") != 0 &&std::strcmp(word, "can") != 0 &&std::strcmp(word, "cannot") != 0 &&std::strcmp(word, "could") != 0 &&std::strcmp(word, "dear") != 0 &&std::strcmp(word, "did") != 0 &&std::strcmp(word, "do") != 0 &&std::strcmp(word, "dose") != 0 &&std::strcmp(word, "either") != 0 &&std::strcmp(word, "else") != 0 &&std::strcmp(word, "ever") != 0 &&std::strcmp(word, "every") != 0 &&std::strcmp(word, "for") != 0 &&std::strcmp(word, "from") != 0 &&std::strcmp(word, "get") != 0 &&std::strcmp(word, "got") != 0 &&std::strcmp(word, "had") != 0 &&std::strcmp(word, "has") != 0 &&std::strcmp(word, "have") != 0 &&std::strcmp(word, "he") != 0 &&std::strcmp(word, "her") != 0 &&std::strcmp(word, "hers") != 0 &&std::strcmp(word, "him") != 0 &&std::strcmp(word, "his") != 0 &&std::strcmp(word, "how") != 0 &&std::strcmp(word, "however") != 0 &&std::strcmp(word, "i") != 0 &&std::strcmp(word, "if") != 0 &&std::strcmp(word, "into") != 0 &&std::strcmp(word, "is") != 0 &&std::strcmp(word, "it") != 0 &&std::strcmp(word, "its") != 0 &&std::strcmp(word, "just") != 0 &&std::strcmp(word, "least") != 0 &&std::strcmp(word, "let") != 0 
&&std::strcmp(word, "like") != 0 &&std::strcmp(word, "likely") != 0 &&std::strcmp(word, "may") != 0 &&std::strcmp(word, "me") != 0 &&std::strcmp(word, "might") != 0 &&std::strcmp(word, "most") != 0 &&std::strcmp(word, "must") != 0 &&std::strcmp(word, "my") != 0 &&std::strcmp(word, " neither ") != 0 &&std::strcmp(word, "no") != 0 &&std::strcmp(word, "nor") != 0 &&std::strcmp(word, "not") != 0 &&std::strcmp(word, "of") != 0 &&std::strcmp(word,"“off") != 0 &&std::strcmp(word, "often") != 0 &&std::strcmp(word, "since") != 0 &&std::strcmp(word, "so") != 0 &&std::strcmp(word, "some") != 0 &&std::strcmp(word, "than") != 0 &&std::strcmp(word, "that") != 0 &&std::strcmp(word, "the") != 0 &&std::strcmp(word, "their") != 0 &&std::strcmp(word, "them") != 0 &&std::strcmp(word, "then") != 0 &&std::strcmp(word, "the") != 0 &&std::strcmp(word, "there") != 0 &&std::strcmp(word, "these") != 0 &&std::strcmp(word, "they") != 0 &&std::strcmp(word, "this") != 0 &&std::strcmp(word, "tis") != 0 &&std::strcmp(word, "to") != 0 &&std::strcmp(word, "too") != 0 &&std::strcmp(word, "twas") != 0 &&std::strcmp(word, "us") != 0 &&std::strcmp(word, "wants") != 0 &&std::strcmp(word, "was") != 0 &&std::strcmp(word, "we") != 0 &&std::strcmp(word, "were") != 0 &&std::strcmp(word, "what") != 0 &&std::strcmp(word, "when") != 0 &&std::strcmp(word, "where") != 0 &&std::strcmp(word, "which") != 0 &&std::strcmp(word, "while") != 0 &&std::strcmp(word, "who") != 0 &&std::strcmp(word, "whom") != 0 &&std::strcmp(word, "why") != 0 &&std::strcmp(word, "will") != 0 &&std::strcmp(word, "with") != 0 &&std::strcmp(word, "would") != 0 &&std::strcmp(word, "yet") != 0 &&std::strcmp(word, "you") != 0 &&std::strcmp(word, "your") != 0 &&std::strcmp(word, "on") != 0 &&std::strcmp(word, "only") != 0 &&std::strcmp(word, "or") != 0 &&std::strcmp(word, "other") != 0 &&std::strcmp(word, "our") != 0 &&std::strcmp(word, "own") != 0 &&std::strcmp(word, "rether") != 0 &&std::strcmp(word, "said") != 0 &&std::strcmp(word, "say") != 0 &&std::strcmp(word, "says") != 0 &&std::strcmp(word, "she") != 0 &&std::strcmp(word, "should") != 0 &&std::strcmp(word, "whom") != 0 &&std::strcmp(word, "why") != 0 &&std::strcmp(word, "will") != 0 
&&std::strcmp(word, "with") != 0 &&std::strcmp(word, "would") != 0 &&std::strcmp(word, "yet") != 0 &&std::strcmp(word, "you") != 0 &&std::strcmp(word, "your") != 0 ) 
cout<<word<<" ";
example>>word; 
ChangeCase(word); 
} ChangeCase(word); 
cout << word; 

第一次在if语句中输出while循环中的最后一个单词。这是

if (std::strcmp(word, "a") != 0 /*...other conditions */ ) cout<<word<<" ";

第二次输出循环后的最后一个单词

...
    } ChangeCase(word); 
    cout << word; 

问题是当输入失败时,变量字的内容没有改变。它仍然等于“蓝色”

答案 2 :(得分:0)

我认为你把功能放在了

的问题
ChangeCase(word);

两次

example>>word; 
ChangeCase(word); 
} ChangeCase(word); 
cout << word; 

你必须删除第二个

代码如下

#include <iostream> 
#include <cmath>
#include <fstream> 
#include <cstdlib> 
#include <cstring> 
using namespace std; 
void ChangeCase(char word[]) 
{ 
    for(size_t i = 0; i < strlen(word); i++) 
    { if( isupper(word[i])) word[i] = tolower(word[i]); 
    } }
    int main()
    { char filename[50]; 
    ifstream example; 
    cin.getline(filename , 50); 
    example.open("example.txt"); 
    if(!example.is_open())
    { 
        exit(EXIT_FAILURE); 
        } char word[50]; 
        example>>word; 
        while (example.good()) 
        { 
            if (std::strcmp(word, "a") != 0 &&std::strcmp(word, "able") != 0 &&std::strcmp(word, "about") != 0 &&std::strcmp(word, "across") != 0 &&std::strcmp(word, "after") != 0 &&std::strcmp(word, "all") != 0 &&std::strcmp(word, "almost") != 0 &&std::strcmp(word, "also") != 0 &&std::strcmp(word, "am") != 0 &&std::strcmp(word, "among") != 0 &&std::strcmp(word, "an") != 0 &&std::strcmp(word, "and") != 0 &&std::strcmp(word, "any") != 0 &&std::strcmp(word, "are") != 0 &&std::strcmp(word, "as") != 0 &&std::strcmp(word, "at") != 0 &&std::strcmp(word, "be") != 0 &&std::strcmp(word, "because") != 0 &&std::strcmp(word, "been") != 0 &&std::strcmp(word, "but") != 0 &&std::strcmp(word, "by") != 0 &&std::strcmp(word, "can") != 0 &&std::strcmp(word, "cannot") != 0 &&std::strcmp(word, "could") != 0 &&std::strcmp(word, "dear") != 0 &&std::strcmp(word, "did") != 0 &&std::strcmp(word, "do") != 0 &&std::strcmp(word, "dose") != 0 &&std::strcmp(word, "either") != 0 &&std::strcmp(word, "else") != 0 &&std::strcmp(word, "ever") != 0 &&std::strcmp(word, "every") != 0 &&std::strcmp(word, "for") != 0 &&std::strcmp(word, "from") != 0 &&std::strcmp(word, "get") != 0 &&std::strcmp(word, "got") != 0 &&std::strcmp(word, "had") != 0 &&std::strcmp(word, "has") != 0 &&std::strcmp(word, "have") != 0 &&std::strcmp(word, "he") != 0 &&std::strcmp(word, "her") != 0 &&std::strcmp(word, "hers") != 0 &&std::strcmp(word, "him") != 0 &&std::strcmp(word, "his") != 0 &&std::strcmp(word, "how") != 0 &&std::strcmp(word, "however") != 0 &&std::strcmp(word, "i") != 0 &&std::strcmp(word, "if") != 0 &&std::strcmp(word, "into") != 0 &&std::strcmp(word, "is") != 0 &&std::strcmp(word, "it") != 0 &&std::strcmp(word, "its") != 0 &&std::strcmp(word, "just") != 0 &&std::strcmp(word, "least") != 0 &&std::strcmp(word, "let") != 0 
&&std::strcmp(word, "like") != 0 &&std::strcmp(word, "likely") != 0 &&std::strcmp(word, "may") != 0 &&std::strcmp(word, "me") != 0 &&std::strcmp(word, "might") != 0 &&std::strcmp(word, "most") != 0 &&std::strcmp(word, "must") != 0 &&std::strcmp(word, "my") != 0 &&std::strcmp(word, " neither ") != 0 &&std::strcmp(word, "no") != 0 &&std::strcmp(word, "nor") != 0 &&std::strcmp(word, "not") != 0 &&std::strcmp(word, "of") != 0 &&std::strcmp(word,"“off") != 0 &&std::strcmp(word, "often") != 0 &&std::strcmp(word, "since") != 0 &&std::strcmp(word, "so") != 0 &&std::strcmp(word, "some") != 0 &&std::strcmp(word, "than") != 0 &&std::strcmp(word, "that") != 0 &&std::strcmp(word, "the") != 0 &&std::strcmp(word, "their") != 0 &&std::strcmp(word, "them") != 0 &&std::strcmp(word, "then") != 0 &&std::strcmp(word, "the") != 0 &&std::strcmp(word, "there") != 0 &&std::strcmp(word, "these") != 0 &&std::strcmp(word, "they") != 0 &&std::strcmp(word, "this") != 0 &&std::strcmp(word, "tis") != 0 &&std::strcmp(word, "to") != 0 &&std::strcmp(word, "too") != 0 &&std::strcmp(word, "twas") != 0 &&std::strcmp(word, "us") != 0 &&std::strcmp(word, "wants") != 0 &&std::strcmp(word, "was") != 0 &&std::strcmp(word, "we") != 0 &&std::strcmp(word, "were") != 0 &&std::strcmp(word, "what") != 0 &&std::strcmp(word, "when") != 0 &&std::strcmp(word, "where") != 0 &&std::strcmp(word, "which") != 0 &&std::strcmp(word, "while") != 0 &&std::strcmp(word, "who") != 0 &&std::strcmp(word, "whom") != 0 &&std::strcmp(word, "why") != 0 &&std::strcmp(word, "will") != 0 &&std::strcmp(word, "with") != 0 &&std::strcmp(word, "would") != 0 &&std::strcmp(word, "yet") != 0 &&std::strcmp(word, "you") != 0 &&std::strcmp(word, "your") != 0 &&std::strcmp(word, "on") != 0 &&std::strcmp(word, "only") != 0 &&std::strcmp(word, "or") != 0 &&std::strcmp(word, "other") != 0 &&std::strcmp(word, "our") != 0 &&std::strcmp(word, "own") != 0 &&std::strcmp(word, "rether") != 0 &&std::strcmp(word, "said") != 0 &&std::strcmp(word, "say") != 0 &&std::strcmp(word, "says") != 0 &&std::strcmp(word, "she") != 0 &&std::strcmp(word, "should") != 0 &&std::strcmp(word, "whom") != 0 &&std::strcmp(word, "why") != 0 &&std::strcmp(word, "will") != 0 
&&std::strcmp(word, "with") != 0 &&std::strcmp(word, "would") != 0 &&std::strcmp(word, "yet") != 0 &&std::strcmp(word, "you") != 0 &&std::strcmp(word, "your") != 0 ) 
cout<<word<<" ";
example>>word; 
ChangeCase(word); 
} 
cout<<word<<"   ";


system("PAUSE");
return 0; }

,结果将是

I love c++ car blue