簡體   English   中英

從文本文件讀取輸入並將其提取-C ++

[英]Reading input from a text file and extracting it - C++

我正在嘗試逐行讀取文本文件並將數據提取到程序中。 本質上,C ++程序將讀取以下格式的輸入文件:

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

格式基本上是前三行,包括“聚會”,“職位”和“姓名”,接下來的幾行表示“結果”,其形式為:

[第一份立場書] [當事方投票]

因此,例如,如果您看到SR,則表示參議員需要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" "SR\\n" "SR\\n" "SR\\n" "Democrat Mayor Steven Markel\\n" "Republican Judge Matt Stevens\\n" "MD\\n" "JR\\n" "SR\\n" "SR\\n"; 

    但是,該代碼可同時用於兩種目的。

  • 我以輸入顯示的順序使其無關緊要。
  • 但是,您可以選擇斷言單個字母(S,M,J)實際上與該點之前列出的位置相對應。 通過使用posletter_check取消對支票的注釋來啟用此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 *****

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM