C++Primer 第五版 习题答案 第四章 表达式

4.1

105

4.2

a) (vec.begin())
b) (
(vec.begin()))+1

4.3

可以接受,c++效率是最大优势,我们要做的就是避免类似代码的产生

4.4

((12/3)*4)+(5*15)+((24%4))/2
91

4.5

a)-86 b)-18
c) 0 d) -2

4.6

i%2==0? “even”:“odd”

4.7

short svalue =32767; ++svalue;
unsigen uvalue =0; -uvalue;
unsign short value =65535; ++usvalue;

4.8

逻辑与和逻辑或都是先求左侧运算对象,只有当左侧无法运算对象无法确定表达式结果时,才会计算右侧运算对象的值
逻辑与:当且仅当左侧运算对象为真时才对右侧运算对象求值;
逻辑或:当且仅当左侧运算对象为假时才对右侧运算对象求值;
相等性运算符:两个运算对象都需要求值, 求值顺序不确定。

4.9

当指针cp不为空时,才判断cp解引用的值

4.10

while(cin>>i&&i!=42)

4.11

a>b&&b>c&&c.d

4.12

<运算符的优先级大于!=,因此等价于 i!=(j<k)

4.13

a) i=3,d=3.0
b) d=3.5,i=3

4.14

非法操作,字面值为右值
if判断为true

4.15

pi改为*pi

4.16

a)!=优先级大于= if((p=getPtr())!=0)
b) 误认为相等运算符 if(i==1024)

4.17

前置版本:将运算对象加1,将对象本身作为左值返回
后置版本:将运算对象加1,但将对象的原始值的副本作为右值返回

4.18

*(++pbeg) 循环第一次解引用第二个地址,最后一次解引用最后一个地址的后一个元素

4.19

a)指针不为空时,判断指针指向的值是否为0
b)判断ival和 ival+1的值是否为0
c)判断vec[ival]<=vec[ival+1]

4.20

a) 合法 等价于*(iter++)求iter的解引用,然后将iter指向下一个位置;
b)不合法 *iter为字符串,不能++;
c)不合法 点运算符优先级高于解引用,iter没有empty();
d)合法,判断iter指向的值是否为空;
e)不合法 字符串不能++;
f)合法,先判断是否为空,再++;

4.21

#include<iostream>
#include<vector>


using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> iv{ 1,2,3,4,5 };
	for (auto& i : iv)
		i=(i%2) ? (i * 2) : i;

	for (auto j : iv)
		cout << j << " ";
	cout << endl;
}

4.22

#include<iostream>
#include<vector>
#include<string>


using std::cout;
using std::endl;
using std::vector;
using std::string;

int main()
{
	int grade=59;
	string finalgrade = (grade > 90) ? "high pass" : (grade > 75) ? "pass" : (grade > 60) ? "low pass" : "fail";
	cout << finalgrade << endl;
	if (grade > 90)
	{
		finalgrade = "high pass";
	}
	else if(grade>75)
	{
		finalgrade = "pass";
	}
	else if(grade>60)
	{
		finalgrade = "low pass";
	}
	else
	{
		finalgrade = "fail";
	}
	cout << finalgrade << endl;
	return 0;

}

4.23

+优先级大于==大于?
所以先执行s+s[s.size()-1]
应改为 string p1 s+(s[s.size()-1]==s?"":“s”)

4.24

finalgrade = (grade > 90) ? “high pass” : (grade < 60) ? “fail” : “pass”;

if grade > 90,finalgrade = “high pass” ? “fail” : “pass”;
finalgrade = “fail”;
grade <= 90时,结果符合预期

4.25

1000 0000

4.26

int只有16位,可能出现位不够的情况

4.27

011 111
a)011 ,3
b)111,7
c)true
d)true

4.28

#include <iostream> // high level input/output operations.

int main()
{
    // by using method below only include what is needed.
    using std::cout;
    using std::endl;

    // void type
    cout << "void: nullptr_t\t" << sizeof(std::nullptr_t) << " bytes" << endl << endl;

    // boolean type
    cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl << endl;

    // charactor type
    cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
    cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
    cout << "char16_t:\t" << sizeof(char16_t) << " bytes" << endl;
    cout << "char32_t:\t" << sizeof(char32_t) << " bytes" << endl << endl;

    // integers type
    cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
    cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
    cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
    cout << "long long:\t" << sizeof(long long) << " bytes" << endl << endl;

    // floating point type
    cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
    cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
    cout << "long double:\t" << sizeof(long double) << " bytes" << endl << endl;

    // Fixed width integers
    cout << "int8_t:\t\t" << sizeof(int8_t) << " bytes" << endl;
    cout << "uint8_t:\t" << sizeof(uint8_t) << " bytes" << endl;
    cout << "int16_t:\t" << sizeof(int16_t) << " bytes" << endl;
    cout << "uint16_t:\t" << sizeof(uint16_t) << " bytes" << endl;
    cout << "int32_t:\t" << sizeof(int32_t) << " bytes" << endl;
    cout << "uint32_t:\t" << sizeof(uint32_t) << " bytes" << endl;
    cout << "int64_t:\t" << sizeof(int64_t) << " bytes" << endl;
    cout << "uint64_t:\t" << sizeof(uint64_t) << " bytes" << endl;

    return 0;
}

4.29

40/4=10,数组所占的字节数/数组类型int所占的字节数,就是数组的个数;
4/4=1,指针所占的字节数/int所占的字节数。

4.30

(a)(sizeof x) + y;
(b)sizeof(p->mem[i]);
(c)(sizeof a) < b;
(d)sizeof(f())。

4.31

不需要保存修改前的值
无改动

4.32

遍历数组的下标实现和指针实现

4.33

(someValue ? ++x, ++y : --x), --y

4.34

a)float转换为bool
b)ival转换为float,结果转换为double
c)cval转换为int,结果转换为double
右结合律

4.35

a)‘a’转为为int,int转换为char
b)ival转换为double,ui转换为double,最后double转换为float
c)ui转换为float,float转换为double
d)ival转换为float,float转换为double,double转换为char

4.36

i*=static (d);

4.37

(a)pv= static_cast<void*>(const_cast<string*>(ps))
(b)i = static_cast<int*>(pc);
(c)pv = static_cast<void*>(&d);
(d)pc = reinterpret_cast<void*>(pv);

4.38

将(j/i)转换为double,并赋值给slope。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值