C++ Primer_1 书店程序
一个简单的书单程序,能够实现书本的编码isbn、销售册数、单价的输入;
输出:如果两次输入的isbn相同,则输出isbn、累加销售册数、销售总额和平均单价;
不同则输出上一次输入的书本销售相关信息。
Sales_item.h (新手可跳过这一段代码)
//条件编译,防止重命名
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>
//using namespace std;
class Sales_item {
// 7.2.1, p270
// 第14章, p557, 558, 561
//重载运算符>> / << / == / <
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
friend bool operator<(const Sales_item&, const Sales_item&);
friend bool
operator==(const Sales_item&, const Sales_item&);
public:
// 7.1.4, p262 - 265
#if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
Sales_item() = default;
#else
Sales_item(): units_sold(0), revenue(0.0) { }
#endif
Sales_item(const std::string &book):
bookNo(book), units_sold(0), revenue(0.0) { }
Sales_item(std::istream &is) { is >> *this; }
public:
Sales_item& operator+=(const Sales_item&);
std::string isbn() const { return bookNo; }
double avg_price() const;
private:
std::string bookNo; // implicitly initialized to the empty string
#ifdef IN_CLASS_INITS
unsigned units_sold = 0; // explicitly initialized
double revenue = 0.0;
#else
unsigned units_sold;
double revenue;
#endif
};
inline long lround(double d)
{
//实现小数的四舍五入
return (d >= 0) ? long(d + 0.5) : long(d - 0.5);
}
inline
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }
// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);
inline bool
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
// must be made a friend of Sales_item
return lhs.units_sold == rhs.units_sold &&
lhs.revenue == rhs.revenue &&
lhs.isbn() == rhs.isbn();
}
inline bool
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
return !(lhs == rhs); // != defined in terms of operator==
}
// assumes that both objects refer to the same ISBN
Sales_item& Sales_item::operator+=(const Sales_item& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
// assumes that both objects refer to the same ISBN
Sales_item
operator+(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs); // copy (|lhs|) into a local object that we'll return
ret += rhs; // add in the contents of (|rhs|)
return ret; // return (|ret|) by value
}
std::istream&
operator>>(std::istream& in, Sales_item& s)
{
double price;
in >> s.bookNo >> s.units_sold >> price;
// check that the inputs succeeded
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_item(); // input failed: reset object to default state
return in;
}
std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
out << s.isbn() << " " << s.units_sold << " "
<< s.revenue << " " << s.avg_price();
return out;
}
double Sales_item::avg_price() const
{
if (units_sold)
return revenue/units_sold;
else
return 0;
}
#endif
Book_sales.cpp(这一段是本章重点)
#include <iostream>
#include "Sales_item.h"
//using namespace std;
//防止命名空间名称·冲突
using std::cin;
using std::cout;
using std::endl;
int main()
{
Sales_item total;
if (cin >> total)
{
Sales_item trans;
while (cin >> trans)
{
if (total.isbn() == trans.isbn())
{
total += trans;
}
else
{
cout << total << endl;
total = trans;
}
}
cout << total << endl;
}
else {
//发出警告
std::cerr << "No data!" << endl;
return -1;
}
return 0;
}
主要是关于std命名空间的使用细节,为防止变量名称冲突,尽量少用using namespace std;