Standard C program library - Question Example - Type String

xiaoxiao2021-03-06  20

Islower - The content of the quotes is lowercase letters, and the contents of the True ISDIGIT - the quotes are numbers, then the contents of the True Isalphabetic - the quotes are case-write letters, and the contents of the True Isaplhanumeric - the contents of the quotes are uppercase letters or numbers. When you return to TRUE

A1: must be introduced

Bool islower (String & Text) {for (unsigned INT i = 0; i {IF ((Text [i] <'a') || (TEXT [I]> 'Z') Return False;} Return True;}

Bool isdigit (String & Text) {for (unsigned int i = 0; i {= ((Text [i] <'0') || (Text [i]> '9')) Return False;} Return True;}

Bool isaplhabetic (String & Text) {for (unsigned int i = 0; i {= ((Text [i] <'a') || (Text [i]> 'Z') && ((Text [i] <'a') || (Text [i]> 'Z')))) Return False;} Return True;

Bool isalphanumeric (String & Text) {for (unsigned INT i = 0; i {= ((Text [i] <'a') || (Text [i]> 'Z') && ((Text [i] <'A') || (Text [I]> 'Z') && ((Text [i] <'0') || (Text [i]> '9'))) Return False;} Return True }

-------------------------------------------- Q2: Write a one It can be judged whether the two strings are parallel words (the length is the same, but the order is different).

A2: must be introduced,

BOOL Anagram (STRING S1, STRING S2) {sort (s1.begin (), s1.end ()); // Sort the two strings and compare the same sort (s2.begin (), s2.end () ); If (S1 == S2) Return true; Return False;}

-------------------------------------------- Q3: From the input Enter a text file and calculate the number of single characters and the length of each word. A3: must be introduced, void main () {

String filename, buffer, word; // declared variable fstream file; // buffer is the data mematic Int Words = 0, allchar = 0; // Word is permitted English alphabet int Start, end;

// The following is a file read program, this program has no error handling COUT << "Please enter the file name:"; cin >> filename;

File.open (filename.c_str ()); while (! file.eof ()) // reads the data ring {char ch; file.get (ch); buffer = ch;} file.close ();

// legal letters fill in for (int i = 0; i <26; i ) {Word = ('a' i); Word = ('A' i);}

// The following is an alphabetical analysis int Len = buffer.Length (); start = buffer.find_first_of (word, 0); // get the first legal alphabet location while (start> = 0 && start ​​len) end = len; // Check the obtained value correct allchar = (end- START); // Plus the total number of words Words ; // single-word number plus a start = buffer.find_first_of (word, end ); // Double Average = Double after the next legal alphanumeric position} // Double Average = Double (Double) Allchar) / Words; // Type conversion COUT << "Words =" << Words << Endl; // Printing cout << "Average =" << Average << Endl;} ------- ------------------------------------- Q4: With inheritance method, overload "*" operation Sub, make it function - the word string N times. For example, "THIS" * 3 is "ThisthisThis". A4: must be introduced

Class nstring: public string {public:

NSTRING OPERATOR * (Unsigned Int);

NSTRING NSTRING :: Operator * (unsigned int n) {nstring temp; while (n! = 0) {temp = c_STR (); n-;} return temp;} ----------- --------------------------------- Q5: With inheritance method, overload "-" operator, make it The function is - delete a particular string. If a strong string, delete the first string.

A5: The class nstring: public string {public: nstring operator - (nstring);

NString NSTRING :: Operator - (nstring str) {nstring Temp; unsigned int pos1 = string :: find (str); unsigned int len1 = str.length (); temp.assign (c_str ()); if (POS1> 0 && POS1 <= Length ()) TEMP.ERASE (POS1, LEN1); Return Temp;} ----------------------------- --------------- Q6: With inheritance method, overload the "int" type conversion operation, so that it can convert the first number in the string. A6: You must introduce Class nstring: public string {public: operator int ();};

NSTRING :: Operator int () {string Num ("0123456789"), TEMP; // All legitimate digital INT VAL = 0;

/ / Get the position of the first number unsigned int start = find_first_of (num, 0); unsigned int end = find_first_not_of (num); temp = substr (start, (end-start);

// Convert word string into an integer for (unsigned INT i = 0; i {val = (TEMP [i] - '0'); // utilizes a circle to get the number Val * = 10; //// After getting, the number of positions will be moved to ten digits} // Except for 10 digits to pull back if (AT (START-1) == '-') return -val / 10; // Number Whether the location is a negative RETURN VAL / 10;}

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

New Post(0)