C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1

时间:2023-03-09 01:51:12
C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1

Chapter 1.1

1. 每个C++程序都必须有且只能有一个main函数,main函数的返回类型必须是int。操作系统通过调用main函数来运行C++程序。

2. 一个函数的定义包含四部分:返回类型、函数名、形参列表以及函数体。

3. 当return语句包含一个值时,此返回值的类型必须与函数的返回类型相兼容。

4. 类型是程序设计最基本的概念之一。一种类型不仅定义了数据元素的内容,还定义了这类数据上可以进行的运算。

Chapter 1.2

1. C++包含了一个全面的标准库,标准库就是一个类型和函数的集合,每个C++编译器都必须支持。

2. 标准库中包含了iostream库,用来提供IO机制。iostream库包含了两个基础类型istream和ostream,分别表示输入流和输出流。一个流就是一个字符序列,术语“流”想要表达的是,随着时间的推移,字符是顺序生成或消耗的。

3. 标准库定义了4个IO对象:1. cin,一个istream类型的对象,也被称为标准输入,用来处理输入 2. cout,一个ostream类型的对象,也被称为标准输出,用来处理输出 3. cerr,一个ostream类型的对象,也被称为标准错误,通常写入到与标准输出相同的设备,用来输出错误信息或其他不属于程序正常逻辑的输出内容。默认情况下,写到cerr的数据是不缓冲的 4. clog,一个ostream类型的对象,用来报告程序的执行信息。默认情况下,写到clog的数据是被缓冲的。

4. #include指令和头文件的名字必须写在同一行中。

5. 输出(<<)运算符的计算结果就是其左侧运算对象。代码:

std::cout << "Enter two numbers:" << std::endl;

使用了输出运算符两次。因为输出运算符的结果是其左侧运算对象,所以第一个运算符的结果成为了第二个运算符的左侧

运算对象。因此,我们的代码等价于:

// 表达式 std::cout << "Enter two numbers:" 的运算结果是std::cout
std::cout << "Enter two numbers:";
std::cout << std::endl;

6. 输入(>>)运算符与输出运算符类似,返回其左侧运算对象作为其计算结果。因此,代码:

int v1 = , v2 = ;
std::cin >> v1 >> v2;

等价于:

// 表达式 std::cin >> v1 的运算结果是std::cin
int v1 = , v2 = ;
std::cin >> v1;
std::cin >> v2;

7. IO设备通常将输入(或输出)数据保存在一个缓冲区中。

8. std::endl是一个被称为操纵符的特殊值。写入std::endl的效果是结束当前行,并将与设备关联的缓冲区中的内容刷到设备中。

9. 标准库定义的所有名字都在命名空间std中。

Chapter 1.3

1. 编译器会忽略注释,因此注释对程序的行为或性能不会有任何影响。

2. C++中有两种注释:单行注释和界定符对注释。

单行注释以双斜线(//)开始,以换行符结束。这种注释可以包含任何文本,包括额外的双斜线。

界定符对注释以“/*”开始,以“*/”结束,可以包含除“*/”外的任意内容。

3. 界定符对注释不能嵌套。下面的代码中嵌套使用界定符对注释就会产生错误:

int main()
{
/* /* 注释1 */ 注释2 */
// "注释2 */" 会被当做源码
return ;
}

Chapter 1.4

1. 所谓语句块,就是用花括号包围的零条或多条语句的序列。语句块也是语句的一种,在任何要求使用语句的地方都可以使用语句块。

2. while语句的形式为:
while ( condition )    // 所谓condition就是一个产生真或假的结果的表达式
statement

while语句的执行过程是交替地检测condition和执行关联的语句statement,直至condition为假时停止。

3.

for ( int val = ; val <= ; ++val )
statement;

上述代码使用了for语句。每个for语句都包含两部分:循环头和循环体。循环头控制循环体的执行次数,它由三部分组成:初始化语句、循环条件以及表达式。在上述代码中,初始化语句为“int val = 1”,初始化语句只在for语句入口处执行一次。在上述代码中,循环条件为“val <= 10”,循环体每次执行前都会先检查循环条件,只要循环条件为真,循环体就会被执行。在上述代码中,表达式为“++val”,表达式在循环体之后执行,执行完表达式后,for语句就会重新检测循环条件。

4. 当使用一个istream对象作为条件时,其效果是检测流的状态。如果流是有效的,则条件为真。当遇到文件结束符或遇到一个无效输入时,istream对象的状态会变为无效。处于无效状态的istream对象会使条件变为假。

5. 当用键盘向程序输入数据时,对于如何指定文件结束,不同操作系统有不同的约定。在Windows系统中,输入文件结束符的方法是Ctrl + Z。在Unix系统中,包括Mac OS X系统中,输入文件结束符的方法是Ctrl + D。

Chapter 1.5

1. 每个类实际上都定义了一个新的类型,其类型名就是类名。

2. 大多数操作系统支持文件重定向,这种机制允许我们将标准输入和标准输出与命名文件关联起来:
$ addItems <infile >outfile

假定$是操作系统提示符,应用程序为addItems.exe,则上述命令会从一个名为infile的文件读取销售记录,并将输出结果写入到一个名为outfile的文件中,两个文件都位于当前目录中。

Exercises Section 1.2

Q_1. Write a program to print Hello, World on the standard output.

A_1.

#include <iostream>

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

Q_2. Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.

A_2.

#include <iostream>

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

Q_3. We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand.

A_3.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; std::cout << "The sum of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 + v2;
std::cout << std::endl; return ;
}

Q_4. Explain whether the following program fragment is legal.

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

If the program is legal, what does it do? If the program is not legal, why not? How would you fix it?

A_4. 这个程序是非法的,v1后的分号表示了一条语句的结束,所以程序会认为“<< " and " << v2;”是一条独立的语句,而这条语句缺少了左侧运算对象。同理,“<< " is " << v1 + v2 << std::endl;”也缺少了左侧运算对象。

修正如下:

std::cout << "The sum of " << v1    // 去除分号
<< " and " << v2 // 去除分号
<< " is " << v1 + v2 << std::endl;

Exercises Section 1.3

Q_1. Indicate which, if any, of the following output statements are legal:

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

After you've predicted what will happen, test your answers by compiling a program with each of these statements. Correct any errors you encounter.

A_1.

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

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

std::cout << /* "*/" */;
// 非法,修正为:
std::cout << /* "*/" */"; std::cout << /* "*/" /* "/*" */; // 合法

Exercises Section 1.4.1

Q_1. Write a program that uses a while to sum the numbers from 50 to 100

A_1.

#include <iostream>

int main()
{
int sum = , val = ;
while ( val <= )
{
sum += val;
++val;
} std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return ;
}

Q_2. In addition to the ++ operator that adds 1 to its operand, there is a decrement operator (--) that subtracts 1. Use the decrement operator to write a while that prints the numbers from ten down to zero.

A_2.

#include <iostream>

int main()
{
int val = ;
while ( val >= )
{
std::cout << val << std::endl;
--val;
} return ;
}

Q_3. Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

A_3.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; while ( v1 <= v2 )
{
std::cout << v1 << std::endl;
++v1;
} return ;
}

Exercises Section 1.4.2

Q_1. What does the following for loop do? What is the final value of sum?

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

A_1. 上述代码的功能是计算-100到100的和,最终sum的结果是0。

Q_2. Rewrite the exercises from § 1.4.1 (p. 13) using for loops

A_2.

#include <iostream>

int main()
{
int sum = ;
for ( int val = ; val <= ; ++val )
{
sum += val;
} std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return ;
}
#include <iostream>

int main()
{
for ( int val = ; val >= ; --val )
{
std::cout << val << std::endl;
} return ;
}
#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2; for ( ; v1 <= v2; ++v1 )
{
std::cout << v1 << std::endl;
} return ;
}

Exercises Section 1.4.3

Q_1. Write your own version of a program that prints the sum of a set of integers read from cin.

A_1.

#include <iostream>

int main()
{
int sum = ;
for ( int val; std::cin >> val; )
{
sum += val;
} std::cout << "Sum is: " << sum << std::endl; return ;
}

Exercises Section 1.4.4

Q_1. Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that printed a range of numbers so that it handles input in which the first number is smaller than the second.

A_1.

#include <iostream>

int main()
{
std::cout << "Enter two numbers:" << std::endl; int v1 = , v2 = ;
std::cin >> v1 >> v2;
if ( v1 > v2 )
{
// 如果v1大于v2,就进行交换
int temp = v1;
v1 = v2;
v2 = temp;
} while ( v1 <= v2 )
{
std::cout << v1 << std::endl;
++v1;
} return ;
}

Exercises Section 1.5.1

Q_1. http://www.informit.com/title/032174113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.

A_1.

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

Q_2. Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.

A_2.

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

Q_3. Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read.

A_3.

#include <iostream>
#include "Sales_item.h" int main()
{
Sales_item sum, val; // 读取第一笔交易
std::cin >> sum; // 累加所有交易
while ( std::cin >> val )
{
sum = sum + val;
} std::cout << sum << std::endl; return ;
}

Exercises Section 1.5.2

Q_1. Write a program that reads several transactions and counts how many transactions occur for each ISBN.

A_1.

#include <iostream>
#include "Sales_item.h" int main()
{
Sales_item lastItem;
if ( std::cin >> lastItem )
{
int count = ;
Sales_item curItem;
while ( std::cin >> curItem )
{
if ( lastItem.isbn() == curItem.isbn() )
{
++count;
}
else
{
std::cout << lastItem.isbn() << " occurs " << count; lastItem = curItem;
count = ;
}
} std::cout << lastItem.isbn() << " occurs " << count;
} return ;
}