C ++ reads TXT files marked with space as data differentiation marks, and returns them to an integer Array Using carriage return as the line mark)

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.