7.1.4

原创 2017年01月03日 22:46:46

7.11

#include <string>
#include <iostream>

class Sales_data {
public:
    Sales_data() = default;
    Sales_data(const std::string &s):bookNo(s) { }
    Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(n*p){ }
    Sales_data(std::istream &is);

    std::string isbn() const { return bookNo; };
    Sales_data& combine(const Sales_data&);
private:
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

// nonmember functions
std::istream &read(std::istream &is, Sales_data &item)
{
    double price = 0;
    is >> item.bookNo >> item.units_sold >> price;
    item.revenue = price * item.units_sold;
    return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item)
{
    os << item.isbn() << " " << item.units_sold << " " << item.revenue;
    return os;
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
    Sales_data sum = lhs;
    sum.combine(rhs);
    return sum;
}   

// member functions.
Sales_data::Sales_data(std::istream &is)
{
    read(is, *this);
}

Sales_data& Sales_data::combine(const Sales_data& rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

7.12

#include <string>
#include <iostream>

struct Sales_data;
std::istream &read(std::istream&, Sales_data&);

class Sales_data {
public:
    Sales_data() = default;
    Sales_data(const std::string &s):bookNo(s) { }
    Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(n*p){ }
    Sales_data(std::istream &is) { read(is, *this); }

    std::string isbn() const { return bookNo; };
    Sales_data& combine(const Sales_data&);
private:
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};

// member functions.
Sales_data& Sales_data::combine(const Sales_data& rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

// nonmember functions
std::istream &read(std::istream &is, Sales_data &item)
{
    double price = 0;
    is >> item.bookNo >> item.units_sold >> price;
    item.revenue = price * item.units_sold;
    return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item)
{
    os << item.isbn() << " " << item.units_sold << " " << item.revenue;
    return os;
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
    Sales_data sum = lhs;
    sum.combine(rhs);
    return sum;
}

7.13

int main()
{
    Sales_data total(std::cin);
    if (!total.isbn().empty())
    {
        std::istream &is = std::cin;
        while (is) {
            Sales_data trans(is);
            if (total.isbn() == trans.isbn())
                total.combine(trans);
            else {
                print(std::cout, total) << std::endl;
                total = trans;
            }
        }
        print(std::cout, total) << std::endl;
    }
    else
    {
        std::cerr << "No data?!" << std::endl;
        return -1;
    }

    return 0;
}

7.14

Sales_data() : units_sold(0) , revenue(0){ }

7.15

#include <string>
#include <iostream>



class Person {
public:
    Person() = default;
    Person(const std::string sname, const std::string saddr):name(sname), address(saddr){ }
    Person(std::istream &is){ read(is, *this); }

    std::string getName() const { return name; }
    std::string getAddress() const { return address; }
 private:
    std::string name;
    std::string address;
};

std::istream &read(std::istream &is, Person &person)
{
    is >> person.name >> person.address;
    return is;
}

std::ostream &print(std::ostream &os, const Person &person)
{
    os << person.name << " " << person.address;
    return os;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

nginx1.12.0+php-fpm+php7.1.4+jemalloc4.5.0+redis3.2.8+rsyslog

主服务器IP:192.168.1.104 先配置主服务器的rsyslog: # vim /etc/rsyslog.conf $ModLoad imuxsock $SystemLogRateLimit...

(hdu step 7.1.4)Surround the Trees(求凸包的周长——计算将所有点都包围起来的最小凸多边形的周长)

题意:Surround the TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others...

VMware7.1.4 + Centos6.5 + QT5.2.1 for android 开发环境配置

VMware7.1.4 + Centos6.5 + QT5.2.1 for android 开发环境配置 经过了5天的摸趴滚打,总算是把环境搭建好了,并在手机上面正常跑起来。 (下一步,安...

CentOS6.8服务器LAMP环境搭建(三) 源码安装php7.1.4

第一篇写了CentOS6.8服务器LAMP环境搭建(一) 安装Apache2.4.25,接下来接着CentOS6.8服务器LAMP环境搭建(二) 使用cmake安装MySQL5.7.18,续写Cent...

Surround the Trees 7.1.4

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J...

Android基础入门教程——7.1.4 Android HTTP请求方式-HttpClient

在上一节中我们对HttpURLConnection进行了学习,本节到第二种方式:HttpClient,尽管被Google 弃用了,但是我们我们平时也可以拿HttpClient来抓下包,配合Jsoup...

编译php7.1.4出现 undefined reference to isfinite

我在 CentOS5.5 上编译 php7.1.4 出现错误: ext/standard/.libs/var.o: In function `php_var_export_ex': /PHP/32/s...
  • letshi
  • letshi
  • 2017年05月04日 11:32
  • 881

VMware-workstation-full-7.1.4 + Fedora-11-i386-DVD.iso ---LINUX虚拟机安装配置详解

一、首先百度谷歌一下下载VMware-workstation和linux系统的镜像.iso文件 1、这里我使用的是VMware-workstation-full-7.1.4,而不是网上常见比较经典的...

[Fedora--VMware]虚拟机VMware7.1.4中安装Fedora15-FriendlyARM

问题描述:打算使用Fedora对一个开发板源码进行修改和编译,电脑已经装过虚拟机了,于是试着装个Fedora15,现将尝试过程做下记录。     文件下载:在百度云上下载了个Fedora15的压缩包,...

Installing VMware 7.1.4 and Debian 6.0

Installing VMware After unzip it,you will see a name for the “VMware-workstation-7.1.3.exe"file. Ru...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:7.1.4
举报原因:
原因补充:

(最多只允许输入30个字)