C++ Primer课后习题解答(第四章)
Exercises Section 4.1.2
Ex4.1
5 + 10 * 20 / 2 = 105
Ex4.2
a) *(vec.begin())
b) (*(vec.begin())) + 1
Exercises Section 4.2
Ex4.4
((12 / 3) * 4) + (5 * 15) + ((24 % 4) / 2) = 91
Ex4.5
a) -30 * 3 + 21 / 5 = -86
b) -30 + 3 * 21 / 5 = -18
c) 30 / 3 * 21 % 5 = 0
d) -30 / 3 * 21 % 4 = -2
Ex4.6
#include<iostream>
using namespace std;
int main()
{
int value;
cin >> value;
if (value % 2 == 0)
cout << "The number is even " << endl;
else
cout << "The number is odd" << endl;
system("pause");
return 0;
}
Ex4.7
Exercises Section 4.3
Ex4.9
const char *cp = "Hello World";
if (cp && *cp) // 如果指针 cp 的内容不为 0 且指针 cp 指向的地址内的内容不为 0,那么条件成立
Ex4.10
#include<iostream>
using namespace std;
int main()
{
int value;
while ((cin >> value) && (value != 42))
{
cout << value << " ";
}
cout << endl;
system("pause");
return 0;
}
Ex4.11
#include<iostream>
using namespace std;
void test(int a, int b, int c, int d)
{
if ((a > b) && (b > c) && (c > d))
{
cout << "Success" << endl;
}
else
{
cout << "Failed" << endl;
}
}
int main()
{
int a, b, c, d;
cout << "Please enter four numbers: " << endl;
cin >> a >> b >> c >> d;
test(a, b, c, d);
system("pause");
return 0;
}
Ex4.12
int i, j, k;
if (i != j < k) // 如果 j < k 那么就判断 i 是否等于 1,不等于则条件成立; 如果 j >= k 那么就判断 i 是否等于 0,不等于则条件成立
Exercises Section 4.4
Ex4.13
int i; double d;
a) d = i = 3.5; // i = 3, d = 3.0
b) i = d = 3.5; // d = 3.5, i = 3
Ex4.14
if (42 = i) // 非法,42不能为左值
if (i = 42) // 合法,变量 i 被赋值42,条件一直成立
Ex4.15
double dval; int ival; int *pi;
dval = ival = pi = 0; // int double 类型都不能和指针类型发生转换
// 修改如下:
dval = ival = *pi = 0;
Ex4.16
a) if (p = getPtr() != 0) // origin
if ((p = getPtr()) != 0) // 修改
b) if (i = 1024) // origin
if (i == 1024) // 修改
Exercises Section 4.5
Ex4.17
前缀增量符例如 "++i",先增加变量 i 的值然后再使用 i 的值;后缀增量符例如 "i++"先使用变量 i 然后再把 i 的值加1。而且后缀增量符会创建一个临时变量,效率没有相同效果的前缀增量符高。
Ex4.18
如果使用了前缀增量符会超出 vector 容器容量。
Ex4.19
int *ptr; vector<int> vec; int ival;
a) ptr != 0 && *ptr++ // 指针 ptr 内容不为 0 且指针 ptr 指向的地址的内容不为 0 条件成立,判断后增加 ptr 的值
b) ival++ && ival // 可能是不正确的
++ival && ival // 修改
c) vec[ival++] <= vec[ival] // 可能是不正确的
vec[++ival] <= vec[ival] // 修改
Exercises Section 4.6
Ex4.20
vector<string>::iterator iter;
a) *iter++; // 合法
b) (*iter)++; // 非法; 修改:*iter++
c) *iter.empty() // 非法; 修改:(*iter).empty()
d) iter->empty() // 合法
e) ++*iter; // 非法;修改:*iter++
f) iter++->empty(); // 合法
Exercises Section 4.7
Ex4.21
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int value;
vector<int> vec;
while (cin >> value)
{
vec.push_back(value);
}
for (auto it = vec.begin(); it != vec.end(); ++it)
{
if (*it % 2 == 0)
cout << *it << " is even" << endl;
else
cout << *it << " is odd" << endl;
}
system("pause");
return 0;
}
Ex4.23
string s = "word";
string p1 = s + s[s.size() - 1] == 's' ? "" : "s"; // 类型不匹配
// 修改如下
string p1 = s + (s[s.size() - 1] == 's' ? "" : "s");
Exercises Section 4.8
Ex4.25
'q' --> 01110001
~'q' --> 10001110
(~'q' << 6) --> 10000000
Ex4.27
unsigned long ul1 = 3, ul2 = 7;
a) ul1 & ul2 // 3
b) ul1 | ul2 // 7
c) ul1 && ul2 // true
d) ul1 || ul2 // true
Exercises Section 4.9
Ex4.28
#include<iostream>
using namespace std;
int main()
{
cout << sizeof(char) << endl;
cout << sizeof(short) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(float) << endl;
cout << sizeof(double) << endl;
system("pause");
return 0;
}
Ex4.29
int x[10]; int *p = x;
cout << sizeof(x) / sizeof(*x) << endl; // 结果为 10
cout << sizeof(p) / sizeof(*p) << endl; // 结果为 2
Ex4.30
a) sizeof(x + y)
b) sizeof(p->mem[i])
c) sizeof(a) < b
d) sizeof(f())
Exercises Section 4.10
Ex4.32
// 分别用指针和索引遍历数组
constexpr int size = 5;
int ia[size] = {1, 2, 3, 4, 5};
for (int *ptr = ia, ix = 0; ix != size && ptr != ia + size; ++ix, ++ptr) { /*... */ }
Ex4.33
// 如果someValue 为真,那么 x 加 1,y 加 1;反之,x 减 1,y 减 1
someValue ? ++x, ++y : --x, --y;
Exercises Section 4.11.1
Ex4.35
char cval; int ival; unsigned int ui;
float fval; double dval;
a) cval = 'a' + 3; // 结果为 d
b) fval = ui - ival * 1.0; // ival 从 int 转换成 float, ui 从 unsigned int 转换成 float
c) dval = ui * fval; // ui 转换成 float,最后结果再转换成 double
d) cval = ival + fval + dval; // 先是 ival 转换成 float 类型,然后 (ival + fval) 转换成 double 类型,最后结果转换为 char 类型
Exercises Scetion 4.11.3
Ex4.36
int i = 2;
double d = 3.5;
i *= static_cast<int>(d);
Ex4.37
int i; double d; const string *ps; char *pc; void *pv;
a) pv = (void*)ps;
b) i = (int)(*pc); ---> i = static_cast<int>(*pc);
c) pv = &d; ---> pv = static_cast<void*>(&d);
d) pc = (char*) pv; ---> pc = static_cast<char*>(pv);
Ex4.38
// 将变量 j 除以 变量 i 的值转换成 double 类型赋给 变量 slope
double slope = static_cast<double>(j / i);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端