update exercises 7.1--7.10

This commit is contained in:
Shine wOng
2019-05-21 18:44:09 +08:00
parent 2b2e746598
commit 7e56b42089
5 changed files with 179 additions and 4 deletions

View File

@@ -3,4 +3,81 @@ Solutions to Exercises in Chapter Seven
> Exercise 7.1: Write a version of the transaction-processing program from §1.6 (p. 24) using the Sales_data class you defined for the exercises in §2.6.1 (p. 72).
[exercise7_1.cpp](exercise7_1.cpp)
[exercise7_1.cpp](exercise7_1.cpp)
> Exercise 7.2: Add the combine and isbn members to the Sales_data class you wrote for the exercises in § 2.6.2 (p. 76).
[exercise7_2.h](exercise7_2.h)
> Exercise 7.3: Revise your transaction-processing program from § 7.1.1 (p.256) to use these members.
[exercsie7_3.cpp](exercise7_3.cpp)
> Exercise 7.4: Write a class named Person that represents the name and address of a person. Use a string to hold each of these elements. Subsequent exercises will incrementally add features to this class.
```cpp
#ifndef EXERCISE7_4_H_
#define EXERCISE7_4_H_
#include <string>
using std::string;
struct Person{
string name;
string address;
};
#endif
```
> Exercise 7.5: Provide operations in your Person class to return the name and address. Should these functions be const? Explain your choice.
```cpp
#ifndef EXERCISE7_4_H_
#define EXERCISE7_4_H_
#include <string>
using std::string;
struct Person{
string name;
string address;
string getName() const { return name; }
string getAddr() const { return address; }
};
#endif
```
These two functions should be `const`. Because these two functions do not modify the object's members, and `const` objects of this class should be able to call these two functions.
> Exercise 7.6: Define your own versions of the add, read, and print functions.
It seems I have done this back in Exercise 7.1.
> Exercise 7.7: Rewrite the transaction-processing program you wrote for the exercises in § 7.1.2 (p. 260) to use these new functions.
It seems I have done this back in Exercise 7.1.
> Exercise 7.8: Why does `read` define its Sales_data parameter as a plain reference and `print` define its parameter as a reference to const?
Because in `read`, the `Sales_data` object will be modified. While in `print`, this function do not modify the object.
> Exercise 7.9: Add operations to `read` and `print` Person objects to the code you wrote for the exercises in § 7.1.2 (p. 260).
```
istream& read(istream &is, Person &person){
is >> person.name >> person.address;
return is;
}
ostream& print(ostream &os, Person const &person){
os << person.name << ' ' << person.address;
return os;
}
```
> Exercise 7.10: What does the condition in the following if statement do?
`if (read(read(cin, data1), data2))`
Take in two objects(data1 and data2) at the same time, and step into the `if` block only when both objects are valid.

View File

@@ -14,8 +14,12 @@ struct Sales_data{
}
};
void add(Sales_data one, Sales_data two){
one.combine(two);
Sales_data add(Sales_data const &one, Sales_data const &two){
Sales_data res;
res.bookNo = one.bookNo;
res.units_sold = one.units_sold + two.units_sold;
res.revenue = one.revenue + two.revenue;
return res;
}
istream& read(istream &is, Sales_data &data){
@@ -25,7 +29,7 @@ istream& read(istream &is, Sales_data &data){
return is;
}
ostream& print(ostream &os, Sales_data const data){
ostream& print(ostream &os, Sales_data const &data){
os << "book number: " << data.bookNo << endl;
os << "units sold: " << data.units_sold << endl;
os << "total revenue: " << data.revenue << endl;

View File

@@ -0,0 +1,44 @@
#ifndef EXERCISE7_2_H_
#define EXERCISE7_2_H_
#include <iostream>
#include <string>
using namespace std;
struct Sales_data {
string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
string isbn() const { return bookNo; }
double avg_price() const { return revenue / units_sold; }
Sales_data& combine(Sales_data const &data); //behave same as the `+=` operator, thus return a left-value
};
Sales_data& Sales_data::combine(Sales_data const &data){
units_sold += data.units_sold;
revenue += data.revenue;
return *this;
}
Sales_data add(Sales_data const &one, Sales_data const &two) {
Sales_data res = one;
return res.combine(two);
}
istream& read(istream &is, Sales_data &data) {
is >> data.bookNo;
is >> data.units_sold;
is >> data.revenue;
return is;
}
ostream& print(ostream &os, Sales_data const data) {
os << "book number: " << data.bookNo << endl;
os << "units sold: " << data.units_sold << endl;
os << "total revenue: " << data.revenue << endl;
os << "averge price: " << data.avg_price(); // no endl at the end
return os;
}
#endif

View File

@@ -0,0 +1,23 @@
#include "exercise7_2.h"
int main() {
Sales_data total; // variable to hold the running sum
if (read(cin, total)) { // read the first transaction
Sales_data trans; // variable to hold data for the next transaction
while (read(cin, trans)) { // read the remaining transactions
if (total.isbn() == trans.isbn()) // check the isbns
total = total.combine(trans); // update the running total
else {
print(cout, total) << endl; // print the results
total = trans; // process the next book
}
}
print(cout, total) << endl; // print the last transaction
}
else { // there was no input
cerr << "No data?!" << endl; // notify the user
}
system("pause");
return 0;
}

View File

@@ -0,0 +1,27 @@
#ifndef EXERCISE7_4_H_
#define EXERCISE7_4_H_
#include <string>
#include <iostream>
using std::string;
using std::istream;
using std::ostream;
struct Person{
string name;
string address;
string getName() const { return name; }
string getAddr() const { return address; }
};
istream& read(istream &is, Person &person){
is >> person.name >> person.address;
return is;
}
ostream& print(ostream &os, Person const &person){
os << person.name << ' ' << person.address;
return os;
}
#endif