文章目录
- 题目概览
- 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 <iostream>
int main()
{
std::cout << "Hello,World" << std::endl;
return 0;
}
练习1.4:我们的程序使用加法运算符+来将两个数相加。编写程序使用乘法运算符*,来打印两个数的积。
1.4 两数相乘
#include <iostream>
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 <iostream>
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 <iostream>
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 <iostream>
int main()
{
int i = 10;
while(i >= 0)
{
std::cout << i << std::endl;
--i;
}
return 0;
}
练习1.11:编写程序,提示用户输入两个整数,打印出这两个整数所指定的范围内的所有整数。
1.11 指定范围内的所有整数
#include <iostream>
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 <iostream>
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 <iostream>
int main()
{
for (int i = 10; i >= 0; --i)
std::cout << i << std::endl;
return 0;
}
#include <iostream>
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 <iostream>
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 <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item;
std::cin >> item;
std::cout << item <<std::endl;
return 0;
}
练习1.21:编写程序,读取两个 ISBN 相同的 Sales_item 对象,输出他们的和。
1.21 两个ISBN相同的对象
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1,item2;
std::cin >> item1 >> item2;
std::cout << item1 + item2 <<std::endl;
return 0;
}
练习1.22:编写程序,读取多个具有相同 ISBN 的销售记录,输出所有记录的和。
1.22 多个相同ISBN的对象
#include <iostream>
#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 <iostream>
#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 <iostream>
#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 process
if (std::cin >> total)
{
Sales_item trans; // variable to hold the running sum
// read and process the remaining transactions
while (std::cin >> trans)
{
// if we're still processing the same book
if (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 user
std::cerr << "No data?!" << std::endl;
return -1; // indicate failure
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)