C ++ programming thoughts (version 2) Chapter 1 C (notes, exercises, and answers) in C ++ (I) and answers to java programming thoughts
I. Summarize the content of this chapter:
1. if the declared pointer is void *, it means that any type of address can indirectly reference the pointer (and if int * is declared, it can only indirectly reference the pointer to the address of the int type variable ). Once we indirectly reference a void *, information about the type will be lost. This means that the type must be converted to the correct one before use.
2. Static variables make the values of local variables still exist throughout the life cycle of the program. The advantage is that they are unavailable outside the scope of the function, so they cannot be easily changed.
3. The internal link only creates space for the files being compiled. The external connection means creating a separate bucket for all the files that have been compiled.
4. Display conversion Syntax:
(1). static_cast: used for "benign" and "moderate benign" conversions, including non-forced conversions (for example, automatic conversions );
(2). const_cast: converts "const" and/or "volatile;
(3). reinterpret_cast: the conversion is totally different. To use it securely, The key must be converted back to the original type. The converted type can only be used for bitwise operations. This is the most dangerous of all conversions.
(4). dynamic_cast: used for type-safe downward conversion.
5. asm Keyword: it 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 the compilation code can only be used for necessary efficient adjustments, or use special processor commands.
6. when you want to pass a command line parameter to a program, the main () function of C and C ++ has a special parameter table in the form of int main (int argc, char * argv []);
Argv [0] is the path and name of the program.
7. Complex Variables and function definitions:
(1). void * (* fp1) (int) [10];
* Fp1 is a pointer to a function. This function accepts an integer parameter and returns a pointer pointing to an array containing 10 void pointers.
(2). float (* fp2) (int, int, float) (int );
* Fp2 is a pointer to a function. This function receives three parameters (int, int, and float) and returns a pointer to the function, this function accepts an integer parameter and returns a float.
(3). typedef double (* (fp3) () [10]) ();
Fp3 is a pointer to a function. This function has no parameters and returns 10 pointers to the array of function pointers. These functions do not accept parameters and return double values.
(4). int (* f4 () [10]) ();
F4 is a function that returns a pointer pointing to an array containing 10 function pointers. These functions return integer values.
Ii. functions related to exercises:
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
#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;cout<<"f():"<<(long)&f<<endl;cout<<"dog:"<<(long)&dog<<endl;cout<<"cat:"<<(long)&cat<<endl;cout<<"bird:"<<(long)&bird<<endl;cout<<"fish:"<<(long)&fish<<endl;cout<<"i:"<<(long)&i<<endl;cout<<"j:"<<(long)&j<<endl;cout<<"k:"<<(long)&k<<endl;return 0;}
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 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||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;}
Iii. 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 1:
#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 2:
#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;}
#include <iostream>using namespace std;int dog;char cat;float bird;double fish;void f(int pet){cout<<"pet id number:"<<pet<<endl;}int main(){int i,j,k;cout<<"f():"<<(long)&f<<endl;cout<<"dog:"<<(long)&dog<<endl;cout<<"cat:"<<(long)&cat<<endl;cout<<"bird:"<<(long)&bird<<endl;cout<<"fish:"<<(long)&fish<<endl;cout<<"i:"<<(long)&i<<endl;cout<<"j:"<<(long)&j<<endl;cout<<"k:"<<(long)&k<<endl;return 0;}
Result:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.