Exercise 7.6
1#include <iostream>2#include <string>3#include"Factmain.h"4 using namespacestd;5 6 structSales_data {7 stringISBN ()Const{returnBookno;}8Sales_data &combine (Constsales_data&);9 stringBookno;Ten unsigned units_sold; One DoublePrice =0.0; A DoubleRevenue =0.0; - }; - theSales_data Add (ConstSales_data &LHS,ConstSales_data &RHS); -Ostream &print (Ostream &os,ConstSales_data &item); -IStream &read (IStream & is, Sales_data &item); - + - intMain () + { A Sales_data item1, item2; at DoubleTotalrevenue =0; - DoubleTotalsold =0; - intCounter =1; - if(Read (CIN, item1)) - { -Item1.revenue = Item1.price *Item1.units_sold; in while(Read (cin,item2)) { -Item2.revenue = Item2.price *Item2.units_sold; to if(Item1.bookno = =item2.bookno) { + Item1.combine (item2); -++counter; the } * Else { $Print (cout, item1) <<Endl;Panax NotoginsengItem1.bookno =Item2.bookno; -Item1.units_sold =Item2.units_sold; theItem1.revenue =item2.revenue; +Counter =1; A } the } +Print (cout, item2) <<Endl; - } $ return 0; $ } - -Sales_data Add (ConstSales_data & LHS,ConstSales_data & RHS)//non-member function add the { -Sales_data sum =LHS;Wuyi Sum.combine (RHS); the returnsum; - //TODO: Insert a return statement here Wu } - AboutIStream & Read (IStream & is, Sales_data &Item) $ { - is>> item.bookno >> item.units_sold >>Item.price; -Item.revenue = Item.units_sold *Item.price; - return is; A //TODO: Insert a return statement here + } the -Ostream & Print (Ostream & OS,ConstSales_data &Item) $ { theOS << item.bookno <<" "<< Item.units_sold <<" "<< Item.price <<" "<<item.revenue; the returnos; the //TODO: Insert a return statement here the } - inSales_data & Sales_data::combine (ConstSales_data &RHS) the { theUnits_sold + =Rhs.units_sold; AboutRevenue + =rhs.revenue; the return* This; the}
Exercise 7.7
See above
Exercise 7.8
Read involves the calculation of revenue, Item.revenue is updated, so it is defined as a normal reference, and print is the value of the output object, so it can be declared as a constant reference;
Exercise 7.9
See the procedure at the end of this chapter.
Exercise 7.10
Determine whether the input of data1 and data2 is correct (non-null or input error), first input data1, in the input data2;
C++primer 7.1.3 sessions