The C ++ Programming Language Chapter 6 Homework

xiaoxiao2021-03-06  18

THE C Programming Language Chapter 6 James Chen / 200503 6.6.1 Write the following for cycle to use the WHILE loop equivalent form for (i = 0; i m // (int *) P) -> M * pm //(*P ).m * a [i] // (* a) [i] 6.6.10 Write the following function: strlen (), returns the length of the C string; strcpy (), copied a C string to another; strcmp (), compares two C strings.

#include using namespace std; int Strlen (char * d) {int L = 0; while (* D ! = 0) L ; returnit} void strcpy (char * d, char * s) {while * D = * S );} Bool strcmp (char * d, char * s) {while (* d) if (* D ! = * S ) Return 0; return 1;} void main () {char * a = "i love you !!"; cout << strlen (a) << endl; char * b = new char; strcpy (b, a); cout << b << endl; cout << strcmp (a, b) << Endl; char * c = "sdfsf"; cout << strcmp (a, c) << end1;} 6.6.13 Write a CAT (), it takes two C strings, returns a string, the word Strings are stitching of two strings. Use New to get storage for this result. #include using namespace std; char * cat (const char * a, const char * b) {char * TEMP = new char; char * t = temp; while (* t = * a ); t -; / / Return to the position of the C-string A tail while (* t = * b ); return temp;} void main () {char * a = "abcde"; char * b = "12345"; char * c = CAT (a, b); cout << c << endl;} 6.6.14 Write a REV (), which takes the C string as a parameter and reverses the characters. #include using namespace std; // Here, two pointers points to the head tail, personal feelings, compare Int strull (char * a) {int L = 0; while (* a ! = 0) L ; return L;} void Rev (char * a) {char t; int L = strlen (a); char * p = a l-1; for (int i = 1; i <= L / 2) i ) {t = * a; * a = * p; * p - = t;}} void main () {char A [] = "1234sfdeefd98765"; REV (a); cout << a << endl } 6.6.16 Write a Function ATOI (const char *), which is parameter, return to the corresponding int value, and can handle the decimal and hexadecimal. Halo, it is not very much in const. .

#include using namespace std; // decimal, octal, hexadecimal string turn 10 // atoi (char *): Convert different credit format strings to int // strlen (char *): calculate C style string length // getype (char *): Check string in compliance with what kind of credit format, return type // 1: decimal 2: octal 3: hexadecimal 0: Format does not // ASCII: // AF : 65-70 // 0-9: 48-57 // x: 88 int Strlen (char * p) {int L = 0; while (* p ! = 0) L ; returnit (} int getype (char * p) {char * a = p; int T = 0; // First, determine if it contains illegal characters, then return 0 while (* a! = 0) {t = int (* a ); if (! (T> 47 && T <58) || (T == 88))) Return 0;} a = p; // Pointer Reset // Second, determine whether it is decimal (non-0 Head), then returns 1, illegally return 0 if (* p! = 0 ') {while (* a! = 0) {t = int (* a ); if (t <48 || t> 57) Return 0;} Return 1;} else // Third, determined whether it is an eight-binary (0 head, second character is not 0), is returned 2, illegally returns 0 {IF (* (p 1)! = ' X ') {if (* (p 1) ==' 0 ') Return 0; // Octa-Binary second character is zero as illegal While (* a! = 0) {t = int (* a ); if (T <48 || T> 55) RETURN 0;} Return 2;} Else / / Otherwise, hexadecimal. Return 3;}} int atoi (char * p) {int L = strlen (p); int CT = getype (p); int TEMP = 0, o = 1; p = L-1; // Point to the last digit INT T = 0; switch (CT) {case 1: // calculate the decimal While (L - ) {TEMP = (int (* p -) - 48) * O; o * = 10;} Break; case 2: // calculate eight-way while ((L -) - 1) {TEMP = (int (* P -) - 48) * O; o * = 8;} BRE AK; Case 3: // Calculate hex to WHILE ((L -) - 2) {t = int (* p -); if (t> 57) T- = 55; // Reduction Abcdef to 10, 11, 12, 13, 14, 15 else t- = 48; Temp = T *O; o * = 16;} Break; default: temp = 0;} return temp;} void main () {char * a = "0x4f5d"; // hexadecimal char * b = "0231"; // octal char * c = "2314"; // decimal char * D = "0x2314"; // incorrect hexadecimal Char * e = "002314"; // incorrect octal char * f = "2314f"; // incorrect decimal cout << ATOI (a) <

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

New Post(0)