C++ Primer课后习题解答(第一章)
Preface
使用的书籍为《C++ Primer》第五版 英文版
Exercises Section 1.1
Ex1.2

Exercises Section 1.2
Ex1.3

Ex1.4

Ex1.5

Ex1.6
v1 后面多了一个冒号
Exercises Section 1.3
Ex1.7

Ex1.8
std::cout << "/*"; // 合法
std::cout << "*/"; // 合法
std::cout << /* "*/" */; // 不合法
std::cout << /* "*/" /* "/*" */; // 合法

不合法的更改如下:
std::cout << /* "*/" */";

Exercises Section 1.4.1
Ex1.9
#include<iostream>
int main()
{
int sum = 0;
int num = 50;
while (num <= 100)
{
sum = sum + num;
num = num + 1;
}
std::cout << sum << std::endl;
system("pause");
return 0;
}
Ex1.10
#include<iostream>
int main()
{
int num = 10;
while (num >= 0)
{
std::cout << num << " ";
num--;
}
std::cout << std::endl;
system("pause");
return 0;
}
Ex1.11
#include<iostream>
int main()
{
int v1 = 0, v2 = 0;
std::cout << "Enter two numbers: ";
std::cin >> v1 >> v2;
while (v1 <= v2)
{
std::cout << v1 << " ";
v1++;
}
std::cout << std::endl;
system("pause");
return 0;
}
Exercises Section 1.4.2
Ex1.12
计算 -100 到 100 范围内所有整数的和; 最后的值为0
Ex1.13
重写了第一个,其实都是一样的
#include<iostream>
int main()
{
int sum = 0;
for (int i = 50; i <= 100; i++)
{
sum += i;
}
std::cout << sum << std::endl;
system("pause");
return 0;
}
Ex1.14
当知道循环次数时,使用 for 循环更方便;条件循环时,使用 while 循环更好。
Exercises Section 1.4.3
Ex1.16
#include<iostream>
int main()
{
int sum = 0, value = 0;
while (std::cin >> value)
sum += value;
std::cout << "Sum is: " << sum << std::endl;
system("pause");
return 0;
}
Exercises Section 1.4.4
Ex1.17
一个数字出现多次;多个数字,每个出现一次。
Ex1.18

Ex1.19
重写了 1.11:
#include<iostream>
int main()
{
int v1 = 0, v2 = 0;
std::cout << "Enter two numbers: ";
std::cin >> v1 >> v2;
if (v1 > v2)
{
int temp = v1;
v1 = v2;
v2 = temp;
}
while (v1 <= v2)
{
std::cout << v1 << " ";
v1++;
}
std::cout << std::endl;
system("pause");
return 0;
}
Exercises Section 1.5.1
Ex1.20
Sales_item.h 文件如下:
#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>
class Sales_item
{
public:
Sales_item(const std::string& book) :isbn(book), units_sold(0), revenue(0.0) {}
Sales_item(std::istream& is) { is >> *this; }
friend std::istream& operator>>(std::istream&, Sales_item&);
friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
Sales_item& operator+=(const Sales_item&);
public:
double avg_price() const;
bool same_isbn(const Sales_item& rhs)const
{
return isbn == rhs.isbn;
}
Sales_item() :units_sold(0), revenue(0.0) {}
public:
std::string isbn;
unsigned units_sold;
double revenue;
};
using std::istream;
using std::ostream;
Sales_item operator+(const Sales_item&, const Sales_item&);
inline bool operator==(const Sales_item& lhs, const Sales_item& rhs)
{
return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs);
}
inline bool operator!=(const Sales_item& lhs, const Sales_item& rhs)
{
return !(lhs == rhs);
}
inline Sales_item& Sales_item::operator +=(const Sales_item& rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs)
{
Sales_item ret(lhs);
ret += rhs;
return ret;
}
inline istream& operator>>(istream& in, Sales_item& s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_item();
return in;
}
inline ostream& operator<<(ostream& out, const Sales_item& s)
{
out << s.isbn << "\t" << s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();
return out;
}
inline double Sales_item::avg_price() const
{
if (units_sold)
return revenue / units_sold;
else
return 0;
}
#endif
Ex1.21
#include<iostream>
#include "Sales_item.h"
int main()
{
Sales_item t1, t2;
std::cin >> t1 >> t2;
if (t1.isbn == t2.isbn)
{
std::cout << t1 + t2 << std::endl;
return 0;
}
else
{
std::cerr << "Data must refer to same ISBN" << std::endl;
return -1;
}
}
Ex1.22
#include<iostream>
#include "Sales_item.h"
int main()
{
Sales_item total, item;
if (std::cin >> total)
{
while (std::cin >> item)
{
if (item.isbn == total.isbn)
{
total += item;
}
else
{
std::cout << total << std::endl;
total = item;
}
}
std::cout << total << std::endl;
}
else
{
std::cerr << "No data !" << std::endl;
return -1;
}
return 0;
}
Ex1.23
#include<iostream>
#include "Sales_item.h"
int main()
{
Sales_item total, item;
if (std::cin >> total)
{
int cnt = 1;
while (std::cin >> item)
{
if (item.isbn == total.isbn)
{
cnt++;
}
else
{
std::cout << total.isbn << " " << cnt << std::endl;
total = item;
}
}
std::cout << total.isbn << " " << cnt << std::endl;
}
else
{
std::cerr << "No data !" << std::endl;
return -1;
}
return 0;
}

浙公网安备 33010602011771号