Skip to content

Commit

Permalink
finished 17.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Jun 28, 2017
1 parent 548e4b4 commit e33e2d8
Show file tree
Hide file tree
Showing 46 changed files with 435 additions and 376 deletions.
2 changes: 1 addition & 1 deletion ch02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ int main()
> Write your own version of the `Sales_data.h` header and use it to rewrite the ## Exercise from 2.6.2(p. 76)
- [Sales_data.h](ex2_42.h)
- [Sales_data.h](ex2_42_sales_data.h)
- [1.5.1.](ex2_42_1.cpp)
- [1.5.2.](ex2_42_2.cpp)
- [1.6.](ex2_42_3.cpp)
3 changes: 2 additions & 1 deletion ch02/ex2_42_1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include "ex2_42.h"

#include "ex2_42_sales_data.h"

int main()
{
Expand Down
3 changes: 2 additions & 1 deletion ch02/ex2_42_2.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include "ex2_42.h"

#include "ex2_42_sales_data.h"

int main()
{
Expand Down
3 changes: 2 additions & 1 deletion ch02/ex2_42_3.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include "ex2_42.h"

#include "ex2_42_sales_data.h"

int main()
{
Expand Down
5 changes: 3 additions & 2 deletions ch02/ex2_42.h → ch02/ex2_42_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <string>
#include <iostream>
#include <string>

// own Sales_data
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
Expand Down Expand Up @@ -48,4 +49,4 @@ void Sales_data::Print()
std::cout << averagePrice << std::endl;
else
std::cout << "(no sales)" << std::endl;
}
}
20 changes: 13 additions & 7 deletions ch07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

## [Exercise 7.1](ex7_01.cpp)

## [Exercise 7.2](ex7_02.h)
## [Exercise 7.2](ex7_02_sales_data.h)

## [Exercise 7.3](ex7_03.cpp)

## [Exercise 7.4](ex7_04.h)

## [Exercise 7.5](ex7_05.h)

## [Exercise 7.6](ex7_06.h)
## [Exercise 7.6](ex7_06_sales_data.h)

## [Exercise 7.7](ex7_07.cpp)

Expand Down Expand Up @@ -38,9 +38,11 @@ if (secondStep)

the condition of the `if` statement would read two `Sales_data` object at one time.

## Exercise 7.11 [Header](ex7_11.h)|[CPP](ex7_11.cpp)
## Exercise 7.11

## [Exercise 7.12](ex7_12.h)
[Sales_data Definition](ex7_11_sales_data.h) | [Sales_data Implementation](ex7_11_sales_data.cpp)

## [Exercise 7.12](ex7_12_sales_data.h)

## [Exercise 7.13](ex7_13.cpp)

Expand Down Expand Up @@ -97,7 +99,7 @@ the interface should be defined as public, the data shouldn't expose to outside
- lessens encapsulation and therefore maintainability.
- code verbosity, declarations inside the class, outside the class.
## [Exercise 7.21](ex7_21.h)
## [Exercise 7.21](ex7_21_sales_data.h)
## [Exercise 7.22](ex7_22.h)
Expand All @@ -115,7 +117,9 @@ The class below can rely on it. It goes in *Section 7.1.5*:
Hence the class below which used only built-in type and strings can rely on the default version of copy and assignment. (by [@Mooophy](https://github.com/Mooophy)\)
## Exercise 7.26 [Header](ex7_26.h)|[CPP](ex7_26.cpp)
## Exercise 7.26
[Sales_data Definition](ex7_26_sales_data.h) | [Sales_data Implementation](ex7_26_sales_data.cpp)
## Exercise 7.27 [Class](ex7_27.h)|[Test](ex7_27_TEST.cpp)
Expand Down Expand Up @@ -279,7 +283,9 @@ private:
};
```

## Exercise 7.41 [Header](ex7_41.h)|[CPP](ex7_41.cpp)|[Test](ex7_41_TEST.cpp)
## Exercise 7.41

[Sales_data Definition](ex7_41_sales_data.h) | [Sales_data Implementation](ex7_41_sales_data.cpp)|[Sales_data Test](ex7_41_sales_data_test.cpp)

## Exercise 7.42

Expand Down
37 changes: 7 additions & 30 deletions ch07/ex7_01.cpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,19 @@
//
// ex7_01.cpp
// Exercise 7.1
//
// Created by pezy on 14/10/30.
// Copyright (c) 2014 pezy. All rights reserved.
//

#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;

struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
#include "../ch02/ex2_42_sales_data.h"

int main()
{
Sales_data total;
if (cin >> total.bookNo >> total.units_sold >> total.revenue) {
if (std::cin >> total.bookNo >> total.units_sold >> total.revenue) {
Sales_data trans;
while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {
if (total.bookNo == trans.bookNo) {
total.units_sold += trans.units_sold;
total.revenue += trans.revenue;
}
while (std::cin >> trans.bookNo >> trans.units_sold >> trans.revenue) {
if (total.bookNo == trans.bookNo)
total.AddData(trans);
else {
cout << total.bookNo << " " << total.units_sold << " "
<< total.revenue << endl;
total.Print();
total = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.revenue
<< endl;
total.Print();
}
else {
std::cerr << "No data?!" << std::endl;
Expand Down
9 changes: 1 addition & 8 deletions ch07/ex7_02.h → ch07/ex7_02_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
//
// ex7_02.h
// Exercise 7.2
//
// Created by pezy on 14/11/8.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_02_h
#define CP5_ex7_02_h

#include <string>

// Add the combine and isbn members to the Sales_data
struct Sales_data {
std::string isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
Expand Down
11 changes: 2 additions & 9 deletions ch07/ex7_03.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
//
// ex7_03.cpp
// Exercise 7.03
//
// Created by pezy on 14/11/8.
// Copyright (c) 2014 pezy. All rights reserved.
//

#include "ex7_02.h"
#include "ex7_02_sales_data.h"
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
Expand Down
13 changes: 3 additions & 10 deletions ch07/ex7_06.h → ch07/ex7_06_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
//
// ex7_06.h
// Exercise 7.6
//
// Created by pezy on 11/8/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_06_h
#define CP5_ex7_06_h

#include <string>
#include <iostream>
#include <string>

// added add, read, print functions
struct Sales_data {
std::string const& isbn() const { return bookNo; };
Sales_data& combine(const Sales_data&);
Expand Down Expand Up @@ -51,4 +44,4 @@ Sales_data add(const Sales_data& lhs, const Sales_data& rhs)
return sum;
}

#endif
#endif
10 changes: 1 addition & 9 deletions ch07/ex7_07.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
//
// ex7_07.cpp
// Exercise 7.7
//
// Created by pezy on 11/8/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#include "ex7_06.h"
#include "ex7_06_sales_data.h"

int main()
{
Expand Down
12 changes: 1 addition & 11 deletions ch07/ex7_11.cpp → ch07/ex7_11_sales_data.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
//
// ex7_11.cpp
// Exercise 7.11
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#include "ex7_11.h"
#include "ex7_11_sales_data.h"

int main()
{
Expand All @@ -21,6 +13,4 @@ int main()

Sales_data item4(std::cin);
print(std::cout, item4) << std::endl;

return 0;
}
13 changes: 3 additions & 10 deletions ch07/ex7_11.h → ch07/ex7_11_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
//
// ex7_11.h
// Exercise 7.11
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_11_h
#define CP5_ex7_11_h

#include <string>
#include <iostream>
#include <string>

// Add constructors to your Sales_data class
struct Sales_data {
Sales_data() = default;
Sales_data(const std::string& s) : bookNo(s) {}
Expand Down Expand Up @@ -64,4 +57,4 @@ Sales_data& Sales_data::combine(const Sales_data& rhs)
return *this;
}

#endif
#endif
12 changes: 3 additions & 9 deletions ch07/ex7_12.h → ch07/ex7_12_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
//
// ex7_12.h
// Exercise 7.12
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_12_h
#define CP5_ex7_12_h

#include <string>
#include <iostream>
#include <string>

struct Sales_data;
std::istream& read(std::istream&, Sales_data&);

// Move the definition of the Sales_data constructor that takes an istream into
// the body of the Sales_data class.
struct Sales_data {
Sales_data() = default;
Sales_data(const std::string& s) : bookNo(s) {}
Expand Down
10 changes: 1 addition & 9 deletions ch07/ex7_13.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
//
// ex7_13.cpp
// Exercise 7.13
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#include "ex7_12.h"
#include "ex7_12_sales_data.h"

int main()
{
Expand Down
11 changes: 2 additions & 9 deletions ch07/ex7_21.h → ch07/ex7_21_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
//
// ex7_21.h
// Exercise 7.21
//
// Created by pezy on 11/13/14.
// Copyright (c) 2014 pezy. All rights reserved.
//

#ifndef CP5_ex7_21_h
#define CP5_ex7_21_h

#include <string>
#include <iostream>
#include <string>

// Update Sales_data class to hide its implementation
class Sales_data {
friend std::istream& read(std::istream& is, Sales_data& item);
friend std::ostream& print(std::ostream& os, const Sales_data& item);
Expand Down
12 changes: 1 addition & 11 deletions ch07/ex7_26.cpp → ch07/ex7_26_sales_data.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
//
// ex7_26.cpp
// Exercise 7.26
//
// Created by pezy on 11/9/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief implementation of class Sales_data
// @See ex7_26.h

#include "ex7_26.h"
#include "ex7_26_sales_data.h"

// member functions.
Sales_data& Sales_data::combine(const Sales_data& rhs)
Expand Down
13 changes: 2 additions & 11 deletions ch07/ex7_26.h → ch07/ex7_26_sales_data.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
//
// ex7_26.h
// Exercise 7.26
//
// Created by pezy on 11/14/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @See ex7_21.h
// @Add inline member function "Sales_data::avg_pric"

#ifndef CP5_ex7_26_h
#define CP5_ex7_26_h

#include <string>
#include <iostream>
#include <string>

// define Sales_data::avg_price as an inline function.
class Sales_data {
friend std::istream& read(std::istream& is, Sales_data& item);
friend std::ostream& print(std::ostream& os, const Sales_data& item);
Expand Down
12 changes: 1 addition & 11 deletions ch07/ex7_41.cpp → ch07/ex7_41_sales_data.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
//
// ex7_41.cpp
// Exercise 7.41
//
// Created by pezy on 11/20/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// @Brief implementation of class Sales_data
// @See ex7_41.h

#include "ex7_41.h"
#include "ex7_41_sales_data.h"

// constructor
Sales_data::Sales_data(std::istream& is) : Sales_data()
Expand Down
Loading

0 comments on commit e33e2d8

Please sign in to comment.