从C ++中的文本文件中读取

时间:2018-05-18 02:55:26

标签: c++

我想请求帮助来完成我的C ++项目。问题是从包含18个名字的故事的文本文件中读取。这些名字在故事中重复了很多。该程序应计算重复的名称。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
    ifstream in("Romeo and Juliet.txt");
    ofstream out;

    string name[] = {"Escalus","Paris","Montague","Capulet","Romeo","Tybalt",
                     "Mercutio","Benvolio","Friar Laurence","Friar 
                      John","Balthasar","Abram","Sampson","Gregory",
                      "Peter","Lady Montague","Lady Capulet","Juliet"};
    string str;
    int scount=0,v[18];

    for(int i=0;i<18;i++)
    {
     scount=0;
        while(!in.eof())
       {

           while(getline(in,str))
           {
               if(str==name[i])
               {
                   scount++;
               }
           }
       }
        v[i]=scount;
    }

      for(int i=0;i<18;i++)
      {
         cout<<v[i]<<endl;
      }
       in.close();
 }

2 个答案:

答案 0 :(得分:1)

如果您使用std::string,我认为您可以使用std::map

Map用于计算文本中的名称。

map < string, int > v;
for (int i = 0; i < 18; i++)
    v[name[i]] = 0;

对于逐字输入我使用:

freopen("Romeo and Juliet.txt", "r", stdin);
while (cin >> str)
{
    if (str == "Friar" || str == "Lady")
    {
        string s;
        cin >> s;
        if (s.empty())
            continue;
        if ( ((s == "Montague" || s == "Capulet") && str == "Lady") ||
            str == "Friar" && s == "Laurence")
        {
            v[str + " " + s]++;
        }
        else if (v.find(s) != v.end())
            v[s]++;

    }
    else if (v.find(str) != v.end())
        v[str]++;
}    

来自地图的输出值:

for (auto it = v.begin(); it != v.end(); it++)
{
    cout << it->first << " " << it->second << endl; 
}

这很有效。

答案 1 :(得分:0)

主要问题是您需要按空格而不是换行符分组

#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <map>
#include <algorithm>

using namespace std;

typedef map<string, int> word_count_t;

template<typename A, typename B>
std::pair<B,A> flip_pair(const std::pair<A,B> &p)
{
    return std::pair<B,A>(p.second, p.first);
}

template<typename A, typename B>
std::multimap<B,A> flip_map(const std::map<A,B> &src)
{
    std::multimap<B,A> dst;
    std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
                   flip_pair<A,B>);
    return dst;
}

int main() {
    std::ifstream in("/tmp/test.txt");

    word_count_t word_count;
    word_count.insert(make_pair("Escalus", 0));
    word_count.insert(make_pair("Paris", 0));
    word_count.insert(make_pair("Friar Laurence", 0));

    string line;
    string previous;
    string current;

    bool first_line(true);

    while(getline(in, line)) {
        stringstream ss(line);

        if(first_line && getline(ss, previous, ' ')) {
            word_count_t::iterator itr = word_count.find(previous);

            if(itr != word_count.end()) {
                itr->second++;
            }

            first_line = false;
        }

        while(getline(ss, current, ' ')) {
            word_count_t::iterator itr = word_count.find(current);

            if(itr != word_count.end()) {
                itr->second++;
            }

            itr = word_count.find(previous + " " + current);

            if(itr != word_count.end()) {
                itr->second++;
            }

            previous = current;
        }
    }

    std::multimap<int, string> sorted = flip_map(word_count);

    for(std::multimap<int, string>::iterator itr = sorted.begin(); itr != sorted.end(); itr++) {
        cout << itr->first << ": [" << itr->second << "]" << endl;
    }

    cout << endl;

    for(std::multimap<int, string>::reverse_iterator itr = sorted.rbegin(); itr != sorted.rend(); itr++) {
        cout << itr->first << ": [" << itr->second << "]" << endl;
    }
}

编辑:您可能需要添加一些代码,如:

#include <boost/algorithm/string.hpp>

boost::replace_all(current, ",", "");
boost::replace_all(current, ".", "");
boost::replace_all(current, ";", "");
boost::replace_all(current, ":", "");
boost::replace_all(current, "\t", "");
boost::replace_all(current, "[", "");
boost::replace_all(current, "]", "");
boost::replace_all(current, "(", "");
boost::replace_all(current, ")", "");