Skip to content

Commit

Permalink
A solution for exercise 17.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Queequeg92 committed Nov 18, 2014
1 parent 21f8cf2 commit 0ad787d
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 3 deletions.
155 changes: 155 additions & 0 deletions ch17/ex17.4.5.6.7.8/Sales_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/


#include <iostream>
using std::istream; using std::ostream;

#include "Sales_data.h"
Sales_data::Sales_data(std::istream &is)
{
// read will read a transaction from is into this object
read(is, *this);
}

double
Sales_data::avg_price() const {
if (units_sold)
return revenue/units_sold;
else
return 0;
}

// add the value of the given Sales_data into this object
Sales_data&
Sales_data::combine(const Sales_data &rhs)
{
units_sold += rhs.units_sold; // add the members of rhs into
revenue += rhs.revenue; // the members of ``this'' object
return *this; // return the object on which the function was called
}
//! = Sales_data
Sales_data &Sales_data::operator =(const Sales_data &rhs)
{
this->bookNo = rhs.bookNo;
this->revenue = rhs.revenue;
this->units_sold = rhs.units_sold;

return *this;
}

//! =string
Sales_data &Sales_data::operator =(const std::string &rhs)
{
*this= Sales_data(rhs);
return *this;
}

//! +=
Sales_data &Sales_data::operator +=(const Sales_data &rhs)
{
this->revenue += rhs.revenue;
this->units_sold += rhs.units_sold;

return *this;
}

Sales_data
add(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs; // copy data members from lhs into sum
sum.combine(rhs); // add data members from rhs into sum
return sum;
}

// transactions contain ISBN, number of copies sold, and sales price
istream&
read(istream &is, Sales_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> price;
item.revenue = price * item.units_sold;
return is;
}

ostream&
print(ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price();
return os;
}

//! added 10.Jan 2014
std::ostream &
operator <<(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " "
<< item.revenue << " " << item.avg_price();

return os;
}

//! added 12.Jan 2014
std::istream&
operator >>(std::istream &is, Sales_data &s)
{
double price;

//! read input
is >> s.bookNo >> s.units_sold >> price;

//! if successful, write into the object, give the object default state otherwise.
if(is)
s.revenue = s.units_sold * price;
else
s = Sales_data();

return is;
}




















121 changes: 121 additions & 0 deletions ch17/ex17.4.5.6.7.8/Sales_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/

//!
//! Exercise 14.45:
//! Write conversion operators to convert a Sales_data to string and to double.
//! What values do you think these operators should return?
//!
#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <string>
#include <iostream>


class Sales_data
{
//! friends
friend Sales_data operator+(const Sales_data& lhs, const Sales_data& rhs);

friend std::ostream&
operator << (std::ostream& os, const Sales_data& s);

friend std::istream&
operator >> (std::istream& is, Sales_data& s);

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&);

public:
// constructors
Sales_data() = default;
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(const Sales_data &s ):
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
{}

Sales_data(Sales_data&& s):
bookNo(s.bookNo), units_sold(s.units_sold), revenue(s.revenue)
{}

~Sales_data(){}
Sales_data(std::istream &);

std::string isbn() const { return bookNo; }
Sales_data& combine(const Sales_data&);

//! assignments
Sales_data& operator =(const Sales_data& rhs);
Sales_data& operator =(const std::string& rhs);
Sales_data& operator +=(const Sales_data& rhs);

//! conversion
explicit operator std::string () const { return bookNo; }
explicit operator double () const { return revenue;}

double avg_price() const;
private:
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};


//! overloaded operators added 10.Jan.2014 for ex14.2
inline Sales_data
operator+(const Sales_data& lhs, const Sales_data& rhs)
{
Sales_data sum = lhs;
sum += rhs;

return sum;
}

std::ostream&
operator << (std::ostream& os, const Sales_data& item);

std::istream&
operator >> (std::istream& is, Sales_data& s);

// nonmember Sales_data interface functions
Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istream&, Sales_data&);

// used in future chapters
inline
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
return lhs.isbn() < rhs.isbn();
}
#endif
40 changes: 37 additions & 3 deletions ch17/ex17.4.5.6.7.8/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>

#include "Sales_data.h"

Expand All @@ -53,6 +54,18 @@ typedef std::pair<std::vector<Sales_data>::size_type,
std::vector<Sales_data>::const_iterator>>
matches_pair;

//! for ex17.6
//! return a struct that holds an index of a store and iterators into that store's vector
struct matches_struct
{
std::vector<Sales_data>::size_type st;
std::vector<Sales_data>::const_iterator first;
std::vector<Sales_data>::const_iterator last;
matches_struct(std::vector<Sales_data>::size_type s,
std::vector<Sales_data>::const_iterator f,
std::vector<Sales_data>::const_iterator l) : st(s), first(f), last(l) {}
} ;

//! for ex17.4
//! return a vector with an entry for each store that sold the given book.
std::vector<matches>
Expand All @@ -64,15 +77,20 @@ void reportResults(std::istream& in, std::ostream os,
const std::vector<std::vector<Sales_data>>& files);

//! for ex17.5
//! return a vector with an entry for each store that the given book
//! return a vector with an entry for each store that sold the given book.
std::vector<matches_pair>
findBook_pair(const std::vector<std::vector<Sales_data> > &files,
const std::string &book);

//! for ex17.6
//! return a vector with an entry for each store that sold the given book.
std::vector<matches_struct>
findBook_struct(const std::vector<std::vector<Sales_data> > &files,
const std::string &book);

int main()
{

return 0;
}

//! for ex17.4
Expand Down Expand Up @@ -118,7 +136,7 @@ void reportResults(std::istream& in, std::ostream os,
}

//! for ex17.5
//! return a vector with an entry for each store that the given book
//! return a vector with an entry for each store that sold the given book
std::vector<matches_pair>
findBook_pair(const std::vector<std::vector<Sales_data> > &files,
const std::string &book)
Expand All @@ -133,3 +151,19 @@ findBook_pair(const std::vector<std::vector<Sales_data> > &files,
}
return ret;
}

//! for ex17.6
//! return a vector with an entry for each store that sold the given book.
std::vector<matches_struct>
findBook_struct(const std::vector<std::vector<Sales_data> > &files,
const std::string &book)
{
std::vector<matches_struct> ret;
for(auto it = files.cbegin(); it != files.cend(); ++it)
{
auto found = std::equal_range(it->cbegin(), it->cend(), book, compareIsbn);
if(found.first != found.second)
ret.push_back(matches_struct(it - files.cbegin(), found.first, found.second));
}
return ret;
}

0 comments on commit 0ad787d

Please sign in to comment.