习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html

第1章 开始&&第2章 变量和基本类型


练习1.3

#include<iostream>
int main(){
std::cout<<"Hello world"<<std::endl;
return 0;
}

练习1.4

#include<iostream>
int main(){
std::cout << "Input two numbers: " << std::endl;
int a, b;
std::cin >> a >> b;
std::cout << a <<" * "<< b << " = " << a * b << std::endl;
}

练习1.5

#include<iostream>
int main(){
std::cout << "Input two numbers: " << std::endl;
int a, b;
std::cin >> a >> b;
std::cout << a;
std::cout<<" * ";
std::cout<< b ;
std::cout<< " = " ;
std::cout<< a * b ;
std::cout<< std::endl;
}

练习1.6

不合法,第一行有分号表示语句结束,改为如下:

#include<iostream>
int main(){
std::cout << "Input two numbers: " << std::endl;
int a, b;
std::cin >> a >> b;
std::cout << "The sum of "<< a << " and " << b<< " is " << a + b <<std::endl;
return 0;
}

练习1.7

#include<iostream>
int main(){
/*
/* */注释不能嵌套!
*/
return 0;
}

练习1.8

第三行错误,因前双引号被注释掉了,后双引号不匹配。

#include<iostream>
int main(){
std::cout << "/*"<<std::endl;
std::cout << "*/"<<std::endl;
//std::cout << /* "*/" */<<std::endl;
std::cout << /* "*/" /* "/*" */<<std::endl;
return 0;
}

练习1.9

 #include<iostream>
int main(){
int sum = 0, val = 50;
while (val <= 100){
sum += val;
++val;
}
std::cout << sum << std::endl;
return 0;
}

练习1.10

#include<iostream>
int main(){
int sum = 0, val = 10;
while (val >= 0){
sum += val;
--val;
}
std::cout << sum << std::endl;
return 0;
}

练习1.11

#include<iostream>
int main(){
int a, b;
std::cin >> a >> b;
while (a <= b){
std::cout << a << " ";
++a;
}
return 0;
}

练习1.12

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

练习1.14

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

练习1.16

#include<iostream>
int main(){
int a, sum = 0;
while(std::cin >> a){
sum += a;
}
std::cout << sum;
return 0;
}

练习1.19

#include<iostream>
int main(){
int a, b;
std::cin >> a >> b;
if( a > b ){
int temp = a;
a = b;
b = temp;
}
while (a <= b){
std::cout << a << " ";
++a;
}
return 0;
}

练习1.20

#include <iostream>
#include "Sales_item.h"
int main(){
Sales_item book;
while(std::cin >> book){
std::cout << "Record: " << book <<std::endl;
}
return 0;
}

练习1.21

#include <iostream>
#include "Sales_item.h"
int main(){
Sales_item book1, book2;
std::cin >> book1 >> book2;
std::cout << book1 + book2 <<std::endl;
return 0;
}

练习2.8

#include<iostream>
int main(){
cout<<"2M"<<'\n';
cout<<'2'<<'\t'<<'M'<<'\n';
}

练习2.9

a.需要在cin前定义变量名

b.3.14强制转换为Int有精度损失

c.wage未定义

d.同b

练习2.15

a.定义合法但有精度损失

b.引用类型的初始值必须是一个对象

c.正确

d.同b

练习2.17

10 10

练习2.27

a.不合法,引用r的赋值对象必须是一个对象

b.合法,将p2设置为一个常量指针,初始化为i2对象的地址

c.合法,将i设为常量-1,r设置为常量的引用

d.合法,将p3设为指向常量的常量指针,初始化为i2的地址

e.合法,将p1设为指向常量的指针,初始化为i2的地址

f.不合法,常量指针必须初始化

g.合法

练习2.28

a.不合法,常量指针必须初始化

b.不合法,同a

c.不合法,常量ic未初始化

d.不合法,同a

e.合法。


不断学习中,欢迎交流!

C++Primer第五版——习题答案详解(一)的更多相关文章

  1. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  2. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  3. C++Primer第五版——习题答案详解(四)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...

  4. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  5. C++Primer第五版——习题答案详解(六)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

  6. C++Primer第五版——习题答案详解(七)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...

  7. C++Primer第五版——习题答案详解(八)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...

  8. C++Primer第五版——习题答案详解(九)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...

  9. C++Primer第五版——习题答案详解(十)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...

随机推荐

  1. 转载 [c#] 虚函数(Virtual),抽象函数(abstract)和接口的区别

    1.virtual:允许被重写,但不强制要求.声明时提供其自身实现: 2.abstract:强制要求其继承者重写.声明时不提供其自身的实现,抽象类不能被实例化: 3.interface:接口就是协议, ...

  2. python命名空间与作用域

    python命名空间与作用域   命名空间是名称与对象之间的关系,可以将命名空间看做是字典,其中的键是名称,值是对象. 命名空间不共享名称. 在命名空间中的名称能将任何python对象作为值,在不同的 ...

  3. 一直又爱又恨的jqueryValidate,看到一个还不错的laber.error样式

    默认样式,不是很好看 修改之后就高大上多了 功臣如下: label.error {    position: absolute;    right: 18px;    top: 5px;   colo ...

  4. lenet-5,Alexnet详解以及tensorflow代码实现

    http://blog.csdn.net/OliverkingLi/article/details/73849228

  5. Java容器解析系列(6) Queue Deque AbstractQueue 详解

    首先我们来看一下Queue接口: /** * @since 1.5 */ public interface Queue<E> extends Collection<E> { / ...

  6. error: checker javascript/jshint: can’t parse version string (abnormal termination?)”

    vim 安装插件(k-vim方法 )好后 编辑js文件提示错误 可能是nodejs环境没搭建好 或者版本有误 用nvm安装node 后 需要 source ~/.bashrc 或者重新开一个终端 再运 ...

  7. effective java——31用实例域代替序数

    1,永远不要根据枚举的序数导出与它关联的值,而是要将他保存在一个实例域中.(ordinal()) public enum Ensemble { SOLO, DUET, TRIO, QUARTET, Q ...

  8. [python] 使用Jieba工具中文分词及文本聚类概念

    声明:由于担心CSDN博客丢失,在博客园简单对其进行备份,以后两个地方都会写文章的~感谢CSDN和博客园提供的平台.        前面讲述了很多关于Python爬取本体Ontology.消息盒Inf ...

  9. Delphi xe8 FMX StringGrid根据内容自适应列宽。

    Delphi xe8 FMX StringGrid根据内容自适应列宽. 网上的资料比较复杂,而且不是根据字体字号等设置列宽.故自己写了个function来用. function GetColMaxDa ...

  10. 【ShareCode】不错的技术文章 -- 如何使用异或(XOR)运算找到数组中缺失的数?

    如何使用异或(XOR)运算找到数组中缺失的数? 今天给大家分享一篇关于使用XOR(异或)运算找到数组中缺失的数的问题. 在一次Javascript面试中,有这么一个问题: 假设有一个由0到99(包含9 ...