C++ Primer(第五版)读书笔记 & 习题解答 --- Chapter 1(二)
<< "Enter two numbers:" << std::endl;
?
? ? int v1 = 0, v2 = 0;
? ? 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 0;
}
复制代码
?
?
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
?
int main()
{
? ? int sum = 0, val ?= 50;
? ? while ( val <= 100 )
? ? {
? ? ? ? sum += val;
? ? ? ? ++val;
? ? }
?
? ? std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
?
? ? return 0;
}
复制代码
?
?
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
?
int main()
{
? ? int val = 10;
? ? while ( val >= 0 )
? ? {
? ? ? ? std::cout << val << std::endl;
? ? ? ? --val;
? ? }
?
? ? return 0;
}
复制代码
?
?
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
?
int main()
{
? ? std::cout << "Enter two numbers:" << std::endl;
?
? ? int v1 = 0, v2 = 0;
? ? std::cin >> v1 >> v2;
?
? ? while ( v1 <= v2 )
? ? {
? ? ? ? std::cout << v1 << std::endl;
? ? ? ? ++v1;
? ? }
?
? ? return 0;
}
复制代码
?
?
Exercises Section 1.4.2
Q_1. What does the following for loop do? What is the final value of sum?
?
int sum = 0;
for (int i = -100; i <= 100; ++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
?
int main()
{
? ? int sum = 0;
? ? for ( int val = 50; val <= 100; ++val )
? ? {
? ? ? ? sum += val;
? ? }
?
? ? std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
?
? ? return 0;
}
复制代码
复制代码
#include
?
int main()
{
? ? for ( int val = 10; val >= 0; --val )
? ? {
? ? ? ? std::cout << val << std::endl;
? ? }
?
? ? return 0;
}
复制代码
复制代码
#include
?
int main()
{
? ? std::cout << "Enter two numbers:" << std::endl;
? ??
? ? int v1 = 0, v2 = 0;
? ? std::cin >> v1 >> v2;
?
? ? for ( ; v1 <= v2; ++v1 )
? ? {
? ? ? ? std::cout << v1 << std::endl;
? ? }
?
? ? return 0;
}
复制代码
?
?
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
?
int main()
{
? ? int sum = 0;
? ? for ( int val; std::cin >> val; )
? ? {
? ? ? ? sum += val;
? ? }
?
? ? std::cout << "Sum is: " << sum << std::endl;
?
? ? return 0;
}
复制代码
?
?
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
?
int main()
{
? ? std::cout << "Enter two numbers:" << std::endl;
?
? ? int v1 = 0, v2 = 0;
? ? 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 0;
}
复制代码
?
?
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
#include "Sales_item.h"
?
int main()
{
? ? Sales_item book;
? ? while ( std::cin >> book )
? ? {
? ? ? ? std::cout << book << std::endl;
? ? }
?
? ? return 0;
}
复制代码
?
?
Q_2. Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.
?
A_2.
?
复制代码
#include
#include "Sales_item.h"
?
int main()
{
? ? Sales_item item1, item2;
? ? std::cin >> item1 >> item2;
? ? std::cout << item1 + item2 << std::endl;
?
? ? return 0;
}
复制代码
?
?
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
#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 0;
}
复制代码
?
?
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
#include "Sales_item.h"
?
int main()
{
? ? Sales_item lastItem;
? ? if ( std::cin >> lastItem )
? ? {
? ? ? ? int count = 1;
? ? ? ? Sales_item curItem;
? ? ? ? while ( std::cin >> curItem )
? ? ? ? {
? ? ? ? ? ? if ( lastItem.isbn() == curItem.isbn() )
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ++count;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? std::cout << lastItem.isbn() << " occurs " << count;
?
? ? ? ? ? ? ? ? lastItem = curItem;
? ? ? ? ? ? ? ? count = 1;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? std::cout << lastItem.isbn() << " occurs " << count;
? ? }
?
? ? return 0;