本篇是《C++ Primer》(第5版)第1章的习题练习17~25题的答案。
练习17:如果输入的所有值都是相等的,本节的程序会输出什么?如果没有重复值,输出又会是怎样的?
解答:如果输入的所有值都相等,则while循环中的else分支永远不会执行,直到输入结束,while循环退出,循环后的输出语句打印这唯一的一个值和它出现的次数。若没有重复值,则while循环中的if语句的真值分支永远不会执行,每读入一个值,都会进入else分支,打印它的值和出现次数1。输入结束后,while循环退出,循环后的输出语句打印最后一个值和出现次数1
练习18:编译并运行本节的程序,给它输入全都相等的值。再次运行程序,输入没有重复的值。
#include <iostream>
int main()
{
//currVal 是我们正在统计的数;我们将读入的新值存入val
int currVal = 0;
int val = 0;
//读取第一个数,并确保确实有数据可以处理
if(std::cin >> currVal)
{
int cnt = 1; //保存我们正在处理的当前值的个数
while(std::cin >> val) //读取剩余的数
{
if(val == currVal) //如果值相同
{
++cnt; //将cnt加1
}
else //否则,打印前一个值的个数
{
std::cout << currVal << " occurs " << cnt << " times " << std::endl;
currVal = val; //记住新值
cnt = 1; //重置计数器
}
} //while循环在这里结束
//记住打印文件中最后一个值的个数
std::cout << currVal << " occurs " << cnt << " times " << std::endl;
} //最外层的if语句在这里结束
return 0;
}
练习19:修改你为1.4.1节练习1.10(第11页)所编写的程序(打印一个范围内的数),使其能处理用户输入的第一个数比第二个数小的情况。
#include <iostream>
int main()
{
std::cout << "请输入两个数:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
if(v1 > v2)
{
while(v1 >= v2)
{
std::cout << v1 << " ";
--v1;
}
}
else
{
while(v2 >=v1)
{
std::cout << v2 << " ";
--v2;
}
}
std::cout << std::endl;
return 0;
}
![](https://img-blog.csdnimg.cn/20201025091505129.png)
练习20:在网站http://www.informit.com/title/0321714113上,第1章的代码目录包含了头文件Sales_item.h。将它拷贝到你自己的工作目录中。用它编写一个程序,读取一组书籍销售记录,将每条记录打印到标准输出上。
#ifndef SALESITEM_H
// we're here only if SALESITEM_H has not yet been defined
#define SALESITEM_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
Sales_item() = default;
Sales_item(const std::string &book): bookNo(book) { }
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
unsigned units_sold = 0; // explicitly initialized
double revenue = 0.0;
};
// 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() << " " << 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
#include "Sales_item.h"
#include <iostream>
int main()
{
Sales_item book;
std::cout << "请输入销售记录:" << std::endl;
//读入ISBN号、售出的册数以及销售价格
while(std::cin >> book)
{
std::cout << "ISBN、售出本数、销售额和平均售价为 " << book << std::endl;
}
return 0;
}
练习21:编写程序,读取两个ISBN相同的Sales_item对象,输出它们的和。
#include "Sales_item.h"
#include <iostream>
int main()
{
Sales_item trans1, trans2;
std::cout << "请输入两条ISBN相同的销售记录:" << std::endl;
std::cin >> trans1 >> trans2;
if(compareIsbn(trans1, trans2))
{
std::cout << "汇总信息:ISBN、售出本数、销售额和平均售价为 " << trans1 + trans2 << std::endl;
}
else
{
std::cout << "两条销售记录的ISBN不同" << std::endl;
}
return 0;
}
练习22:编写程序,读取多个具有相同ISBN的销售记录,输出所有记录的和。
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total, trans;
std::cout << "请输入几条ISBN 相同的销售记录:" << std::endl;
if(std::cin >> total)
{
while(std::cin >> trans)
{
if(compareIsbn(total, trans)) //ISBN相同
{
total = total + trans;
}
else //ISBN 不同
{
std::cout << "ISBN不同" << std::endl;
return -1;
}
std::cout << "汇总信息:ISBN、售出本数、销售额和平均售价为" << total << std::endl;
}
}
else
{
std::cout << "没有数据" << std::endl;
return -1;
}
return 0;
}
练习23:编写程序,读取多条销售记录,并统计每个ISBN(每本书)有几条销售记录。
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item trans1, trans2;
int num = 1;
std::cout << "请输入若干销售记录:" << std::endl;
if(std::cin >> trans1)
{
while(std::cin >> trans2)
{
if(compareIsbn(trans1, trans2))
{
num++;
}
else //ISBN不同
{
std::cout << trans1.isbn() << "共有" << num << "条销售记录" << std::endl;
trans1 = trans2;
num = 1;
}
std::cout << trans1.isbn() << "共有" << num << "条销售记录" << std::endl;
}
}
else
{
std::cout << "没有数据" << std::endl;
return -1;
}
}
练习24:编写程序,读取多条销售记录,并统计每个ISBN(每本书)有几条销售记录。
练习25:输入表示多个ISBN的多条销售记录来测试上一个程序,每个ISBN的记录应该聚在一起。
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total; //保存下一条交易记录的变量
//读入第一条交易记录,并确保有数据可以处理
if(std::cin >> total)
{
Sales_item trans; //保存和的变量
//读入并处理剩余交易记录
while(std::cin >> trans)
{
//如果我们仍在处理相同的书
if(total.isbn() == trans.isbn())
{
total += trans; //更新总销售额
}
else
{
//打印前一本书的结果
std::cout << total << std::endl;
total = trans; //total现在表示下一本书的销售额
}
}
std::cout << total << std::endl; //打印最后一本书的结果
}
else
{
//没有输入!警告读者
std::cerr << "No data?!" << std::endl;
return -1; //表示失败
}
return 0;
}