In: Computer Science
C++.how to write a selection sort to sort it.
read_book_data()
This member function takes one parameter, a string that contains the name of a file. This string parameter can be a C++ string or a C string (your choice). The function returns nothing.
This constructor should do the following:
Declare and open an input file stream variable using the file name string passed in as a parameter.
Check to make sure the file was opened successfully. If not, print an error message and exit the program.
Read the file into your book_store object using the ifstream::read() member function for binary input.
Close the file stream variable.
Sort the book objects in the array in ascending order by ISBN number using a sorting algorithm of your choice. Note that the ISBNs are C strings, which means that you will not be able to compare them using the standard relational operators. The ISBN is also private data of the book class, so code in the book_store class will need to call get_isbn() for book object rather than accessing the object's ISBN directly.
Note that the code described above will read data into all of the book data members. That includes both the array of 30 book objects, and the number of array elements filled with valid data. No further initialization of the data members will be needed
void book_store::read_book_data(string data)
{
ifstream inFile;
inFile.open(data, ios::binary);
if( inFile.fail() )
{
cout << "input file did not open";
exit(-1);
}
inFile.read((char*)this, sizeof(book_store));
inFile.close();
}
C++.how to write a selection sort to sort it.
SELECTION SORT:
The selection sort algorithm is a sorting technique to sort an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning of the array. The algorithm maintains two subarrays in a given array.
1) Sorted Subarray
2) Unsorted Subarray.
In every iteration of selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarray.
In the above problem , isbn numbers are the character arrays (C-strings).Every Object of the bookstore class contains the isbn number property which is private to the specific class.
To access the isbn number we use the get_isbn() acessor method.
We read the objects of the class from the File inFile using the read() method.
get_isbn() accesor method :
void bookstore::get_isbn(){
return this.isbn()
}
CODE FOR THE SELECTION SORT:
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define MAX_LENGTH 100
// isbn string should be smaller than MAX_LEN
void Sort(char inFile)
{
int i = -1
vector<book_store> arr = new vector<book_store>();
// reading all the objects of book store class into an array
while(!inFile.eof())
arr[++i] = inFile.read((char*)&obj, sizeof(obj));
int n = arr.size()
int start, end, min_index;
char minStr[MAX_LENGTH];
for (start = 0; start < n-1; start++)
{
int min_index = start;
strcpy(minStr, arr[start].get_isbn());
for (end = start + 1; end < n; end++)
{
if (strcmp(minStr, arr[end].get_isbn()) > 0)
{
strcpy(minStr, arr[end].get_isbn());
min_index = end;
}
}
if (min_index != start)
{
char tempChar[MAX_LENGTH];
strcpy(tempChar, arr[start].get_isbn()); //swap item[pos] and item[i]
strcpy(arr[start].get_isbn(), arr[min_index].get_isbn());
strcpy(arr[min_index].get_isbn() , tempChar);
}
}
}
NOTICE THAT INORDER TO COMPARE THE STRINGS WE USED THE strcmp() Standard method present in the
String library.
Tested the code with some sample isbn numbers:
(ISBN IS THE 13-DIGIT STANDARD BOOK NUMBER)