Created
October 29, 2015 18:15
-
-
Save krysseltillada/6ed6d1e8fa8ac3688911 to your computer and use it in GitHub Desktop.
own implementation of std::tuple, std::pair, std::equal_range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#include "Sales_data.hpp" | |
template <typename type1, typename type2> | |
class Double { | |
public: | |
Double (const type1 &t1, const type2 &t2) : | |
typ1 (t1), typ2 (t2) { } | |
Double (const Double &cObj) : | |
typ1 (cObj.typ1), typ2 (cObj.typ2) { } | |
type1 typ1; | |
type2 typ2; | |
}; | |
template <class ForwardIterator, class T> | |
Double <ForwardIterator,ForwardIterator> | |
Equal_range (ForwardIterator first, ForwardIterator last, const T& val) | |
{ | |
ForwardIterator it = std::lower_bound (first,last,val); | |
return Double <ForwardIterator, ForwardIterator> ( it, std::upper_bound(it,last,val) ); | |
} | |
class Result { | |
public: | |
Result () = default; | |
Result (std::vector <Sales_data>::size_type i, | |
std::vector <Sales_data>::const_iterator b, | |
std::vector <Sales_data>::const_iterator e) : | |
ind (i), beg (b), end (e) { } | |
std::vector <Sales_data>::size_type ind; | |
std::vector <Sales_data>::const_iterator beg; | |
std::vector <Sales_data>::const_iterator end; | |
}; | |
std::vector <Result> | |
findBook (const std::vector <std::vector <Sales_data> > &files, | |
const std::string &book) | |
{ | |
std::vector <Result> ret; | |
for (auto it = files.cbegin (); it != files.cend (); ++it) { | |
Double <std::vector <Sales_data>::const_iterator, | |
std::vector <Sales_data>::const_iterator> result ( Equal_range (it->cbegin (), it->cend (), | |
book)); | |
if (result.typ1 != result.typ2) { | |
ret.push_back (Result (it - files.cbegin (), result.typ1, result.typ2)); | |
} | |
} | |
return ret; | |
} | |
void reportResults (std::istream &in, std::ostream &os, | |
const std::vector <std::vector <Sales_data> > &files) | |
{ | |
std::string s; | |
while (in >> s) { | |
auto trans = findBook (files, s); | |
if (trans.empty ()) { | |
std::cout << s << " not found in any stores " << std::endl; | |
continue; | |
} | |
for (auto it = trans.begin (); it != trans.end (); ++it) { | |
std::cout << "store: " << it->ind << "Sales: " << std::accumulate (it->beg, it->end, Sales_data ()); | |
} | |
} | |
} | |
int main () | |
{ | |
std::vector <Sales_data> tmpData; | |
std::vector <std::vector <Sales_data> > files; | |
std::string bookName = ""; | |
double revenue = 0; | |
unsigned units_sold = 0; | |
char key; | |
while (std::cin >> bookName) { | |
std::cin >> revenue; | |
std::cin >> units_sold; | |
tmpData.push_back (Sales_data(bookName, units_sold, revenue)); | |
std::cout << "enter again? y or n" << std::endl; | |
std::cin >> key; | |
if (key == 'n') break; | |
else if (key == 'y') continue; | |
else { std::cout << "error" << std::endl; continue; } | |
} | |
files.push_back (tmpData); | |
reportResults(std::cin, std::cout, files); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SALES_DATA_H | |
#define SALES_DATA_H | |
#include <string> | |
#include <iostream> | |
class Sales_data { | |
friend class std::hash <Sales_data>; | |
friend Sales_data add(const Sales_data&, const Sales_data&); | |
friend std::ostream &print(std::ostream&, const Sales_data&); | |
friend std::istream &read(std::istream&, Sales_data&); | |
friend std::ostream &operator << (std::ostream &, const Sales_data &); | |
friend std::istream &operator >> (std::istream &, Sales_data &); | |
friend bool operator < (const Sales_data &lhs, const Sales_data &rhs); | |
friend Sales_data operator + (const Sales_data &, const Sales_data &); | |
friend Sales_data operator - (const Sales_data &, const Sales_data &); | |
friend Sales_data operator * (const Sales_data &, const Sales_data &); | |
friend Sales_data operator / (const Sales_data &, const Sales_data &); | |
public: | |
Sales_data() : | |
bookNo (std::string ("")), units_sold (0), revenue (0) { } | |
Sales_data(const std::string &s): | |
bookNo(s) { | |
} | |
Sales_data(const std::string &s, unsigned n, double p): | |
bookNo(s), units_sold(n), revenue(p*n) { | |
} | |
Sales_data(std::istream &); | |
std::string isbn() const { | |
return bookNo; | |
} | |
Sales_data &operator = (const Sales_data &); | |
Sales_data &operator += (const Sales_data &); | |
void operator -= (const Sales_data &); | |
void operator *= (const Sales_data &); | |
void operator /= (const Sales_data &); | |
Sales_data& combine(const Sales_data&); | |
double avg_price() const; | |
private: | |
std::string bookNo; | |
unsigned units_sold = 0; | |
double revenue = 0.0; | |
}; | |
double Sales_data::avg_price () const { | |
return units_sold / revenue; | |
} | |
bool operator < (const Sales_data &lhs, const Sales_data &rhs) { | |
return lhs.bookNo < rhs.bookNo; | |
} | |
Sales_data &Sales_data::operator += (const Sales_data &lhs) { | |
units_sold += lhs.units_sold; | |
revenue += lhs.revenue; | |
return *this; | |
} | |
void Sales_data::operator -= (const Sales_data &lhs) { | |
this->units_sold -= lhs.units_sold; | |
this->revenue -= lhs.revenue; | |
} | |
void Sales_data::operator *= (const Sales_data &lhs) { | |
this->units_sold *= lhs.units_sold; | |
this->revenue *= lhs.revenue; | |
} | |
void Sales_data::operator /= (const Sales_data &lhs) { | |
this->units_sold /= lhs.units_sold; | |
this->revenue /= lhs.revenue; | |
} | |
Sales_data operator + (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data sum = lhs; | |
sum += rhs; | |
return sum; | |
} | |
Sales_data operator - (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data diff = lhs; | |
diff -= rhs; | |
return diff; | |
} | |
Sales_data operator * (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data product = lhs; | |
product *= rhs; | |
return product; | |
} | |
Sales_data operator / (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data quotient = lhs; | |
quotient /= lhs; | |
return quotient; | |
} | |
Sales_data &Sales_data::operator = (const Sales_data &c_obj) { | |
this->bookNo = c_obj.bookNo; | |
this->units_sold = c_obj.units_sold; | |
this->revenue = c_obj.revenue; | |
return *this; | |
} | |
std::ostream &operator << (std::ostream &out, const Sales_data &rho) { | |
out << rho.isbn () << " " << rho.units_sold << " " | |
<< rho.revenue << " " << rho.avg_price(); | |
return out; | |
} | |
std::istream &operator >> (std::istream &in, Sales_data &rho) { | |
double t_price; | |
in >> rho.bookNo >> rho.units_sold >> t_price; | |
if (in) | |
rho.revenue = rho.units_sold * t_price; | |
else | |
rho = Sales_data (); | |
return in; | |
} | |
Sales_data add(const Sales_data&, const Sales_data&); | |
std::ostream &print(std::ostream&, const Sales_data&); | |
std::istream &read(std::istream&, Sales_data&); | |
inline | |
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) | |
{ | |
return lhs.isbn() < rhs.isbn(); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment