The C ++ Programming Language Chapter 5 Homework

xiaoxiao2021-03-06  42

// The C Programming Language Job 5.9.1 // Write a declaration: The pointer to the character contains 10 integers, including 10 integer arrays, pointers, pointers to the character string, to characters pointers Pointer, constant integer, constant integer / / pointer to integers constant pointers.

#include

Void main () {char * a = "i love bb !!"; cout << a << endl; int b [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int i = 0; i <10; i ) cout << b [i] << ""; cout << Endl; int * c = b; for (i = 0; i <10; i ) ) cout << * c << ""; cout << endl; char d [] = "i love bb !!"; char * e = d; for (i = 0; * e! = 0; i ) cout << * E ; cout << Endl; char ** f = & a; // and int ** a bit different cout << * f << endl; char *** g = & f; cout << ** g <

#include void main () {// typed char typef unsigned char uc; typef const unsigned char cuc; uc A; a = 'g'; coup << a << endl; cuc B = a; // must initialize COUT << B << endl; // b = 'f'; // error, Const type

// 到 整 整 INT * C = new int (123); cout << * c << endl; // to the pointer of the character of the pointer Char * d = "hahaha"; char ** E = & d; cout <

/ / To the pointer INT ** H = g of the array containing 7 intenses; for (i = 0; i <7; i ) cout << * h << Endl; // Displays pointers in each element Address // can also be used with cout << h [i] << endl; h- = 7; // restore pointer address for (i = 0; i <7; i ) cout << ** h << Endl; / / Display the value of the object referring to the pointer in each element

/ / Contains an array of 8 arrays, each array contains 7 pointers INT ** J [8] = {G, G, G, G, G, G, G, G}; int K = 0; For (i = 0; i <8; i ) {cout << "j [" << i << ":"; for (k = 0; k <7; k ) cout << * j [i] [K] << "; cout << Endl;} // If an array element is an array, that is, the element of this array is the first address of another array, // is a pointer, then this array is also a The pointer, the J is a pointer to the / / pointer to 8 arrays. } // The C Programming Language Job 5.9.4 // Write a function, it swaps two integers (exchange their values). Work with int *. Write // Another exchange function, use int & to work. #include void swap1 (int * a, int * b) {INT T = * a; * a = * b; * b = t;} void swap2 (int & a, int & b) {INT T = a; a = b; b = t;} void main () {INT A = 11, b = 22; swap1 (& a, & b); // pointer must pass the address cout << "A:" << a << ", B:" << b << Endl; swap2 (a, b); // Citing direct transition can be cout << "A:" << a << ", b:" << B << Endl;} // The C Programming Language Job 5.9.6 // After the function f (Char), G (Char &), parameters 'C', 49, 3300, C, UC, SC / / Call them as a parameter, where C is Char, UC is unsigned char, SC is Signed Char. Which tuning // use is legal? Which calls will cause the compiler to cause temporary variables

#include void f (char a) {cout << a << end1;} Void g (char & a) {cout << a << endl;} void h (const char & a) {cout << a << Endl;} void main () {char c = 'd'; unsigned char uc = 'e'; signed char sc = 'f'; int i = 77; f (i); f ('c') ; F (49); f (127); f (3300); // Cause warning, assigning Const Int to char, meaningless F (c); f (uc); f (sc); cout << end1 // g ('c'); // Wrong, can not turn const char to char & // g (49); // fault, can not turn Const Int to Char & // G (3300); // Wrong, Turn Const Int to CHAR & G (C); // OK, you can turn the char to char & // g (i); // fault, can not turn int to Char & // G (UC); // Wrong, Turn unsigned char to Char & // G (SC); // Wrong, you cannot transfer Signed Char to Char & Cout << Endl; H ('c'); h (49); h (3300); // Cause warning , Assigning const Int to char, meaning h (c); h (uc); h (sc); char p = 0; cout << Endl; cout << p << endl; // argument to 'C At 49, 3300, the calling function will introduce the compiler to temporary variables. }//in conclusion:

// CHAR range is -128 to 127, and the implicit conversion is performed, and the compiler will warn, such as 3300.

// 1, with char as a function of the function: // can accept CHAR, INT, const int, signed char, unsigned char, etc. // because the incoming argument is only used in the function body, does not affect the function Actual variables are accepted. // 2, with char & as a function gate: // can only accept CHAR types. // Because the incoming argument is a reference external variable, an external value is indirectly modified, and there are some unsafe // implicit conversions, so the types of CHAR are not accepted. // 3, with the const char & functional number: // can accept CHAR, INT, const int, signed char, unsigned char, etc. // mainly because it is const, so it will not modify the arguments in the function body, so accept. // The C Programming Language Job 5.9.10 // Defines an array of strings, where the string saves the name of the month. Print these strings // passed these strings to a function and printed. #include void printm (char ** a) {for (int i = 0; i <12; i ) cout << a [i] << endl;} void printm1 (char * a) {cout << a << Endl;} void main () {char * month [12]; month [0] = "Jan"; Month [1] = "February"; Month [2] = "March"; MONTH [3] = "April"; Month [4] = "May"; Month [5] = "June"; Month [6] = "July"; Month [7] = "August"; MONTH [8] = "September"; Month [9] = "October"; Month [10] = "November"; Month [11] = "December"; for (int i = 0; i <12; i ) cout << MONTH [I] << endl; cout << endl; printm (month); cout << endl; printm1 (Month [0]);}

/ / Pass the array of strings, the type of pointer CHAR pointer // the C Programming Language job 5.9.11: // From the input read a series of words, use QUIT as the end of the input. These words are printed in the order of reading, / / ​​but the same word should not print twice, and the word is sorted before printing.

// Made with char, one word, annoyance ..... # include bool hehe (char * a, char * b) {while (* a! = 0) IF (* a ! = * B ) Return 0; if (* b! = 0) Return 0; Else Return 1;} Bool Findchar (Char * S [], Char * D, INT N) {for (int i = 0; i 99) Break; CIN >> CXBB [99]; IF (HEHE (CXBB [99], "Quit")) Break; cxbb [i-1] = nchar (cxbb [99]);} i- = 1; Cout << "Circular" << i << "word !!" << endl; // New array, remove recurrent char * CZBB [100]; for (int J = 0, o = 0; j #include using namespace std; void main () {string cxbb [100], temp; int n = 100, len = 0; while (n--) {cin >> Temp; IF ( Temp == "quit") Break; cxbb [len ] = TEMP;

} // Remove the re-generating new array String CZB [100]; CZBB [0] = CXBB [0]; INT K = 1, i = 1, j = 0; for (i = 0; i int charlen (char * a) {INT i = 0; while (* a ! = 0) i ; returni} bool hehe (char * s, char * d) { INT L = Charlen (D); while (l -) IF (* S ! = * D ) Return 0; Return 1;} int Haha (char * s, char * d) {int L = charlen (s); INT j = 0; for (int i = 0; i

// Define a struct date to save the date of the date. Provide some functions, write Date from entering Date, output Date to //, and initialize DATE with a date;

转载请注明原文地址:https://www.9cbs.com/read-54088.html

New Post(0)