C++Primer第五版 习题答案 【总目录】:https://blog.csdn.net/Dust_Evc/article/details/114334124
1.1
Visual C++ 项目中使用的文件类型:https://docs.microsoft.com/zh-cn/cpp/build/reference/file-types-created-for-visual-cpp-projects?view=msvc-150
1.2
int main()
{
return -1;
}
1.3
#include <iostream>
int main()
{
std::cout << "Hello, World" << std::endl;
return 0;
}
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 of " << v1 << " and " << v2
<< " is " << v1 * v2 << std::endl;
return 0;
}
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
不合法。
**错误 error C2143: 语法错误 : 缺少“;”(在“<<”的前面)**
修正:移除掉多余的分号。
std::cout << "The sum of " << v1
<< " and " << v2
<< " is " << v1 + v2 << std::endl;
1.7
/*
* 注释在这里嵌套
* /* 这里的嵌套是错的 */
*
*/
int main()
{
return 0;
}
Error E0169: expected a declaration.
1.8
第三行编译出错,改正方法是增加一个引号。 std::cout << /* "*/" */";
输出:/**/ */ /*
1.9
#include <iostream>
int main()
{
int sum = 0, i = 50;
while (i <= 100)
{
sum += i;
++i;
}
std::cout << sum << std::endl;
return 0;
}
1.10
#include <iostream>
int main()
{
int i = 10;
while (i >= 0)
{
std::cout << i << std::endl;
--i;
}
return 0;
}
1.11
#include <iostream>
void print_range(int lo, int hi)
{
if (lo > hi)
{
print_range(hi, lo);
return;
}
while (lo <= hi)
{
std::cout << lo << std::endl;
++lo;
}
}
int main()
{
int low, high;
std::cout << "please input two numbers : " << std::endl;
std::cin >> low >> high;
print_range(low, high);
return 0;
}
1.12
从 -100 加到 100 ,sum 的终值为 0。
1.13
习题1.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;
}
习题1.10:
#include <iostream>
int main()
{
for (int i = 10; i >= 0; --i)
std::cout << i << std::endl;
return 0;
}
习题1.11:
#include <iostream>
void print_range(int lo, int hi)
{
if (lo > hi)
{
print_range(hi, lo);
return;
}
for (int i = lo; i <= hi; ++i)
{
std::cout << i << std::endl;
}
}
int main()
{
int low, high;
std::cout << "please input two numbers : " << std::endl;
std::cin >> low >> high;
print_range(low, high);
return 0;
}
1.14
1、在for循环中,循环控制变量的初始化bai和修du改都放在语句头zhi部分,形式较简洁,且特别适用于循环次数已知的情况。
2、在while循环中,循环控制变量的初始化一般放在while语句之前,循环控制变量的修改一般放在循环体中,形式上不如for语句简洁,但它比较适用于循环次数不易预知的情况(用某一条件控制循环)。
3、两种形式各有优点,但它们在功能上是等价的,可以相互转换。
1.15
常见的编译器报错有语法错误、类型错误、声明错误。
1.16
#include <iostream>
int main()
{
int sum = 0, value = 0;
while (std::cin >> value)
{
sum += value;
}
std::cout << sum << std::endl;
return 0;
}
1.18
#include <iostream>
int main()
{
int currVal = 0, val = 0;
if (std::cin >> currVal)
{
int cnt = 1;
while (std::cin >> val)
{
if (val == currVal)
{
++cnt;
}
else
{
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
currVal = val;
cnt = 1;
}
}
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
}
return 0;
}
1.19
#include <iostream>
void print_range(int lo, int hi)
{
if (lo > hi)
{
print_range(hi, lo);
return;
}
while (lo <= hi)
{
std::cout << lo << std::endl;
++lo;
}
}
int main()
{
int low, high;
std::cout << "please input two numbers : " << std::endl;
std::cin >> low >> high;
print_range(low, high);
return 0;
}
1.20
#include <iostream>
#include "Sales_item.h"
using std::cin;
using std::cout;
using std::endl;
int main()
{
for (Sales_item item; cin >> item; cout << item << endl);
return 0;
}
1.21
#include <iostream>
#include "Sales_item.h"
#include <stdexcept>
#include <string>
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
if (item1.isbn() == item2.isbn())
{
std::cout << item1 + item2 << std::endl;
return 0;
}
else
{
std::cerr << "Data must refer to same ISBN"
<< std::endl;
return -1;
}
}
1.22
#include <iostream>
#include "Sales_item.h"
using namespace std;
#define OK 0;
int main()
{
Sales_item total;
if (cin >> total)
{
Sales_item trans;
while (cin >> trans)
{
if (total.isbn() == trans.isbn())
total += trans;
else
{
cout << "ISBN号为【" << total.isbn() << "】的书籍,总记录为:";
cout << total << endl;
total = trans;
}
}
cout << "ISBN号为【" << total.isbn() << "】的书籍,总记录为:";
cout << total << endl;
}
else
{
cerr << "No data?!" << endl;
return -1;
}
return OK;
}
1.23
#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
代码同上。