temp

temp,第1张

概述QueryResult.h、TextQuery.h、TextQuery.cpp 和 make_plural.h 同练习 12.27。 Query.h #ifndef TEST_QUERY_H#define TEST_QUERY_H#include "TextQuery.h"#include <string>#include <set>#include <iostream>#inclu

queryResult.h、Textquery.h、Textquery.cpp 和 make_plural.h 同练习 12.27。

query.h

#ifndef TEST_query_H#define TEST_query_H#include "Textquery.h"#include <string>#include <set>#include <iostream>#include <fstream>#include <sstream>#include <memory>// abstract class acts as a base class for concrete query types; all members are privateclass query_base {    frIEnd class query;protected:    typedef Textquery::line_no line_no; // used in the eval functions    virtual ~query_base() {}private:    // eval returns the queryResult that matches this query    virtual queryResult eval(const Textquery &) const = 0;    // rep is a string representation of the query    virtual std::string rep() const = 0;};// interface class to manage the query_base inheritance hIErarchyclass query {    // these operators need access to the shared_ptr constructor    frIEnd query operator~(const query &);    frIEnd query operator|(const query &,const query &);    frIEnd query operator&(const query &,const query &);public:    query(const std::string &);  // builds a new Wordquery    // interface functions: call the corresponding query_base operations    queryResult eval(const Textquery &t) const { return q->eval(t); }    std::string rep() const { return q->rep(); }private:    query(std::shared_ptr<query_base> query) : q(query) {}    std::shared_ptr<query_base> q;};inlinestd::ostream &operator<<(std::ostream &os,const query &query) {    // query::rep makes a virtual call through its query_base pointer to rep()    return os << query.rep();}class Wordquery : public query_base {    frIEnd class query; // query uses the Wordquery constructor    Wordquery(const std::string &s) : query_word(s) {}    // concrete class: Wordquery defines all inherited pure virtual functions    queryResult eval(const Textquery &t) const { return t.query(query_word); }    std::string rep() const { return query_word; }    std::string query_word;   // word for which to search};inlinequery::query(const std::string &s) : q(new Wordquery(s)) {}class Notquery : public query_base {    frIEnd query operator~(const query &);    Notquery(const query &q) : query(q) {}    // concrete class: Notquery defines all inherited pure virtual functions    std::string rep() const { return "~(" + query.rep() + ")"; }    queryResult eval(const Textquery &) const;    query query;};class Binaryquery : public query_base {protected:    Binaryquery(const query &l,const query &r,std::string s) :            lhs(l),rhs(r),opSym(s) {}    // abstract class: Binaryquery doesn't define eval    std::string rep() const {        return "(" + lhs.rep() + " "               + opSym + " "               + rhs.rep() + ")";    }    query lhs,rhs;    // right- and left-hand operands    std::string opSym; // name of the operator};class Andquery : public Binaryquery {    frIEnd query operator&(const query &,const query &);    Andquery(const query &left,const query &right) :            Binaryquery(left,right,"&") {}    // concrete class: Andquery inherits rep and defines the remaining pure virtual    queryResult eval(const Textquery &) const;};class Orquery : public Binaryquery {    frIEnd query operator|(const query &,const query &);    Orquery(const query &left,"|") {}    queryResult eval(const Textquery &) const;};inline query operator&(const query &lhs,const query &rhs) {    return std::shared_ptr<query_base>(new Andquery(lhs,rhs));}inline query operator|(const query &lhs,const query &rhs) {    return std::shared_ptr<query_base>(new Orquery(lhs,rhs));}inline query operator~(const query &operand) {    return std::shared_ptr<query_base>(new Notquery(operand));}std::ifstream &open_file(std::ifstream &,const std::string &);Textquery get_file(int,char **);bool get_word(std::string &);bool get_words(std::string &,std::string &);std::ostream &print(std::ostream &,const queryResult &);#endif //TEST_query_H

query.cpp

#include "query.h"#include "Textquery.h"#include <memory>using std::shared_ptr;#include <set>using std::set;#include <algorithm>using std::set_intersection;#include <iostream>using std::ostream;#include <cstddef>using std::size_t;#include <iterator>using std::inserter;#include <vector>using std::vector;#include <string>using std::string;// returns the lines not in its operand's result setqueryResultNotquery::eval(const Textquery &text) const {    // virtual call to eval through the query operand    queryResult result = query.eval(text);    // start out with an empty result set    shared_ptr<set<line_no> > ret_lines(new set<line_no>);    // we have to iterate through the lines on which our operand appears    queryResult::line_it beg = result.begin(),end = result.end();    // for each line in the input file,if that line is not in result,// add that line number to ret_lines    vector<string>::size_type sz = result.get_file()->size();    for (size_t n = 0; n != sz; ++n) {        // if we haven't processed all the lines in result        // check whether this line is present        if (beg == end || *beg != n)            ret_lines->insert(n);  // if not in result,add this line        else if (beg != end)            ++beg; // otherwise get the next line number in result if there is one    }    return queryResult(rep(),ret_lines,result.get_file());}// returns the intersection of its operands' result setsqueryResultAndquery::eval(const Textquery &text) const {    // virtual calls through the query operands to get result sets for the operands    queryResult left = lhs.eval(text),right = rhs.eval(text);    // set to hold the intersection of left and right    shared_ptr<set<line_no> > ret_lines(new set<line_no>);    // writes the intersection of two ranges to a destination iterator    // destination iterator in this call adds elements to ret    set_intersection(left.begin(),left.end(),right.begin(),right.end(),inserter(*ret_lines,ret_lines->begin()));    return queryResult(rep(),left.get_file());}// returns the union of its operands' result setsqueryResultOrquery::eval(const Textquery &text) const {    // virtual calls through the query members,lhs and rhs    // the calls to eval return the queryResult for each operand    queryResult right = rhs.eval(text),left = lhs.eval(text);    // copy the line numbers from the left-hand operand into the result set    shared_ptr<set<line_no> >            ret_lines(new set<line_no>(left.begin(),left.end()));    // insert lines from the right-hand operand    ret_lines->insert(right.begin(),right.end());    // return the new queryResult representing the union of lhs and rhs    return queryResult(rep(),left.get_file());}

get_print.cpp

#include "query.h"#include "Textquery.h"#include <string>using std::string;#include <iostream>using std::cout; using std::cin;#include <fstream>using std::ifstream;#include <stdexcept>using std::runtime_error;// these functions are declared in query.hTextquery get_file(int argc,char **argv) {    // get a file to read from which user will query words    ifstream infile;    if (argc == 2)        infile.open(argv[1]);    if (!infile) {        throw runtime_error("No input file!");    }    return Textquery(infile);  // builds query map}bool get_word(string &s1) {    cout << "enter a word to search for,or q to quit: ";    cin >> s1;    if (!cin || s1 == "q") return false;    else return true;}bool get_words(string &s1,string &s2) {    // iterate with the user: prompt for a word to find and print results    cout << "enter two words to search for,or q to quit: ";    cin >> s1;    // stop if hit eof on input or a "q" is entered    if (!cin || s1 == "q") return false;    cin >> s2;    return true;}

and_orqueryTest.cpp

#include "query.h"#include "Textquery.h"#include <string>#include <set>#include <iostream>using std::set;using std::string;using std::cin; using std::cout; using std::cerr;using std::endl;int main(int argc,char **argv){    // gets file to read and builds map to support querIEs    Textquery file = get_file(argc,argv);    // iterate with the user: prompt for a word to find and print results    while (true) {        string sought1,sought2,sought3;        if (!get_words(sought1,sought2)) break;        cout << "\nenter third word: " ;        cin  >> sought3;        // find all the occurrences of the requested string        query q = query(sought1) & query(sought2)                  | query(sought3);        cout << "\nExecuting query for: " << q << endl;        const queryResult results = q.eval(file);        // report matches        print(cout,results);    }    return 0;}

运行程序前,在 Clion -> Run -> Edit Configurations 下配置 Program arguments 为 ../data

注:../data 即为文件 data 的文件名及其相对路径(是相对于可执行程序所在目录的相对路径)。

并在文件 data 中写入如下内容:

Alice emma has long flowing red hair.Her Daddy says when the wind blowsthrough her hair,it looks almost alive,like a fIEry bird in flight.A beautiful fIEry bird,he tells her,magical but untamed."Daddy,shush,there is no such thing,"she tells him,at the same time wantinghim to tell her more.Shyly,she asks,"I mean,Daddy,is there?"

运行程序,程序执行结果如下所示:

// 运行结果enter two words to search for,or q to quit: fIEry birdenter third word: windExecuting query for: ((fIEry & bird) | wind)((fIEry & bird) | wind) occurs 3 times    (line 2)Her Daddy says when the wind blows    (line 4)like a fIEry bird in flight.    (line 5)A beautiful fIEry bird,enter two words to search for,or q to quit: qProcess finished with exit code 0

下面是 andqueryTest.cpp 和 wordqueryTest.cpp 测试程序及其执行结果:

andqueryTest.cpp

#include "query.h"#include "Textquery.h"#include <string>using std::string;#include <iostream>using std::cout; using std::endl;#include <set>using std::set;int main(int argc,char **argv) {    // gets file to read and builds map to support querIEs    Textquery file = get_file(argc,argv);    do {        string sought1,sought2;        // stop if hit eof on input or a "q" is entered        if (!get_words(sought1,sought2)) break;        // find all the occurrences of the requested string        query andq = query(sought1) & query(sought2);        cout << "\nExecuting query for: " << andq << endl;        queryResult results = andq.eval(file);        // report matches        print(cout,results);        results = query(sought1).eval(file);        cout << "\nExecuted query: " << query(sought1) << endl;        // report matches        print(cout,results);        results = query(sought2).eval(file);        cout << "\nExecuted query: " << query(sought2) << endl;        // report matches        print(cout,results);    } while (true);    return 0;}
// 运行结果enter two words to search for,or q to quit: hair AliceExecuting query for: (hair & Alice)(hair & Alice) occurs 1 time    (line 1)Alice emma has long flowing red hair.Executed query: hairhair occurs 2 times    (line 1)Alice emma has long flowing red hair.    (line 3)through her hair,Executed query: AliceAlice occurs 1 time    (line 1)Alice emma has long flowing red hair.enter two words to search for,or q to quit: qProcess finished with exit code 0

wordqueryTest.cpp

#include "query.h"#include "Textquery.h"#include <string>#include <vector>#include <map>#include <set>#include <iostream>#include <fstream>#include <cctype>#include <cstring>using std::set;using std::string;using std::map;using std::vector;using std::cerr;using std::cout;using std::cin;using std::ifstream;using std::endl;int main(int argc,char **argv) {    Textquery file = get_file(argc,argv);    // iterate with the user: prompt for a word to find and print results    do {        string sought;        if (!get_word(sought)) break;        // find all the occurrences of the requested string        // define synonym for the line_no set         query name(sought);        const queryResult results = name.eval(file);        cout << "\nExecuting query for: " << name << endl;        // report no matches        print(cout,results) << endl;    } while (true);  // loop indefinitely; the exit is insIDe the loop    return 0;}
// 运行结果enter a word to search for,or q to quit: DaddyExecuting query for: DaddyDaddy occurs 3 times    (line 2)Her Daddy says when the wind blows    (line 7)"Daddy,"    (line 10)Shyly,is there?"enter a word to search for,or q to quit: qProcess finished with exit code 0
总结

以上是内存溢出为你收集整理的temp全部内容,希望文章能够帮你解决temp所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1222741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存