Skip to content

Commit

Permalink
15.30-15.35
Browse files Browse the repository at this point in the history
  • Loading branch information
jzplp committed Aug 1, 2019
1 parent c250107 commit 8620413
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Chapter-15/15.30/Quote.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <memory>
#include "Quote.h"

double Quote::net_price(std::size_t n) const
{
return n * price;
}

double print_total(std::ostream & os, const Quote & item, std::size_t n)
{
double ret = item.net_price(n);
os << "ISBN: " << item.isbn() << " # sold: " << n << " total due: " << ret << std::endl;
return ret;
}

double Bulk_quote::net_price(std::size_t cnt) const
{
if(cnt >= quantity)
return cnt * (1 - discount) * price;
else
return cnt * price;
}

std::ostream & Quote::debug(std::ostream & os) const
{
os << bookNo << " " << price;
return os;
}

std::ostream & Disc_quote::debug(std::ostream & os) const
{
this->Quote::debug(os) << " " << quantity << " " << discount;
return os;
}

double Basket::total_receipt(std::ostream &os) const
{
double sum = 0.0;
for(std::multiset<std::shared_ptr<Quote>, decltype(compare)*>::const_iterator iter = items.cbegin(); iter != items.cend(); iter = items.upper_bound(*iter))
sum += print_total(os, **iter, items.count(*iter));
os << "Total Sales: " << sum << std::endl;
return sum;
}
78 changes: 78 additions & 0 deletions Chapter-15/15.30/Quote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef QUOTE_H
#define QUOTE_H

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

class Quote
{
public:
Quote() = default;
Quote(const std::string &book, double sales_price) : bookNo(book), price(sales_price) { }
std::string isbn() const { return bookNo; }

virtual double net_price(std::size_t n) const;
virtual ~Quote() = default;
virtual std::ostream & debug(std::ostream & os) const;

virtual Quote * clone() const & { return new Quote(*this); }
virtual Quote * clone() && { return new Quote(std::move(*this)); }

Quote(const Quote &) = default;
Quote(Quote &&) = default;
Quote & operator=(const Quote &) = default;
Quote & operator=(Quote &&) = default;

private:
std::string bookNo;

protected:
double price = 0.0;
};

class Disc_quote : public Quote
{
public:
Disc_quote() = default;
Disc_quote(const std::string & book, double price, std::size_t qty, double disc) : Quote(book, price), quantity(qty), discount(disc) { }

std::ostream & debug(std::ostream & os) const override;
double net_price(std::size_t) const = 0;

protected:
std::size_t quantity = 0;
double discount = 0.0;
};

class Bulk_quote : public Disc_quote
{
public:
Bulk_quote() = default;
Bulk_quote(const std::string & book, double price, std::size_t qty, double disc) : Disc_quote(book, price, qty, disc) { }

Bulk_quote * clone() const & override { return new Bulk_quote(*this); }
Bulk_quote * clone() && override { return new Bulk_quote(std::move(*this)); }

double net_price(std::size_t) const override;
};

double print_total(std::ostream & os, const Quote & item, std::size_t cnt);

class Basket
{
public:
void add_item(const Quote &sale){ items.insert(std::shared_ptr<Quote>(sale.clone())); }
void add_item(Quote &&sale){ items.insert(std::shared_ptr<Quote>(std::move(sale).clone())); }
double total_receipt(std::ostream &) const;

private:
static bool compare(const std::shared_ptr<Quote> &lhs, const std::shared_ptr<Quote> &rhs)
{ return lhs->isbn() < rhs->isbn(); }

std::multiset<std::shared_ptr<Quote>, decltype(compare)*> items{compare};
};


#endif
19 changes: 19 additions & 0 deletions Chapter-15/15.30/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <memory>
#include "Quote.h"

int main()
{
Basket b;
b.add_item(Quote("qwe", 10));
b.add_item(Quote("qwe", 10));
b.add_item(Bulk_quote("wer", 20, 2, 0.2));
b.add_item(Bulk_quote("wer", 20, 2, 0.2));
b.add_item(Bulk_quote("wer", 20, 2, 0.2));
b.add_item(Bulk_quote("ert", 20, 2, 0.2));
b.add_item(Quote("rty", 10));
b.total_receipt(std::cout);

return 0;
}

22 changes: 22 additions & 0 deletions Chapter-15/15.35/Query.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<string>
#include<memory>
#include "TextQuery.h"
#include "Query.h"

Query::Query(const std::string &s) : q(new WordQuery(s)) { }

Query operator~(const Query &operand)
{
return std::shared_ptr<Query_base>(new NotQuery(operand));
}

Query operator&(const Query &lhs, const Query &rhs)
{
return std::shared_ptr<Query_base>(new AndQuery(lhs, rhs));
}

Query operator|(const Query &lhs, const Query &rhs)
{
return std::shared_ptr<Query_base>(new OrQuery(lhs, rhs));
}

82 changes: 82 additions & 0 deletions Chapter-15/15.35/Query.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#ifndef QUERY_H
#define QUERY_H

#include<string>
#include<memory>
#include "TextQuery.h"

class Query_base
{
friend class Query;
protected:
using line_no = TextQuery::line_no;
virtual ~Query_base() = default;
private:
virtual QueryResult eval(const TextQuery &) const = 0;
virtual std::string rep() const = 0;
};

class Query
{
friend Query operator~(const Query &);
friend Query operator|(const Query &, const Query &);
friend Query operator&(const Query &, const Query &);
public:
Query(const std::string &);
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;
};

class WordQuery : public Query_base
{
friend class Query;
WordQuery(const std::string &s) : query_word(s) { }
QueryResult eval(const TextQuery &t) const { return t.query(query_word); }
std::string rep() const { return query_word; }

std::string query_word;
};

class NotQuery : public Query_base
{
friend Query operator~(const Query &);
NotQuery(const Query &q) : query(q) { }
std::string rep() const { return "~(" + query.rep() + ")"; }
//todo Test substitution function
QueryResult eval(const TextQuery &t) const { return t.query(query.rep()); }
Query query;
};

class BinaryQuery : public Query_base
{
protected:
BinaryQuery(const Query &l, const Query &r, std::string s) : lhs(l), rhs(r), opSym(s) { }
std::string rep() const { return "(" + lhs.rep() + " " + opSym + " " + rhs.rep() + ")"; }
Query lhs, rhs;
std::string opSym;
};

class AndQuery : public BinaryQuery
{
friend Query operator&(const Query &, const Query &);
AndQuery(const Query &left, const Query &right) : BinaryQuery(left, right, "&") { }
//todo Test substitution function
QueryResult eval(const TextQuery &t) const { return t.query("temp"); }
};

class OrQuery : public BinaryQuery
{
friend Query operator|(const Query &, const Query &);
OrQuery(const Query &left, const Query &right) : BinaryQuery(left, right, "|") { }
//todo Test substitution function
QueryResult eval(const TextQuery &t) const { return t.query("temp"); }
};

Query operator~(const Query &);
Query operator|(const Query &, const Query &);
Query operator&(const Query &, const Query &);

#endif
67 changes: 67 additions & 0 deletions Chapter-15/15.35/TextQuery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include<iostream>
#include<fstream>
#include<memory>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<sstream>
#include "TextQuery.h"

std::string make_plural(size_t ctr, const std::string &word, const std::string &ending)
{
return (ctr > 1) ? word + ending : word;
}

TextQuery::TextQuery(std::ifstream & is) : file(new std::vector<std::string>)
{
std::string text;
while(std::getline(is, text))
{
file->push_back(text);
int n = file->size()-1;
std::istringstream line(text);
std::string word;
while(line >> word)
{
std::shared_ptr<std::set<line_no>> &lines = wm[word];
if(!lines)
lines.reset(new std::set<line_no>);
lines->insert(n);
}
}
}

QueryResult TextQuery::query(const std::string & sought) const
{
static std::shared_ptr<std::set<line_no>> nodata(new std::set<line_no>);
std::map<std::string, std::shared_ptr<std::set<line_no>>>::const_iterator loc = wm.find(sought);
if(loc == wm.cend())
return QueryResult(sought, nodata, file);
else
return QueryResult(sought, loc->second, file);
}

std::set<TextQuery::line_no>::iterator QueryResult::begin()
{
return lines->begin();
}

std::set<TextQuery::line_no>::iterator QueryResult::end()
{
return lines->end();
}

std::shared_ptr<std::vector<std::string>> QueryResult::get_file()
{
return file;
}

std::ostream & print(std::ostream & os, const QueryResult &qr)
{
os << qr.sought << " occurs " << qr.lines->size() << " " << make_plural(qr.lines->size(), "time", "s") << std::endl;
for(TextQuery::line_no num : *qr.lines)
os << "\t(line " << num+1 << ") " << *(qr.file->begin() + num) << std::endl;
return os;
}

44 changes: 44 additions & 0 deletions Chapter-15/15.35/TextQuery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef TEXTQUERY_H
#define TEXTQUERY_H

#include<iostream>
#include<fstream>
#include<memory>
#include<vector>
#include<set>
#include<map>
#include<string>

class QueryResult;

class TextQuery
{
public:
using line_no = std::vector<std::string>::size_type;
TextQuery(std::ifstream &);
QueryResult query(const std::string &) const;
TextQuery(const TextQuery & tq) = default;
TextQuery & operator=(const TextQuery & tq) = default;
~TextQuery() = default;

private:
std::shared_ptr<std::vector<std::string>> file;
std::map<std::string, std::shared_ptr<std::set<line_no>>> wm;
};

class QueryResult
{
friend std::ostream & print(std::ostream &, const QueryResult &);
public:
QueryResult(std::string s, std::shared_ptr<std::set<TextQuery::line_no>> p, std::shared_ptr<std::vector<std::string>> f) : sought(s), lines(p), file(f) { }
std::set<TextQuery::line_no>::iterator begin();
std::set<TextQuery::line_no>::iterator end();
std::shared_ptr<std::vector<std::string>> get_file();
private:
std::string sought;
std::shared_ptr<std::set<TextQuery::line_no>> lines;
std::shared_ptr<std::vector<std::string>> file;
};

#endif

22 changes: 22 additions & 0 deletions Chapter-15/15.35/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include<iostream>
#include<string>
#include<fstream>
#include "TextQuery.h"
#include "Query.h"

int main()
{
std::ifstream ifs("C:\\Users\\jz\\Desktop\\1.txt");
TextQuery tq1(ifs);
std::string s1, s2;
std::cin >> s1 >> s2;
Query q1(s1), q2(s2);
Query qand = q1 & q2;
Query qor = q1 | q2;
print(std::cout, q1.eval(tq1)) << std::endl;
print(std::cout, (~q1).eval(tq1)) << std::endl;
print(std::cout, qand.eval(tq1)) << std::endl;
print(std::cout, qor.eval(tq1)) << std::endl;
std::cout << q1.rep() << " " << q2.rep() << " " << qand.rep() << " " << qor.rep() << std::endl;
return 0;
}
Loading

0 comments on commit 8620413

Please sign in to comment.