在C++primer第五版的1.6节中,示例了一个书店程序。要求是从一个文件中读取销售记录,生成每本书的销售报告,显示售出册数、总销售额和平均售价,假定每个ISBN书号的所有销售记录在文件中是聚在一起保存的。也就是说,输入销售记录时,要求同一个书号的销售记录要连续一次性全部输入。
编程思路和前一篇“统计在输入中每个值连续出现的次数”的思路差不多。这里就不再累述,直接附上书上的代码
//假定每个ISBN书号的所有销售记录在文件中是聚在一起保存的
//在main函数中,我们定义了一个名为total的变量,用来保存一个给定的ISBN数据之和
#include "stdafx.h"
#include "Sales_item.h"
using namespace std;
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; //total表示下一本书的销售额
}
}
cout << total << endl; //打印最后一本书的结果
}
else
{
cerr << "No data?" << endl; //警告读者,没有输入
return -1; //表示失败
}
system("pause");
return 0;
}
需要说明的是,该代码包含了头文件Sales_item.h,该头文件在http://www.informit.com/title/0321714113第一章中,可以下载,需要将它拷贝到你自己的工作目录中,程序才能正常运行。里面对 >>,<<,>,==,+等符号进行了重载,重载这个知识点在后续会专门讲,此处不懂没关系,仿照着.cpp文件中的使用方式使用就OK了。
效果如下:
类似于 ‘001 3 34’ 的数据为输入数据,分别表示ISBN号、销售册数和售价。
类似于 ‘001 7 278 39.7143’ 的数据是处理后的销售报告打印结果,分别表示ISBN号、销售总册数、总销售额和平均售价。当输入的ISBN号不同时,打印前一ISBN号的销售报告。
细心思考一下会发现,这个编程思路并不适合实际情况,如果没有提前将同一ISBN号的书的销售记录放在一起或者输入时漏掉了某几个销售记录,之后想输入补上,那这个程序就满足不了要求了。而且,该程序是及时打印每一ISBN号的书的销售报告,如果我们想将所有销售记录输入完成后再打印所有的销售报告,这也是无法实现的。
因此,我适当修改了一下程序,希望能将所有销售记录输入完成,以‘q q q’作为结束符,再统一打印所有的销售记录。代码如下:
// primer_1_5_2.cpp : Defines the entry point for the application.
// 读取多条销售记录并生成每本书的销售报告,显示售出册数,总销售额和平均售价。
// Sales_item类的输入为(3个参数):ISBN号,销售册数,售价;输出为(4个参数):ISBN号,销售册数,总销售额和平均售价。
#include "stdafx.h"
#include "Sales_item.h"
using namespace std;
const int Max=50; //设置销售记录条数最大值
int main()
{
Sales_item item[Max]; //创建一个数组存放不同书的销售报告
Sales_item temp; //用来保存销售记录的变量
cout << "enter the sale record: ('q q q' to quit)" << endl;
cout << "#1 ";
int j=1; //计数变量,每增加一种书,j加1,相同书只用一个j
if(cin >> temp) //读入第一本书的销售记录
{
item[0]=temp; //放入数组的第一个元素中
cout << "#2 "; //提示输入语句
int m=3; //提示输入的变量
while(cin >> temp) //依次读入剩下的书的销售记录
{
if(temp.isbn()=="q q q") //如果输入‘q q q’
break; //则退出while循环,转到后面的打印销售报告语句
for(int k=0;k<j;k++) //遍历数组里的已有元素
{
if(temp.isbn()==item[k].isbn()) //判断读入的新书是否是已有记录的书,如果是
{
item[k] += temp; //则将这两本书和在一起
break;
}
else //如果不是
{
if(k==j-1) //则在遍历完所有的已有书后
{
item[j]=temp; //新添一本
j++; //数组中所含元素加1
break;
}
}
}
cout << "#" << m << " "; //提示输入语句
m++;
}
}
cout << "j=" << j << endl;
cout << "the sale record: " << endl; //打印统计结果
cout << "ISBN\t" << "units\t" << "revenue\t" << "average price\t" << endl;
for(int i=0;i<j;i++) //遍历数组中所有元素
cout << item[i] << endl;
system("pause");
return 0;
}
效果如下:
可以看到,不需要将同一ISBN号的书的销售记录放在一起输入,不在输入过程中进行打印,而是最后打印全部内容。
特别提示,这里设置的要输入3个q来作为结束符是因为Sales_item类需要输入三个参数,只输入一个非法字符是不会导致输入结束的,因为它还会等待你输入后面两个参数。
另外,为了打印时对齐美观,我不仅在.cpp文件的打印语句中加了\t(一个制表符),还修改了Sales_item.h中的打印语句,见下面几行代码。
operator<<(std::ostream& out, const Sales_item& s)
{
out << s.isbn() << "\t" << s.units_sold << "\t"
<< s.revenue << "\t" << s.avg_price();
return out;
}
最后,为了减少麻烦,我把Sales_item.h文件附上(去掉了一行要报错的代码以及在打印语句处添加了\t后的文件)
/*
* This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
* Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:
*
* "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
*
*
* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."
*
* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address:
*
* Pearson Education, Inc.
* Rights and Permissions Department
* One Lake Street
* Upper Saddle River, NJ 07458
* Fax: (201) 236-3290
*/
/* This file defines the Sales_item class used in chapter 1.
* The code used in this file will be explained in
* Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
* Readers shouldn't try to understand the code in this file
* until they have read those chapters.
*/
#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_H
//#include "Version_test.h"
// Definition of Sales_item class and related functions goes here
#include <iostream>
#include <string>
class Sales_item {
// these declarations are explained section 7.2.1, p. 270
// and in chapter 14, pages 557, 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:
// constructors are explained in section 7.1.4, pages 262 - 265
// default constructor needed to initialize members of built-in type
#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:
// operations on Sales_item objects
// member binary operator: left-hand operand bound to implicit this pointer
Sales_item& operator+=(const Sales_item&);
// operations on Sales_item objects
std::string isbn() const { return bookNo; }
double avg_price() const;
// private members as before
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
};
// used in chapter 10
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() << "\t" << s.units_sold << "\t"
<< s.revenue << "\t" << s.avg_price();
return out;
}
double Sales_item::avg_price() const
{
if (units_sold)
return revenue/units_sold;
else
return 0;
}
#endif