input/output, operator overloading and dynamic memory management:
string fname, lname;
First, implement the default constructor (which initializes fname and lname to empty strings and IDnum to -1) and the constructor
record(string f,string l,int n)
(which initializes fname to f, etc.).
Next, overload the stream insertion and stream extraction operators . the format for a record is:
( fname lname IDnum )
where there can be arbitrary amounts of white space betwen each field and between the fields and the parentheses (recall that white space is a space, tab, newline, etc.). When reading in a record, you must check to see if the input is properly formatted. If it is not, then you should store -1 in IDnum. The IDnum field is what will be used to specify if a record is invalid. Since the user cannot directly access this field, you must define a member function called valid() that returns boolean type true if it is a valid record and false otherwise.
Finally, define these functions:
int readdb(record *& r, istream &s)
void printdb(record *r,int c,ostream &s)
Function readdb reads records one at a time from istream s using the overloaded stream extraction operator until the end of the input and writes them into an array pointed to by r (the array is to be allocated by readdb). Only valid records are to be read into the array! So e.g. if the following input is given on s:
( Stephen Scott 12345 )
( Leen-Kiat Soh )
( Richard Sincovec 6789 )
Ashok Samal 9914 )
then the first and third records will be stored in the array, but not the second or fourth. Since you don't know a priori how many valid records will be read in, start by pointing r to an array of size 2. After reading 2 records, double its size to 4. If you fill that, then double the size again, and so on. I.e., the size of the final array will be at most twice the number of records. Note that since r is a reference to a pointer to type record, you can point r to new arrays as needed. You should take advantage of this when resizing, which can be done by allocating a new array of twice the size and deleting the old one, or via the C command realloc.
The integer returned by readdb is the number of valid records read, not counting the number of invalid records. It is also not necessarily the final size of the array.
Function printdb takes as input a pointer r to an array of records, an integer c of how many records there are in the array (e.g. the value returned by readdb), and an ostream s to write the records to. Use the overloaded stream insertion operator to write all c records to s.
Đã trao cho:
17 freelancer đang chào giá trung bình $39 cho công việc này
I'm a computer scientist from Poland. I've been working as software engineer for the biggest polish software company. The request will be realised quick and professional.