答案目录

第1章 开始

两数相加

1
2
3
4
5
6
7
8
9
10
#include<iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}

练习1.3

1
2
3
4
5
6
#include<iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}

练习1.4

1
2
3
4
5
6
7
8
9
10
#include<iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 , v2 ;
std::cin >> v1 >> v2;
std::cout << "The multiplication of " << v1 << " and " << v2
<< " is " << v1 * v2 << std::endl;
return 0;
}

练习1.5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
int main()
{
std::cout << "Enter two number" << std::endl;
int a, b;
std::cin >> a >> b;
std::cout << "The multiplication of ";
std::cout << a;
std::cout << " and ";
std::cout << b;
std::cout << " is ";
std::cout << a * b;
std::cout << std::endl;
return 0;
}

练习1.6 不合法,第一行有分号表示语句结束

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
int main()
{
std::cout << "Input two numbers: " << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1
<< " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}

练习1.8

1
2
3
4
5
6
7
8
9
#include<iostream>
int main()
{
std::cout << "/*" << std::endl;
std::cout << "*/" << std::endl;
//std::cout << /* "*/" */<<std::endl;
std::cout << /* "*/" /* "/*" */ << std::endl;
return 0;
}

练习1.9

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
int main()
{
int sum = 0, val = 50;
while (val <= 100) {
sum += val;
++val;
}
std::cout << "Sum of 50 to 100 inclusive is "
<< sum << std::endl;
return 0;
}

练习1.10

1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
int main()
{
int sum = 0, val = 10;
while (val >= 0) {
sum += val;
--val;
}
std::cout << "Sum of 0 to 10 inclusive is "
<< sum << std::endl;
return 0;
}

练习1.11(以下代码满足练习1.19的要求)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
int main()
{
int a, b, c = 0;
std::cout << "Enter two numbers" << std::endl;
std::cin >> a >> b;
if (a > b) {
c = a;
a = b;
b = c;
}
while (b >= a) {
std::cout << a << " ";
++a;
}
return 0;
}

练习1.12 程序的功能是求[-100, 100]范围内的整数的和,sum的终值为0

1
2
3
4
5
6
7
8
9
#include<iostream>
int main()
{
int sum = 0;
for (int i = -100; i <= 100; ++i)
sum += i;
std::cout << sum << std::endl;
return 0;
}

练习1.13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
int main()
{
int sum = 0;
int a, b, c = 0;
for (int i = 50; i <= 100; ++i)
sum += i;
std::cout << "The sum of 50 to 100 is " << sum << std::endl;
for (int j = 10; j >= 0; --j) {
std::cout << j << " ";
if (j == 0)
std::cout << std::endl;
}
std::cout << "Enter two numbers" << std::endl;
std::cin >> a >> b;
if (a > b) {
c = a;
a = b;
b = c;
}
for (; a <= b; ++a)
std::cout << a << " ";
return 0;
}

练习1.14

已知循环次数的时候用for简便,未知时用while简便

练习1.16

文件结束符(Windows系统为Ctrl+Z,然后按Enter)

1
2
3
4
5
6
7
8
9
#include<iostream>
int main()
{
int sum = 0, value = 0;
while (std::cin >> value)
sum += value;
std::cout << "Sum is " << sum << std::endl;
return 0;
}

统计值出现次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
int main()
{
// currVal 是我们正在统计的数;我们将读入的新值存入val
int currVal = 0,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;
}

练习1.20

Sales_ item.h代码,在头文件创建该文件输入以下代码就可以用了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#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

练习1.21

1
2
3
4
5
6
7
8
#include <iostream>
#include "Sales_item.h"
int main(){
Sales_item book1, book2;
std::cin >> book1 >> book2;
std::cout << book1 + book2 <<std::endl;
return 0;
}