【问题标题】:Word count in c++ after using (getline(cin,input))?使用(getline(cin,input))后c ++中的字数?
【发布时间】:2014-04-21 15:45:17
【问题描述】:

所以我对此非常陌生。我有一个任务来计算用户输入的行数、单词数、字符数、唯一行数和唯一单词数。到目前为止,我已经从我的代码中获得了线条、独特的线条和字符。我以为我明白了,但是当我考虑到双空格和制表符时,它就不起作用了。我也不知道如何找到独特的词。请提供您的帮助。

代码:

 // What I dont have:
//words
//Total words


#include <iostream>
#include <string>
#include <set>
using namespace std;


unsigned long countWords(const string& s, set<string>& wl);  //total words

int main()
{
     int linenum=0, charnum=0, totalwords=0;
     set<string> lines;
     string input;
     set<string> unique;   //to store unique words from countWords function

while (getline(cin,input))
    {
         lines.insert(input);
         linenum++; 

         charnum+= input.length();


         totalwords += countWords(input,unique);        
    }

    cout << linenum <<"     "<< totalwords <<"     "<< charnum <<"     " << lines.size()<<"     "         << unique.size()<< endl;

         system("PAUSE"); 
     return 0;
}

unsigned long countWords(const string& s, set<string>& wl) //total words        
{
     int wcount=1;         


     for (unsigned int i=0; i < s.length(); i++)
     {   

          if ((s.at(i) == ' ')&&(s.at(i)+1 !='\0')) {
                         wcount++;

                         }

      }    


return wcount;
}

【问题讨论】:

标签: c++ string set substring word-count


【解决方案1】:

这是函数外观的示例

#include <iostream>
#include <sstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>


unsigned long countWords( std::set<string> &wl, const std::string &s )  
{
    std::istringstream is( s );
    wl.insert( std::istream_iterator<std::string>( is ),
               std::istream_iterator<std::string>() );

    is.clear();
    is.str( s );

    return ( std::distance( std::istream_iterator<std::string>( is ),
                            std::istream_iterator<std::string>() ) );
}

//...

在这个例子中,puctuations 被认为是单词的一部分。

如果你还不知道 std::istringstream 和 C++ 的其他功能,那么你可以按以下方式编写函数

#include <iostream>
#include <set>
#include <string>


unsigned long countWords( std::set<string> &wl, const std::string &s )  
{
    const char *white_space = " \t";
    unsigned long count = 0;

    for ( std::string::size_type pos = 0, n = 0; 
          ( pos = s.find_first_not_of( white_space, pos ) ) != std::string::npos;
          pos = n == std::string::npos ? s.size() : n )
    {
        ++count;
        n = s.find_first_of( white_space, pos );
        wl.insert( s.substr( pos, ( n == std::string::npos ? std::string::npos : n - pos ) ) );
    }

    return count;
}

//...

【讨论】:

  • 我不了解这里使用的任何库或 w/e。讲师建议使用子字符串;跟踪前一个和下一个空格以计算子字符串的参数。然而,我什至考虑用锄头来解决这个问题。
  • @jdreamin 你可以使用 std::vector 吗?
  • 是的,我可以。导师明确表示我不能使用 strtok()。
  • 如果它有助于我想出的另一种方法是 (i) 使用 for 循环遍历字符串 (ii) 如果找到空格 (ws) 将该索引存储为 int wsloc。 (iii) 将最后一个 ws 到新 ws 的所有字符插入到集合 wl 中。
  • 但是我不知道如何更新 ws 位置或如何将最后一个 ws 索引中的字符插入到集合 wl 中
【解决方案2】:

你需要把+1放在括号内,你的功能就是这样

unsigned long countWords(const string& s, set<string>& wl) //total words        
{
     int wcount=0;// initial value must be zero
     int N = 0;// you need to add this to count the characters of each word.
     for (unsigned int i=0; i < s.length(); i++)
     {   
          if ((s.at(i) == ' ')||(s.at(i+1) =='\0')) {// Condition must be or instead of and
                 wl.insert(s.substr(i-N-1,N));
                 ++wcount;
                 N = 0;
              }else ++N;

      }    
return wcount;
}

【讨论】:

  • 非常感谢,但这种方法的结果与我最初的方法相同。
  • 例如:如果我输入 hello 后跟两个空格,则字数返回 3 而应该返回 1 忽略空格和制表符
  • 试试这个 if(N!=0){++wcount;wl.insert(s.substr(i-N-1,N));}
  • 字数为 0,唯一字为 0
  • 必须做N=0前的条件;
猜你喜欢
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-07
  • 2015-11-17
  • 1970-01-01
相关资源
最近更新 更多