C++ Primer第五版_第一章习题答案
迪丽瓦拉
2024-06-03 22:55:32
0

文章目录

      • 题目概览
        • 1.1 编译器文档
        • 1.2 错误标识
        • 1.3 Hello, World
        • 1.4 两数相乘
        • 1.5 独立语句
        • 1.6 程序合法性
        • 1.7 不正确的嵌套注释
        • 1.8 语句合法性
        • 1.9 50到100的整数相加
        • 1.10 递减顺序打印10到0之间的整数
        • 1.11 指定范围内的所有整数
        • 1.12 sum的终值
        • 1.13 for循环
        • 1.14 for循环 VS while循环
        • 1.15 编译器生成的错误信息
        • 1.16 cin读取一组数
        • 1.17 输入重复值
        • 1.18 输入全部相等的值
        • 1.20 读取一组书籍的销售记录
        • 1.21 两个ISBN相同的对象
        • 1.22 多个相同ISBN的对象
        • 1.23 统计每个ISBN销售记录
        • 1.25 书店程序

题目概览

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

1.1 编译器文档

参考:GCC and File Extensions

练习1.2:改写程序,让它返回-1。返回值-1通常被当作程序错误的标识。重新编译并运行你的程序,观察你的系统如何处理main返回的错误标识。

1.2 错误标识

参考:Exit Codes With Special Meanings

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

1.3 Hello, World

#include int main()
{std::cout << "Hello,World" << std::endl;return 0;
}

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

1.4 两数相乘

#include int main()
{std::cout << "Enter two numbers:" << std::endl;int v1 = 0, v2 = 0;std::cin >> v1 >> v2;std::cout << "The product is " << v1 * v2 << std::endl;return 0;
}

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

1.5 独立语句

#include int main()
{std::cout << "Enter two numbers:" << std::endl;int v1 = 0, v2 = 0;std::cin >> v1 >> v2;std::cout << "The product of ";std::cout << v1;std::cout << " and ";std::cout << v2;std::cout << " is ";std::cout << v1 * v2;std::cout << std::endl;return 0;
}

练习1.6:解释下面程序片段是否合法。

std::cout << "The sum of " << v1;
<< " and " << v2;
<< " is " << v1 + v2 << std::endl;

如果程序是合法的,它的输出是什么?如果程序不合法,原因何在?应该如何修正?

1.6 程序合法性

std::cout << "The sum of " << v1<< " and " << v2<< " is " << v1 + v2 << std::endl;

练习1.7:编译一个包含不正确的嵌套注释的程序,观察编译器返回的错误信息。

1.7 不正确的嵌套注释

$ g++ -o ex07 ex07.cpp 
ex07.cpp: In function ‘int main()’:
ex07.cpp:5:3: error: expected primary-expression before ‘/’ token*/^
ex07.cpp:6:2: error: expected primary-expression before ‘return’return 0;^
ex07.cpp:6:2: error: expected ‘;’ before ‘return’

练习1.8:指出下列哪些输出语句是合法的(如果有的话):

std::cout <<  "/*";
std::cout <<  "*/";
std::cout <<  /* "/*" */;
std::cout <<  /* "/*" /* "/*" */;

预测编译这些语句会产生什么样的结果,实际编译这些语句来验证你的答案(编写一个小程序,每次将上述一条语句作为其主体),改正每个编译错误。

1.8 语句合法性

第三行非法,第三行改为:

std::cout << /* "*/" */";

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

1.9 50到100的整数相加

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

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

1.10 递减顺序打印10到0之间的整数

#include int main()
{int i = 10;while(i >= 0){std::cout << i << std::endl;--i;}return 0;
}

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

1.11 指定范围内的所有整数

#include int main()
{int nSmall = 0, nBig = 0;std::cout << "please input two integers:";std::cin >> nSmall >> nBig;if (nSmall > nBig) {int nTmp = nSmall;nSmall = nBig;nBig = nTmp;}while (nSmall <= nBig) {std::cout << nSmall << " ";++nSmall;}std::cout << std::endl;return 0;
}

练习1.12:下面的for循环完成了什么功能?sum的终值是多少?

int sum = 0;
for (int i = -100; i <= 100; ++i)
sum += i;

1.12 sum的终值

求从-100加到100的和,最终值为0。

练习1.13:使用for循环重做1.4.1节中的所有练习(第11页)。

1.13 for循环

Ex1.9:
#include int main()
{int sum = 0;for (int i = 50; i <= 100; ++i) sum += i;std::cout << "the sum is: " << sum << std::endl;return 0;
}Ex1.10:
#include int main()
{for (int i = 10; i >= 0; --i)std::cout << i << std::endl;return 0;
}#include Ex1.11:
int main()
{std::cout << "please input two integers:\n";int small = 0, big = 0;std::cin >> small >> big;if (small > big){int tmp = small;small = big;big = tmp;}for (int i = small; i != big; ++i)std::cout << i << std::endl;return 0;
}

练习1.14:对比for循环和while循环,两种形式的优缺点各是什么?

1.14 for循环 VS while循环

已知迭代次数时,使用for循环比较简洁;不知道迭代次数时使用while。
参考:A similar question on Stack Overflow

练习1.15:编写程序,包含第14页“再探编译”中讨论的常见错误。熟悉编译器生成的错误信息。

1.15 编译器生成的错误信息

练习1.16:编写程序,从cin读取一组数,输出其和。

1.16 cin读取一组数

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

练习1.17:如果输入的所有值都是相等的,本节的程序会输出什么?如果没有重复值,输出又会是怎样的?

1.17 输入重复值

如果输入的所有值都是相等的,Ctrl+d后,才有输出统计的个数;如果没有重复输出,输入与前个不同的数即打印一行,Ctrl+d后,才输出最后一行统计的个数。

练习1.18:编译并运行本节的程序,给它输入全都相等的值。再次运行程序,输入没有重复的值。

1.18 输入全部相等的值

$ ./ex17 
1 1 1 1 1 
1 occurs 5 times
$ ./ex17 
1 2 3 4 5 
1 occurs 1 times
2 occurs 1 times
3 occurs 1 times
4 occurs 1 times
5 occurs 1 times

练习1.19:修改你为1.4.1节练习1.11(第11页)所编写的程序(打印一个范围内的数),使其能处理用户输入的第一个数比第二个数小的情况。

练习1.20:在网站http://www.informit.com/title/032174113 上,第1章的代码目录包含了头文件 Sales_item.h。将它拷贝到你自己的工作目录中。用它编写一个程序,读取一组书籍销售记录,将每条记录打印到标准输出上。

1.20 读取一组书籍的销售记录

#include 
#include "Sales_item.h"int main()
{Sales_item item;std::cin >> item;std::cout << item <

练习1.21:编写程序,读取两个 ISBN 相同的 Sales_item 对象,输出他们的和。

1.21 两个ISBN相同的对象

#include 
#include "Sales_item.h"int main()
{Sales_item item1,item2;std::cin >> item1 >> item2;std::cout << item1 + item2 <

练习1.22:编写程序,读取多个具有相同 ISBN 的销售记录,输出所有记录的和。

1.22 多个相同ISBN的对象

#include 
#include "Sales_item.h"int main()
{Sales_item item,sum_item;std::cin >> sum_item;while(std::cin >> item){sum_item += item;}std::cout << sum_item << std::endl;return 0;
}

练习1.23:编写程序,读取多条销售记录,并统计每个 ISBN(每本书)有几条销售记录。

1.23 统计每个ISBN销售记录

#include 
#include "Sales_item.h"int main()
{Sales_item currItem, valItem;if (std::cin >> currItem) {int cnt = 1;while (std::cin >> valItem) {if (valItem.isbn() == currItem.isbn())++cnt;else {std::cout << currItem << " occurs " << cnt << " times "<< std::endl;currItem = valItem;cnt = 1;}}std::cout << currItem << " occurs " << cnt << " times " << std::endl;}return 0;
}

练习1.24:输入表示多个 ISBN 的多条销售记录来测试上一个程序,每个 ISBN 的记录应该聚在一起。

练习1.25:借助网站上的 Sales_item.h 头文件,编译并运行本节给出的书店程序。

1.25 书店程序

#include 
#include "Sales_item.h"int main() 
{Sales_item total; // variable to hold data for the next transaction// read the first transaction and ensure that there are data to processif (std::cin >> total){Sales_item trans; // variable to hold the running sum// read and process the remaining transactionswhile (std::cin >> trans) {// if we're still processing the same bookif (total.isbn() == trans.isbn()) total += trans; // update the running total else {              // print results for the previous book std::cout << total << std::endl;  total = trans;  // total now refers to the next book}}std::cout << total << std::endl; // print the last transaction} else {// no input! warn the userstd::cerr << "No data?!" << std::endl;return -1;  // indicate failure}return 0;
}

上一篇:浏览器事件循环

下一篇:春日旅游路线

相关内容

热门资讯

linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
C++ 机房预约系统(六):学... 8、 学生模块 8.1 学生子菜单、登录和注销 实现步骤: 在Student.cpp的...
A.机器学习入门算法(三):基... 机器学习算法(三):K近邻(k-nearest neigh...
数字温湿度传感器DHT11模块... 模块实例https://blog.csdn.net/qq_38393591/article/deta...
有限元三角形单元的等效节点力 文章目录前言一、重新复习一下有限元三角形单元的理论1、三角形单元的形函数(Nÿ...
Redis 所有支持的数据结构... Redis 是一种开源的基于键值对存储的 NoSQL 数据库,支持多种数据结构。以下是...
win下pytorch安装—c... 安装目录一、cuda安装1.1、cuda版本选择1.2、下载安装二、cudnn安装三、pytorch...
MySQL基础-多表查询 文章目录MySQL基础-多表查询一、案例及引入1、基础概念2、笛卡尔积的理解二、多表查询的分类1、等...
keil调试专题篇 调试的前提是需要连接调试器比如STLINK。 然后点击菜单或者快捷图标均可进入调试模式。 如果前面...
MATLAB | 全网最详细网... 一篇超超超长,超超超全面网络图绘制教程,本篇基本能讲清楚所有绘制要点&#...
IHome主页 - 让你的浏览... 随着互联网的发展,人们越来越离不开浏览器了。每天上班、学习、娱乐,浏览器...
TCP 协议 一、TCP 协议概念 TCP即传输控制协议(Transmission Control ...
营业执照的经营范围有哪些 营业执照的经营范围有哪些 经营范围是指企业可以从事的生产经营与服务项目,是进行公司注册...
C++ 可变体(variant... 一、可变体(variant) 基础用法 Union的问题: 无法知道当前使用的类型是什...
血压计语音芯片,电子医疗设备声... 语音电子血压计是带有语音提示功能的电子血压计,测量前至测量结果全程语音播报࿰...
MySQL OCP888题解0... 文章目录1、原题1.1、英文原题1.2、答案2、题目解析2.1、题干解析2.2、选项解析3、知识点3...
【2023-Pytorch-检... (肆十二想说的一些话)Yolo这个系列我们已经更新了大概一年的时间,现在基本的流程也走走通了,包含数...
实战项目:保险行业用户分类 这里写目录标题1、项目介绍1.1 行业背景1.2 数据介绍2、代码实现导入数据探索数据处理列标签名异...
记录--我在前端干工地(thr... 这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 前段时间接触了Th...
43 openEuler搭建A... 文章目录43 openEuler搭建Apache服务器-配置文件说明和管理模块43.1 配置文件说明...