C++primer 7.1.3 sessions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.