C + + Primer version Fifth exercise 7.7

Source: Internet
Author: User

Exercise 7.7: Use these new functions to rewrite the transaction handlers in the 7.1.2 practice session

Sales.data.h#ifndef sales_data_h#define sales_data_h#include <iostream> #include   &LT;STRING&GT;STRUCT&NBSP;SALES_DATA{&NBSP;&NBSP;&NBSP;&NBSP;STD::STRING&NBSP;ISBN ()  const{return  Bookno;}     double avg_price ()  const;    Sales_data&  Combine (const sales_data&);    unsigned units_sold=0;     std::string bookno;    double revenue=0.0;    double  price=0.0;}; Double sales_data::avg_price ()  const{    if (units_sold)          return revenue/units_sold;    else         return 0;} Sales_data& sales_data::combine (CONST&NBSP;SALES_DATA&NBSP;&AMP;RHS) {    units_ sold+=rhs.units_sold;    revenue+=rhs.revenue;    return *this;} Std::istream& read (std::istream &is,sales_data &item) {    is >>item.bookno>>item.units_sold>>item.price;    item.revenue=item.units_ Sold*item.price;    return is;} Std::ostream& print (Std::ostream &os,const sales_data &item) {   &NBSP;&NBSP;OS&LT;&LT;ITEM.ISBN () << " " <<item.units_sold<< " " <<item.revenue << " " <<item.avg_price ();     return os;} Sales_data add (CONST&NBSP;SALES_DATA&NBSP;&AMP;LHS,CONST&NBSP;SALES_DATA&NBSP;&AMP;RHS) {     sales_data sum=lhs;    sum.combine (RHS);     return  sum;} #endif  // sales_data_h
 
main.cpp#include  "Sales_data.h" Int main (INT&NBSP;ARGC,CONST&NBSP;CHAR&NBSP;**ARGV) {     sales_data total;    if (Read (std::cin,total))     {         Sales_data trans;         while (Read (Std::cin,trans))         {             if (TOTAL.ISBN () ==TRANS.ISBN ())              {                 total=add (Total,trans);             }            else             {             &nbsP;   print (std::cout,total) <<std::endl;                 total=trans;             }        }         print (std::cout,total);    }    else     {        std::cerr<< "No data?" <<std::endl;    }}

This article is from the "Running Donkey" blog, please make sure to keep this source http://amgodchan.blog.51cto.com/9521586/1568244

C + + Primer version Fifth exercise 7.7

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.