C ++ Programming Practice Guidance 1.14 Implementation of string cross insertion rewrite requirements, Programming Practice 1.14
# Include <cstdlib>
# Include <iostream>
# Include <cstring>
Using namespace std;
Class STRING
{
Char * str1;
Char * str2;
Public:
STRING (char * s1, char * s2)
{
Str1 = new char [strlen (s1)];
Str2 = new char [strlen (s2)];
Strcpy (str1, s1 );
Strcpy (str2, s2 );
}
Void InsertInOrder ();
Void process ();
Void print ()
{
Cout <str1 <endl;
}
};
Void STRING: process ()
{
Int len1, len2, count = 1;
Char str [80];
Int I, j = 0, k = 0;
Len1 = strlen (str1 );
Len2 = strlen (str2 );
Int n = len2/len1;
For (I = 0; I <len1 + len2; I ++)
{
If (count % (n + 1) = 0)
{
Str [I] = str1 [j ++];
Count ++;
} Else {
Str [I] = str2 [k ++];
Count ++;
}
}
Str [I] = '\ 0 ';
Cout <str <endl;
Strcpy (str1, str );
}
Void STRING: InsertInOrder ()
{
Char temp;
Int I, j, k;
Int len1 = strlen (str1 );
Int len2 = strlen (str2 );
For (I = 0; I <len2-1; I ++)
{
K = I;
For (j = I + 1; j <len2; j ++)
If (str2 [k] <str2 [j])
{
K = j;
Temp = str2 [I];
Str2 [I] = str2 [k];
Str2 [k] = temp;
}
}
Cout <str2 <endl;
For (I = 0; I <len1; I ++)
{
For (j = len2-1 + I; j> = 0; j --)
{
If (str1 [I]> str2 [j])
Str2 [j + 1] = str2 [j];
Else
Break;
}
Str2 [j + 1] = str1 [I];
}
Cout <str2 <endl;
}
Int main (int argc, char * argv [])
{
STRING test ("abcd", "ABCDEFG ");
// Test. InsertInOrder ();
Test. process ();
Test. print ();
System ("PAUSE ");
Return EXIT_SUCCESS;
}
Rewrite requirement 2 and 3 should be run separately, that is, comment out and rewrite InsertInOrder () to test rewrite requirement 2. Otherwise, an error occurs. This is because it is directly modified on str1 and str2.