C++Primer第五版 练习8.6答案详解

重写7.1.1节书店程序(第299页),从文件中读取一个交易记录。将文件名作为一个参数传递给main

  • 本题所需源文件如下:

  • Sales_data.h文件:

#pragma once

#include<iostream>
#include<string>
using namespace std;

class Sales_data
{
    friend std::istream & operator >>(std::istream&, Sales_data&);
    friend std::ostream & operator <<(std::ostream&, const Sales_data&);
    friend bool operator <(const Sales_data&, const Sales_data&);
    friend bool operator ==(const Sales_data&, const Sales_data&);

public:
    Sales_data() = default;
    Sales_data(const std::string &book) : bookNo(book) {};
    Sales_data(std::istream &is) { is >> *this; }
    //    Sales_data();
    //    ~Sales_data();
public:
    Sales_data&operator +=(const Sales_data&);
    string isbn()const { return bookNo; }
private:
    std::string bookNo;
    unsigned units_sold = 0;
    double sellingprice = 0.0;
    double saleprice = 0.0;
    double discount = 0.0;
};

inline bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
{
    return lhs.isbn() == rhs.isbn();
}
inline bool operator ==(const Sales_data &lhs, const Sales_data &rhs)
{
    return lhs.units_sold == rhs.units_sold && lhs.sellingprice == rhs.sellingprice && lhs.saleprice == rhs.sellingprice && lhs.isbn() == rhs.isbn();

}
inline bool operator !=(const Sales_data &lhs, const Sales_data &rhs)
{
    return !(lhs == rhs);
}

 

  • Sales_data.cpp文件:

#include "Sales_data.h"


Sales_data operator+(const Sales_data&, const Sales_data&);


Sales_data& Sales_data::operator+=(const Sales_data&rhs)
{
    units_sold += rhs.units_sold;
    saleprice = (rhs.saleprice*rhs.units_sold + saleprice*units_sold) / (rhs.units_sold + units_sold);
    if (sellingprice != 0)
        discount = saleprice / sellingprice;
    return *this;
}

Sales_data operator + (const Sales_data& lhs, const Sales_data& rhs)
{
    Sales_data ret(lhs);
    ret += rhs;
    return ret;
}

std::istream& operator >>(std::istream& in, Sales_data& s)
{
    in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;
    if (in&&s.saleprice != 0)
        s.discount = s.saleprice / s.sellingprice;
    else
        s = Sales_data();
    return in;
}

std::ostream& operator << (std::ostream& out, const Sales_data& s)
{
    out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " << s.discount;
    return out;
}

 

  • 主函数文件:

#include<iostream>
#include<fstream>
#include"Sales_data.h"

using namespace std;

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        cerr << "请给出文件名" << endl;
        return -1;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        cerr << "无法打开输入文件" << endl;
        return -1;
    }


    cout << "请输入交易记录(ISBN、销售量、原价、实际售价):" << endl;
    Sales_data total;
    if (in>>total)
    {
        Sales_data trans;
        while (in >> trans)
        {
            if (total.isbn() == trans.isbn())
                total += trans;
            else
            {
                cout << total << endl;
                total = trans;
            }
        }
        cout << total << endl;
    }
    else
    {
        cerr << "No data?!" << endl;
        return -1;
    }
    return 0;
}

 

  • 存放交易记录的文件设置:

  将存放交易记录的文件放置于 .cpp同级目录下,如下图:

  交易记录格式如下图所示:

 

  • 基于VS从命令行获取参数的设置方法:

  命令行参数的设置方式按照下图所示步骤进行:

  (1)鼠标右键->属性

  (2)参照下图设置命令行

  • 程序运行结果如下:

 

posted @ 2019-05-06 22:55  鲸小鱼-  阅读(373)  评论(0编辑  收藏  举报