"C++primer" V5 the 4th chapter of the expression reading notes exercise answer

Source: Internet
Author: User

4.1

105

4.2

*vec.begin () =* (Vec.begin ())//Call the dot operator first, then dereference

*vec.begin () +1= (*vec.begin ()) +1//first dereference, plus one

4.3 a little?

4.4

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

4.5

(a) -86 (b)-16

(c) 0 (d) 0

4.6

n%2

4.7

Overflow: The calculated result exceeds the range that the data type can represent

2147483647+1

1u-2

...

4.8

Relatively low.

4.9

The first is whether the CP is a null pointer, or if the CP is pointing to an empty string if it is not a null pointer

4.10

    int Val;      while (cin>>val&&val!=);

4.11

int Main () {    int  a,b,c,d;    CIN>>a>>b>>c>>D;     if (a>b&&b>c&&c>D)        cout<<"Yes"< <Endl;     Else         cout<<"No"<<Endl;     return 0 ;}

4.12

Because < priority is higher! =, so the first execution of J<k returns a bool type, possibly 0 or 1, and then the! = operation. The result of a bool operation, which may be 0 or 1, is eventually returned.

4.13

(a) d=3,i=3 because it is executed from right to left

(b) i=3,d=3.5

4.14

The first one will be an error, 42 is a constant, cannot be assigned

The second, the expression returns 42, is true, but may not be the designer's intention

4.15

The pointer data type is incompatible with int and double and cannot be assigned this way.

Correct answer: dval=ival=*pi=0;

4.16

(a)! = will be executed first, then Execute =.

Positive solution: if ((P=getptr ())!=0)

(b) = is an assignment, not a judgement of equality

Positive solution: if (i==1024)

4.17

The predecessor operator, first evaluated and returned to the object for Lvalue operands

A post operator that returns a copy of the object as an rvalue operand, and then evaluates the value

4.18

Using the predecessor operator will not be able to output the first element, and will access the out-of-bounds

In addition, ++*it and *++it are not the same as tested. The front may lead to a dead loop, because only the value is added.

4.19

(a) If PTR is not a null pointer, then determine if PTR is pointing to 0, and then point PTR to the next position

(b) First to determine whether Ival is 0, then add ival to 1, and then determine if Ival is 0.

(c) equivalent to vec[ival]<=vec[ival+1],++ival;

4.20

(a) legal. equivalent to *iter,iter++;

(b) unlawful. The string type does not support the + + operation.

(c) Illegal. The member access operator is executed first, and ITER does not have the empty member, so it will be an error.

(d) correct. Equivalent to (*iter). Empty ()

(e) correct. Equivalent to + + (*iter)

(f) correct. Equivalent to Iter->empty (), iter++;

4.21

intmain () {vector<int>VEC; intVal;  while(cin>>val) Vec.push_back (Val);  for(Auto &I:vec)if(i%2) I=i*2;  for(auto I:vec) cout<<i<<Endl; return 0;}

4.22

intMain () {intVal;  while(cin>>val) {        stringGrade= (val> -)?"High Pass":( -<=val&&val<= the)?"Low pass":(val< -)?"fail":"Pass"; cout<<grade<<Endl; }    return 0;}

Obvious if statement better understand

intMain () {intVal;  while(cin>>val) {        if(val> -) cout<<"High Pass"<<Endl; Else if(val< -) cout<<"fail"<<Endl; Else        {            if( -<=val&&val<= the) cout<<"Low pass"<<Endl; Elsecout<<"Pass"<<Endl; }    }    return 0;}

4.23

Because the addition operation takes precedence over the conditional operator, the addition is performed first, a string is given, and string is compared to the character, which causes an error.

Positive solution: String p=s+ (S[s.size () -1]== ' s '? "": "S");

4.24?

Not very understanding of the Union of the law of this piece. Save it for later study.

4.25

Do not understand what the Latin-1 character set is.

The expression, the first step to get back 10001110

Second move left 6 bits get 10000000

4.26

unsigned int is different than the number of different machines, can not ensure that 30 students must be represented

4.27

ul1=011

ul2=111

(a) 011=3

(b) 111=7

(c) 1

(d) 1

4.28

cout<<"Short :"<<sizeof( Short) <<Endl; cout<<"int:"<<sizeof(int) <<Endl; cout<<"Long:"<<sizeof(Long) <<Endl; cout<<"Long Long:"<<sizeof(Long Long) <<Endl; cout<<"float:"<<sizeof(float) <<Endl; cout<<"Double:"<<sizeof(Double) <<Endl; cout<<"Char:"<<sizeof(Char) <<endl;

This is the output on my machine:

Short:2int:4long:4longlong:8 float:4double:8char:1

4.29

intMain () {intx[Ten]; int*p=x; cout<<sizeof(x) <<" "<<sizeof(*x) <<" "<<sizeof(x)/sizeof(*x) <<Endl; cout<<sizeof(p) <<" "<<sizeof(*p) <<" "<<sizeof(p)/sizeof(*p) <<Endl; return 0;}
sizeof (x) is a space of an int array of size 10
sizeof (*X) is the space for a pointer to int
sizeof (p) is the space for a pointer to int
sizeof (*P) is a space for an INT element
 + 4 Ten 4 4 1

The above is the output

4.30

(a) sizeof (A+B)

(b) sizeof (P->mem[i])

(c) sizeof (A<B) Note that less than the priority is lower than sizeof

(d) sizeof (f ())

4.31

The post will return an extra copy of the object, using the pre-operator more in line with the intended purpose of programming and more efficient

4.32

Iterates through the IA array.

4.33

If the somevalue is true, then ++x,++y and returns the Y from the addition. If False,--x,--y and returns the Y after the decrement.

Note that the comma operator evaluates from left to right, and eventually returns the value of the right-hand expression

4.34

(a) float converted to bool

(b) +:int turn float,=float to double

(c) *:char turn int,+int to double

4.35

(a) char to int, then to char?

(b) *int Double,-unsignedint turn double,=double to float

(c) *unsignedint turn float,float to double

(d) int to float,float turn double,double to Char

To avoid loss of precision, integer and floating-point calculations, the integral type is converted to floating-point type

4.36

i*= (int) D;

4.37

(a) pv=static_cast<void*> (PS);

(b) i=static_cast<int*> (PC);

(c) pv=static_cast<void*> (&D);

(d) pc=static_cast<char*> (PV);

Note that the data type of the strong turn should be expanded with <>, and the variables should be enclosed in ().

4.38

Turn the j/i result into a double assignment to slope

int Main () {    int i=3, j=5;     double slope=static_cast<double> (j/i);    cout<<slope<<Endl;     return 0 ;}

For example, the above results, output 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.