gpt4 book ai didi

c++ - 从文本文件中读取输入并提取它 - C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:58 29 4
gpt4 key购买 nike

我试图逐行读取文本文件并将数据提取到我的程序中。本质上,C++ 程序将读取已创建的格式为以下格式的输入文件:

     Republican Senator John McMahon
Democrat Mayor Steven Markel
Republican Judge Matt Stevens
Democrat Senator Anthony Quizitano
S R
M D
J R
...
..
..

格式基本上是前 3 行,包括党派、职位和姓名,接下来的几行表示“结果”,其形式为:

[立场首字母] [投票支持的一方]

因此,例如,如果您看到 S R,则表示参议员获得 1 票,并且投给了共和党候选人。

这是我目前所拥有的:

            #include<fstream>
int main()
{
std::ifstream input("file.txt");
}

据我所知,这将允许我输入文件,并逐行浏览它,但我不确定我应该如何从这里开始实现这一点……有什么帮助吗?

谢谢!

最佳答案

为了乐趣和荣耀,这是基于 Boost Spirit 的实现。我添加了更多假投票输入,以便可以显示一些内容。

  • 我不确定候选人和选票之间是否存在 1:1 的关系(我不是美国公民,我不知道列出的候选人是会投票还是被投票) .所以我决定只使用假数据。

    const std::string input = 
    "Republican Senator John McMahon\n"
    "Democrat Senator Anthony Quizitano\n"
    "S R\n"
    "S R\n"
    "S R\n"
    "Democrat Mayor Steven Markel\n"
    "Republican Judge Matt Stevens\n"
    "M D\n"
    "J R\n"
    "S R\n"
    "S R\n";

    然而,该代码可用于这两个目的。

  • 我让输入的显示顺序变得不重要。
  • 不过,您可以选择断言单个字母 (S,M,J) 实际上对应于该点之前列出的位置。通过使用 posletter_check
  • 取消注释检查来启用此功能

观看现场演示 http://liveworkspace.org/code/d9e39c19674fbf7b2419ff88a642dc38

#define BOOST_SPIRIT_USE_PHOENIX_V3
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <iomanip>

namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;

struct Candidate { std::string party, position, name; };

BOOST_FUSION_ADAPT_STRUCT(Candidate, (std::string, party)(std::string, position)(std::string, name))

typedef std::map<std::pair<char, char>, size_t> Votes;
typedef std::vector<Candidate> Candidates;

template <typename It>
struct parser : qi::grammar<It>
{
mutable Votes _votes;
mutable Candidates _candidates;

parser() : parser::base_type(start)
{
using namespace qi;
using phx::bind; using phx::ref; using phx::val;

start = (line % eol) >> *eol >> eoi;

line =
vote [ phx::bind(&parser::register_vote, phx::ref(*this), _1) ]
| candidate [ phx::push_back(phx::ref(_candidates), _1) ]
;

vote %= graph
// Comment the following line to accept any single
// letter, even if a matching position wasn't seen
// before:
[ _pass = phx::bind(&parser::posletter_check, phx::ref(*this), _1) ]
>> ' '
>> char_("RD")
;

candidate = (string("Republican") | string("Democrat"))
>> ' '
>> as_string [ +graph ]
>> ' '
>> as_string [ +(char_ - eol) ]
;
}

private:
bool posletter_check(char posletter) const
{
for (auto& c : _candidates)
if (posletter == c.position[0])
return true;
return false;
}
void register_vote(Votes::key_type const& key) const
{
auto it = _votes.find(key);
if (_votes.end()==it)
_votes[key] = 1;
else
it->second++;
}

qi::rule<It, Votes::key_type()> vote;
qi::rule<It, Candidate()> candidate;
qi::rule<It> start, line;
};

int main()
{
const std::string input =
"Republican Senator John McMahon\n"
"Democrat Senator Anthony Quizitano\n"
"S R\n"
"S R\n"
"S R\n"
"Democrat Mayor Steven Markel\n"
"Republican Judge Matt Stevens\n"
"M D\n"
"J R\n"
"S R\n"
"S R\n";

std::string::const_iterator f(std::begin(input)), l(std::end(input));

parser<std::string::const_iterator> p;

try
{
bool ok = qi::parse(f,l,p);
if (ok)
{
std::cout << "\ncandidate list\n";
std::cout << "------------------------------------------------\n";
for (auto& c : p._candidates)
std::cout << std::setw(20) << c.name << " (" << c.position << " for the " << c.party << "s)\n";

std::cout << "\nVote distribution:\n";
std::cout << "------------------------------------------------\n";
for (auto& v : p._votes)
std::cout << '(' << v.first.first << "," << v.first.second << "): " << v.second << " votes " << std::string(v.second, '*') << "\n";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'\n";

if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
} catch(const qi::expectation_failure<std::string::const_iterator>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'\n";
}
}

输出:

candidate list
------------------------------------------------
John McMahon (Senator for the Republicans)
Anthony Quizitano (Senator for the Democrats)
Steven Markel (Mayor for the Democrats)
Matt Stevens (Judge for the Republicans)

Vote distribution:
------------------------------------------------
(J,R): 1 votes *
(M,D): 1 votes *
(S,R): 5 votes *****

关于c++ - 从文本文件中读取输入并提取它 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12805312/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com