C++Primer第五版——习题答案详解(一)
习题答案目录: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第五版——习题答案详解(一)的更多相关文章
- C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- C++Primer第五版——习题答案详解(五)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- C++Primer第五版——习题答案详解(七)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...
- C++Primer第五版——习题答案详解(八)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...
- C++Primer第五版——习题答案详解(九)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...
- C++Primer第五版——习题答案详解(十)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...
随机推荐
- mysql查询今天、昨天、上周
mysql查询今天.昨天.上周 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE ...
- jvm回收器回收过程一:CMS和 G1的初认知(持续更新中)
CMS:介绍: 1.CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合. 在启动JVM参 ...
- [la P5031&hdu P3726] Graph and Queries
[la P5031&hdu P3726] Graph and Queries Time Limit: 10000/5000 MS (Java/Others) Memory Limit: ...
- C++使用: C++中map的基本操作和用法
在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...
- VS2017调试技巧
Visual Studio的调试技巧 调试技巧是衡量程序员水平的一个重要指标.掌握好的调试技巧与工具的使用方法,也是非常重要的.*** 演示环境: VS2017C#*** 演示用的代码: publ ...
- RAID的基本介绍
一.传统磁盘的劣势 影响计算机性能的组件一般包括:CPU.主板总线IO.内存IO.硬盘IO.网卡IO.现代处理器性能已经很高了,但是计算机整体IO性能较弱,严重影响了计算机性能 现代的计算机总线.内存 ...
- bfs两种记录路径方法
#include<cstdio> #include<queue> using namespace std; struct sss { int x,y; }ans[][]; ][ ...
- HTML5:链接与路径
链接与路径 一.路径 绝对路径——指包含服务器协议的完全路径 相对路径——指被链接文档相对于当前文档的路径. 二.超链接<a> 1.语法: <a href=“目标”>链接文本& ...
- restful规范整理
restful的十条规范 restful一共有十条规范,但其并不是规定.可以不去遵守,是一种软件风格 1.API与客户端交互,通常使用https协议 2.域名:https://api.baidu.co ...
- sed语法2
sed命令是一个面向字符流的非交互式编辑器,也就是说sed不允许用户与它进行交互操作.sed是按行来处理文本内容的.在shell中,使用sed来批量修改文本内容是非常方便的. sed命令的选项 sed ...