"C + + Programming Thought" (second edition) in the 2nd chapter (note, exercise and answer)

Source: Internet
Author: User
Tags cmath

A. Summarize the contents of this chapter:

1. If the declaration pointer is void*, it means that any type of address can indirectly refer to that pointer (and if int* is declared, the pointer can only be referenced indirectly to the address of the int variable). Once we indirectly refer to a void*, we lose information about the type. This means that you must convert to the correct type before you use it.

The 2.Static variable allows the value of a local variable to persist throughout the lifetime of the program, with the advantage that it is not available outside the scope of the function, so it cannot be easily changed.

3. Internal links mean that only the files that are being compiled are created, and an external connection means that all the compiled files create a separate storage space.

4. Display the conversion syntax:

(1). Static_cast: For "benign" and "moderately benign" conversions, including without coercion (e.g. automatic type conversion);

(2). Const_cast: Conversion of "Const" and/or "volatile";

(3). Reinterpret_cast: Convert to an entirely different meaning. In order to use it safely, the key must be converted back to its original type. The converted type is generally used only for bit operations, otherwise it is for other covert purposes. This is the most dangerous of all conversions.

(4). dynamic_cast: For type-safe downward conversions.

5.asm keyword: is an escape mechanism that allows you to write assembly code in a C + + program. C + + variables can often be referenced in assembler code, which means that it is convenient to communicate with C + + code, and that the qualified assembler code can only be used for the necessary efficient adjustments or to use special processor instructions.

6. When you want to pass command-line arguments to the program, the C and C + + functions main () have a special parameter table in the form of: int main (int argc, char* argv[]);

ARGV[0] is the path and name of the program itself.

7. Complex variables and function definitions:

(1). void * (* (*FP1) (int)) [10];

*FP1 is a pointer to a function that takes an integer parameter and returns a pointer to an array containing 10 void pointers.

(2). Float (* (*FP2) (int,int,float)) (int);

*FP2 is a pointer to a function that receives three arguments (int, int, and float) and returns a pointer to the function that takes an integer parameter and returns a float.

(3). typedef double (* (* (FP3) ()) [10]);

FP3 is a pointer to a function that has no arguments and returns a pointer containing 10 pointers to the array of function pointers that do not accept arguments and return a double value.

(4). Int (* (*F4 ()) [10]);

F4 is a function that returns a pointer to an array containing 10 function pointers that return an integer value.

Two. Functions related to the exercise:

Ifthen.cpp

#include <iostream>using namespace Std;int main () {int i;cout<< "type a number and ' Enter '" <<endl;cin >>i;if (i>5) {cout<< "It ' s greater than 5" <<ENDL;} Else{if (i<5) {cout<< "It ' s less than 5" <<ENDL;} else{cout<< "It ' s equal to 5" <<ENDL;} cout<< "Type a number and ' Enter '" <<endl;cin>>i;if (i<10) {if (i>5) {cout<< "5<i<10" <<endl;} else{cout<< "i<=5" <<endl;}} else{cout<< "i>=10" <<ENDL;} return 0;}


Menu.cpp

#include <iostream>using namespace Std;int main () {char c;while (TRUE) {cout<< "main MENU:" <<endl;cout << "l:left,r:right,q:quit->"; cin>>c;if (c = = ' Q ') {break;} if (c = = ' L ') {cout<< "left menu:" <<endl;cout<< "Select a or B:"; cin>>c;if (c = = ' a ') {cout<< " You choose ' A ' "<<endl;continue;} if (c = = ' B ') {cout<< "you choose ' B '" <<endl;continue;} else{cout<< "You don ' t choose a or B" <<endl;continue;} if (c = = ' R ') {cout<< "right menu:" <<endl;cout<< "select C or D:"; cin>>c;if (c = = ' C ') {cout<< "You choose ' C '" <<endl;continue;} if (c = = ' d ') {cout<< "you choose ' d '" <<endl;continue;} else{cout<< "You don ' t choose c or D" <<endl;continue;} cout<< "You must type L or R or q!" <<endl;} cout<< "quitting menu ..." <<endl;return 0;}


YourPets1.cpp

#include <iostream>using namespace Std;int dog,cat,bird,fish;void f (int pet) {cout<< "Pet ID Number:" < <pet<<endl;} int main () {int i,j,k;}


YourPet2.cpp



Static.cpp

#include <iostream>using namespace std;void func () {static int i = 0;cout<< "i =" <<++I<<ENDL;} int main () {for (int x = 0;x < 10;x++) {func ();} return 0;}


FileStatic.cpp

#include <iostream>using namespace std;static int Fs;int main () {fs = 1;}


FileStatic2.cpp

#include <iostream>using namespace Std;extern int Fs;int main () {fs = 100;}


Boolean.cpp

#include <iostream>using namespace Std;int main () {int i,j;cout<< "Enter an integer:";cin>>i;cout< < "Enter Another integer:";cin>>j;cout<< "I<j is" << (i<j) <<endl;cout<< "i>= J is "<< (i>=j) <<endl;cout<<" I<=j are "<< (i<=j) <<endl;cout<<" I==j is " << (i==j) <<endl;cout<< "I!=j is" << (i!=j) <<endl;cout<< ' i&&j is ' << (i&&j) <<endl;cout<< "i| | J is "<< (i| | j) <<endl;cout<< "(i<10) && (j<10) is" << ((i<10) && (j<10)) <<endl; return 0;}


Bitwise.cpp

#include <iostream>using namespace std; #define PR (str,expr) cout<<str; Printbinary (EXPR); Cout<<endl;int Main () {unsigned int getval;unsigned char a,b;cout<< "Enter a number between 0 and 255:" << Endl;cin>>getval;a = Getval; PR ("A In binary:", a);cout<< "Enter a number between 0 and 255:" <<endl;cin>>getval;b = Getval; PR ("B in binary:", b); PR ("a | b = ", a|b); PR ("A & B =", a&b); PR ("a ^ b =", a^b); PR ("~a =", ~a); PR ("~b =", ~b); unsigned char c = 0x5A; PR ("C in binary:", c); a |= C; PR ("a |= c; a =  ", a); B &= C; PR ("b &= c; b =  ", b); b ^= A; PR ("b ^= A; b =  ", b); return 0;}

Rotation.cpp

#include <iostream>using namespace std;unsigned char rol (unsigned char val) {int highbit;if (val & 0x80) {Highbit = 1;} else{highbit = 0;} Val<<=1;val |= Highbit;return Val;} unsigned char ror (unsigned char val) {int lowbit;if (val & 1) {lowbit = 1;} else{lowbit = 0;} Val>>=1;val |= (lowbit<<7); return Val;}


Union.cpp

#include <iostream>using namespace std;union packed{char i;short j;int k;long l;float f;double D;}; int main () {cout<< "aizeof (Packed) =" <<sizeof (Packed) <<endl; Packed x;x.i = ' C '; cout<<x.i<<endl;x.d = 3.14159;cout<<x.d<<endl;return 0;}


StructArray.cpp

#include <iostream>using namespace Std;typedef struct{int i,j,k;} Threedpoint;int Main () {threedpoint p[10];for (int i = 0;i < 10;i++) {p[i].i = I+1;P[I].J = I+2;P[I].K = i+3;} return 0;}


ArrayAddresses.cpp

#include <iostream>using namespace Std;int main () {int a[10];cout<< "sizeof (int) =" <<sizeof (int) < <endl;for (int i = 0;i < 10;i++) {cout<< "&a[" <<i<< "]=" << (long) &a[i]<<endl ;} return 0;}


ArgsToInts.cpp

#include <iostream> #include <cstdlib>using namespace std;int main (int argc, char* argv[]) {for (int  i = 1; I < argc;i++) {Cout<<atoi (argv[i]) <<endl;}}


PointerIncrement2.cpp

#include <iostream>using namespace Std;typedef struct{char c;short s;int i;long l;float f;double d;long double ld;} Primitives;int Main () {primitives p[10]; primitives* pp = p;cout<< "sizeof (primitives) =" <<sizeof (Primitives) <<endl;cout<< "PP =" < < (long) pp<<endl;pp++;cout<< "PP =" << (long) Pp<<endl;return 0;}


PointerArithmetic.cpp

#include <iostream>using namespace std; #define P (EX) cout<< #EX << ":" <<ex<<endl;int Main () {int a[10];for (int i = 0;i < 10;i++) {A[i] = i;} int* IP = A; P (*IP); P (*++IP); P (* (ip+5)); int* ip2 = ip+5; P (*IP2); P (* (ip2-4)); P (*--IP2); P (IP2-IP); return 0;}


Three. Exercises and Answers


: s03:prototypes.h//  declares various functionsvoid f (int), int g (float), float h (char), char K (void)  ; Same as Char K ()///:~//: S03:Prototypes.cpp {o}//Implements functions declared in Prototypes.h#include <iostream> ; using namespace std;void f (int i) {  cout << f ("<< I <<") returning void\n ";} int g (float x) {  cout << g ("<< x <<") returning int\n ";  return 0;} Float h (char c) {  cout << h ("<< C <<") returning float\n ";  return 1.5;} Char k (void) {   //Same as Char K ()  cout << "K () returning char\n";  Return ' a ';}

#include "Prototypes.h" int main () {  f (1);  g (1.5);  H (' C ');  K ();}




Method One:

#include <iostream> #include <cmath>    //For sqrt () using namespace Std;int main () {  const int MAX = 100;  //Print 2 as a prime:  cout << "2";  for (int i = 3; I <= MAX; i + = 2)   {    float val = i;//produce float value    int mid = static_cast<int> ( sqrt (val));    Int J;    for (j = 3; J <= Mid; J + = 2) {      if (i% j = 0)  {break        ;  }}    if (J > Mid) {      cout << i << ';}  }  cout << Endl;  return 0;}

Method Two:

#include <iostream> #include <cmath>    //For sqrt () using namespace std; #define MAX 100int Main () {  cout<< "2";  for (int i = 3, i < max+1; i+=2)   {for    (int j = 3; J <= sqrt (i); j + = 2) {if (i% J = = 0) {break;}} if (J > sqrt (i)) {cout << i << ';}  }  cout << Endl;  return 0;}



#include <iostream> #include <string> #include <cstdio>int main () {using namespace std;    string Word; for (;;)        {int code;        CIN >> Word;        if (Word = = "Exit" | word = = "return") break;        Map words:if (Word = = "a" | | | word = = "an" | | word = = "the") code = 0;                 else if (Word = = "after" | | word = "before" | | Word = = "Beside" | |                 Word = = "by" | | Word = = "for" | |                 Word = = "from" | | Word = = "in" | |                 Word = = "into" | | Word = = "of" | |        Word = = "to") code = 1;        else if (Word = = "if" | | word = = "Else") code = 2;                 else if (Word = = "who" | | word = = "what" | | Word = = "when" | |                 Word = = "where" | |        Word = = "why") code = 3;        else Code = 4;            Print Code Description:switch (code) {Case 0:puts ("article");      Break  Case 1:puts ("preposition");        Break            Case 2:puts ("conditional");        Break            Case 3:puts ("interrogative");        Break            Default:puts ("unmapped word");        Break  }} return 0;}



#include <iostream>using namespace Std;int main () {char c;while (TRUE) {cout<< "main MENU:" <<endl;cout << "l:left,r:right,q:quit->"; Cin>>c;switch (c) {case ' Q ': {break;} Case ' L ':{cout<< "left menu:" <<endl;cout<< "Select a or B:"; Cin>>c;switch (c) {case ' a ': {cout << "You choose ' A '" <<endl;continue;} Case ' B ':{cout<< "you choose ' B '" <<endl;continue;} default:{cout<< "You don ' t choose a or B" <<endl;continue;}} Case ' R ':{cout<< "right menu:" <<endl;cout<< "select C or D:"; Cin>>c;switch (c) {case ' C ': {cout << "You choose ' C '" <<endl;continue;} Case ' d ':{cout<< "you choose ' d '" <<endl;continue;} default:{cout<< "You don ' t choose c or D" <<endl;continue;}}} cout<< "You must type L or R or q!" <<endl;} cout<< "quitting menu ..." <<endl;return 0;}



#include <iostream>using namespace Std;int main () {int x=1,y=2,z=3;cout<< "A =" <<X+Y-2/2+Z<< endl;cout<< "A =" <<x+ (Y-2)/(2+z) <<endl;return 0;}




The results show that:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C + + Programming Thought" (second edition) in the 2nd chapter (note, exercise and answer)

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.