C++Primer第五版 ——— (ch1)课后习题参考答案
练习 1.3
#include <iostream>
using namespace std;
int main()
{
char arr[10] = { 0 };
char arr0[10] = { 0 };
char arr1[] = "hello, world";
cin >> arr;
cin >> arr0;
cout << arr << " " << arr0 << endl;
cout << arr1 << endl;
return 0;
}
练习 1.4
#include <iostream>
using namespace std;
int main()
{
int a = 0, b = 0;
cout << "please enter two number:";
cin >> a >> b;
cout << "the sum of " << a << " and " << b << " is " << a + b << endl;
cout << "the multiply of " << a << " and " << b << " is " << a * b << endl;
return 0;
}
练习1.5
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
cout << "please enter two number:";
cin >> a;
cin >> b;
cout << "the sum of ";
cout << a;
cout << " and ";
cout << b;
cout << " is ";
cout << a + b;
cout << endl;
cout << "the multiply of ";
cout << a;
cout << " and ";
cout << b;
cout << " is ";
cout << a * b;
cout << endl;
return 0;
}
练习1.6
std::cout << "The sum of " << v1;
<< "and " << v2;
<< " is " << v1+v2 << std::endl;
不合法。前两行的末尾有分号,代表语句结束,第2、3两行为两条新的语句。而这两条语句在“<<”之前缺少了输出流,应在“<<”之前加上"cout
"。
改为:
练习1.7
#include <iostream>
/*
* 注释对/* */不能嵌套
* “不能嵌套”几个字会被认为是源码,像剩余程序一样处理
*/
int main (void)
{
return 0;
}
练习1.8
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "*/" /* "/*" */;
第一条和第二条语句是合法的,第三条语句的第一个双引号和第四个双引号左引号被注释掉了,第二个双引号和第三个双引号之间的“*/”被认为是字符串的文字内容。
练习1.9
#include <iostream>
using namespace std;
int main()
{
int a = 50;
int cut = 0;
while (a <= 100)
{
cut += a;
++a;
}
cout << "sum is " << cut << endl;
return 0;
}
练习1.10
#include <iostream>
using namespace std;
void main()
{
int i=10;
while (i >= 0)
{
cout << i << endl;
--i;
}
}
练习1.11
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
cout << "输入两个整数:";
cin >> a >> b;
if (a > b)
{
int t = 0;
t = a;
a = b;
b = t;
}
while (a <= b)
{
cout << a << " ";
++a;
}
return 0;
}
练习1.12
int sum = 0;
for (int i = -100; i <= 100; ++i)
sum += i;
-100到100之间(包含-100和100)的整数相加,sum的终值为0。
练习1.14
循环次数已知的情况下,for循环更为简洁;无法预知循环次数是,while循环更合适。
练习1.16
练习1.18
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
if (cin >> a)
{
int cut = 1;
while (cin >> b)
{
if (a == b)
{
++cut;
}
else
{
cout << a << "连续重复个数为:" << cut << "个" << endl;
cut = 0;
a = b;
}
}
cout << a << "连续重复个数为:" << cut << "个" << endl;
}
return 0;
}
练习1.20
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 <iostream>
#include "Sales_item.h"
using namespace std;
int main (void)
{
Sales_item book;
cout << "请输入销售记录:" << endl;
while (cin >> book)
cout << "ISBN、销售额和平均售价为 " << book << endl;
return 0;
}