From 7e56b4208961b1f7d4f91bc2253d8a93dc487fce Mon Sep 17 00:00:00 2001 From: Shine wOng <1551885@tongji.edu.cn> Date: Tue, 21 May 2019 18:44:09 +0800 Subject: [PATCH] update exercises 7.1--7.10 --- c++ note/chp7/chp7answers.md | 79 ++++++++++++++++++++++++++++++++++- c++ note/chp7/exercise7_1.cpp | 10 +++-- c++ note/chp7/exercise7_2.h | 44 +++++++++++++++++++ c++ note/chp7/exercise7_3.cpp | 23 ++++++++++ c++ note/chp7/exercise7_4.h | 27 ++++++++++++ 5 files changed, 179 insertions(+), 4 deletions(-) create mode 100644 c++ note/chp7/exercise7_2.h create mode 100644 c++ note/chp7/exercise7_3.cpp create mode 100644 c++ note/chp7/exercise7_4.h diff --git a/c++ note/chp7/chp7answers.md b/c++ note/chp7/chp7answers.md index 5a2b753..35b747f 100644 --- a/c++ note/chp7/chp7answers.md +++ b/c++ note/chp7/chp7answers.md @@ -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) \ No newline at end of file +[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 +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 +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. diff --git a/c++ note/chp7/exercise7_1.cpp b/c++ note/chp7/exercise7_1.cpp index d1ac557..e3d3012 100644 --- a/c++ note/chp7/exercise7_1.cpp +++ b/c++ note/chp7/exercise7_1.cpp @@ -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; diff --git a/c++ note/chp7/exercise7_2.h b/c++ note/chp7/exercise7_2.h new file mode 100644 index 0000000..147e50d --- /dev/null +++ b/c++ note/chp7/exercise7_2.h @@ -0,0 +1,44 @@ +#ifndef EXERCISE7_2_H_ +#define EXERCISE7_2_H_ + +#include +#include +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 diff --git a/c++ note/chp7/exercise7_3.cpp b/c++ note/chp7/exercise7_3.cpp new file mode 100644 index 0000000..4d6333a --- /dev/null +++ b/c++ note/chp7/exercise7_3.cpp @@ -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; +} diff --git a/c++ note/chp7/exercise7_4.h b/c++ note/chp7/exercise7_4.h new file mode 100644 index 0000000..9a5fb6f --- /dev/null +++ b/c++ note/chp7/exercise7_4.h @@ -0,0 +1,27 @@ +#ifndef EXERCISE7_4_H_ +#define EXERCISE7_4_H_ + +#include +#include +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