Each row of the original file in the previous article is read into an integer array.
Use Getline (the default carriage return Endl as the row mark) to read each row into a string array, and add/0 to the end of the character number to form a string;
Use the strtok function to divide the strings of each line by spaces and return a char * type value pchtok;
Then, convert pchtok to int type using atoi, and store each unit in an integer array in sortarray [I;
Then, the array is sorted by the heap Sorting Algorithm and then output to another text according to the alignment format. Void heapsort_write.
To use a string function to include string. h, to use SETW to include iomanip. h.
# Include "heapsort. H"
# Include <iostream>
# Include <string>
# Include <fstream>
# Include <iomanip>
Using namespace STD;
# Define arra_size 1000
Int heapcount = 0; // The total number of comparisons recorded.
Void fixheap (int l [], int hsize, int root, int K ){
// Int larger = 0;
If (2 * (root + 1)> hsize) // leaf node
L [root] = K;
Else {
Int larger = 0;
If (2 * (root + 1) = hsize) // only the left subtree
Larger = 2 * root + 1;
Else if (L [2 * root + 1]> L [2 * (root + 1)]) // has left and right subtree
Larger = 2 * root + 1;
Else
Larger = 2 * (root + 1 );
If (k> L [larger])
{
Heapcount ++;
L [root] = K;
}
Else {
Heapcount ++;
L [root] = L [larger];
Fixheap (L, hsize, larger, k );
}
}
Return;
}
Void buildheap (int l [], int N ){
For (INT I = n/2-1; I> = 0; I --) // create a heap from the n/2-1 Node
Fixheap (L, N, I, L [I]);
Return;
}
Void heapsort (int l [], int N ){
Buildheap (L, N );
For (INT I = n-1; I> = 0; I --){
Int temp = L [I];
L [I] = L [0];
Fixheap (L, I, 0, temp );
}
Return;
}
Void heapsort_write (){
Int sortarray [15]; // stores the data read from each row and is used to call the sorting algorithm.
String strline;
Char achline [array_size];
Const char * pchtok;
Ifstream in_file ("original.txt", IOS: In );
Ofstream out_file ("heapresult.txt ");
Int precount = 0; // used to calculate the number of times to be compared for each sort based on the difference from thiscount
Int num = 0; // used to record the sorting result of the nth row
While (in_file.good ()){
Num ++;
// Read a row each time and copy it to achline to form an array
Getline (in_file, strline );
Strline. Copy (achline, strline. Size (), 0 );
Achline [strline. Size ()] = '/0 ';
// Elements in each row are marked with spaces. The elements are converted to int type and saved to the array.
Pchtok = strtok (achline ,"");
Int I = 0;
While (pchtok! = NULL )){
Sortarray [I] = atoi (pchtok );
I ++;
// Cout <atoi (pchtok) <"";
Pchtok = strtok (null ,"");
}
// Upload File
Heapsort (sortarray, 15 );
For (Int J = 0; j <15; J ++ ){
// SETW usage
Out_file <SETW (3) <setiosflags (IOs: Right) <sortarray [J]; // each number in the array is right aligned with a three-character space
}
Int thiscount = heapcount;
Out_file <"no." <SETW (4) <num <"the total number of rows is compared" <SETW (4) <thiscount-precount <"times" <"the sorting result is:" <"";
Precount = thiscount;
Out_file <Endl;
}
// Cout <Endl;
Out_file.close ();
In_file.close ();
}