大家好!欢迎大家来看我的博客。这是我的一篇博客,其实就是我自己写的答案也称不上什么博客😊。那么我为什么要发布这样的答案呢?原因很简单:C++Primer这本书没有配答案,想要答案呢就得再花好几十买一本答案,感觉没有买的必要,毕竟你可能和我一样,买了过几天就不做了…。所以我就打算出这么一个答案的博客,这样不仅可以让你方便的看到答案,还能让我有写下去的欲望。
当然了,你要是想请我喝茶,下面有码哦~

如果答案有错,请练习我修改。(email:1583288728@qq.com

第一章 开始

1.1节练习

1.1:查阅你使用的编译器文档,确定它所使用的文件命名约定,编译并运行第2也的main程序

1
2
3
4
5
6
7
8
9

#include <iostream>

using namespace std;

int main()
{
return 0;
}

1.2: 改写程序,让他返回-1.

1
2
3
4
5
6
7
8
9

#include <iostream>

using namespace std;

int main()
{
return -1;
}

1.2节练习

1.3:编写程序,在标准输出上打印Hello, World。

1
2
3
4
5
6
7
8
9
10
11

#include <iostream>

using namespace std;

int main()
{
cout << "Hello, World." << endl;
return 0;
}

1.4 我们的程序使用加法运算符+来将两个数相加。编写程序使用乘法运算符来打印两个数的积。

1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>

using namespace std;

int main()
{
int a = 0, b = 0;
cin >> a >> b;
cout << "a * b = " << a * b <<endl;
return 0;
}

1.5 我们将所有的输出操作放在一条很长的语句中。重写程序,将每个运算对象的打印操作放在一条独立的语句中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>

using namespace std;

int main()
{
int a = 0 , b = 0;
cin >> a >> b;
cout << "The sum of ";
cout << a;
cout << " and ";
cout << b;
cout << " is ";
cout << a + b << endl;

return 0;
}

1.6 不合法,每一行都要加上std::cout

1.3节练习

这一节的练习相对简单,自己就能解决,所以大家可以自己做做就行了,如果真的需要答案的话..可以自行搜索。推荐CSDN里面搜索。

1.4.1节练习

1.9 编写程序,使用while循环将50到100的整数相加。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>

using namespace std;

int main()
{
int sum = 0, i = 50;
while( i < 101){ //也可以写成i <= 100
sum += i;
i++;
}
cout << sum << endl;

return 0;
}

1.10 除了++运算符将运算对象的值增加1外,还有一个递减运算符(–)实现将值减少1.编写程序,使用递减运算符在循环中按递减顺序打印出10到0之间的整数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>

using namespace std;

int main()
{
int a = 10;
while(a){ //a为0,就不再进入while循环了
cout << a << " ";
a--;
}
cout << endl;
return 0;
}

1.11 编写程序,提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>

using namespace std;

int main()
{
int a = 0,b = 0,t = 0;
cout << "Please enter two numbers: ";
cin >> a >> b;
if(a>b){
t = a;
a = b;
b = t;
}
while(b >= a){
cout << b << " ";
b -- ;
}
cout << endl;
return 0;
}

1.4.2节练习

1.12 for循环将-100~100的 数相加,sum = 0。

1.13 利用for循环,重做1.4.1节练习

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
using namespace std;

int main()
{
int sum = 0, i = 50;
for( i; i <= 100; i++){
sum += i;
}
cout << sum << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
using namespace std;

int main()
{
int i = 10;
for( i; i > 0; i--){
cout << i << " ";
}
cout << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include <iostream>
using namespace std;

int main()
{
int a = 0 , b =0, t = 0;
cout << "Please enter two numbers : ";
cin >> a >> b;
if(a>b){
t = a;
a = b;
b = t;
}
for( a; a <= b; a++){
cout << a << " ";
}
cout << endl;
return 0;
}

1.14和1.15 同样不做解答。
1.17,1.18,1.19不做答案。

1.5节练习

1.20 读取一组书籍销售记录,将每条记录打印在标准输出上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <iostream>
#include "Sales_item.h"

using namespace std;

int main(){
Sales_item book;
while(cin >> book){
cout << book << endl;
}

return 0;
}

1.21 读取两条相同的ISBN的记录,输出它们的和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream>
#include "Sales_item.h"

using namespace std;

int main()
{
Sales_item item1,item2;
cin >> item1 >> item2;
if(item1.isbn()==item2.isbn()){
cout << item1 + item2 << endl;
}

return 0;
}

1.22 读取多个相同的ISBN记录,输出它们的和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include "Sales_item.h"

using namespace std;

int main()
{
Sales_item total,item;
cin >> item;
total = item;
while(cin >> item){
if(total.isbn() == item.isbn()){
total += item;
}
}
cout << total << endl;
return 0;
}
更新于

请我喝[茶]~( ̄▽ ̄)~*

Harry Zhu 微信支付

微信支付

Harry Zhu 支付宝

支付宝

Harry Zhu 贝宝

贝宝