Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
Solutions
to selected exercises can be found in the electronic document The Thinking
in C++ Volume 2 Annotated Solution Guide, available for a small fee from www.MindView.net.
1. Open a file by creating an ifstream object. Make an ostringstream
object and read the entire contents into the ostringstream using the rdbuf( )
member function. Extract a string copy of the underlying buffer and
capitalize every character in the file using the Standard C toupper( )
macro defined in <cctype>. Write the result out to a new file.
2. Create a program that opens a file (the first argument on the
command line) and searches it for any one of a set of words (the remaining
arguments on the command line). Read the input a line at a time, and write out
the lines (with line numbers) that match to the new file.
3. Write a program that adds a copyright notice to the beginning of
all source-code files indicated by the program s command-line arguments.
4. Use your favorite text-searching program (grep, for
example) to output the names (only) of all the files that contain a particular
pattern. Redirect the output into a file. Write a program that uses the
contents of that file to generate a batch file that invokes your editor on each
of the files found by the search program.
5. We know that setw( ) allows for a minimum of
characters read in, but what if you wanted to read a maximum? Write an effector
that allows the user to specify a maximum number of characters to extract. Have
your effector also work for output, in such a way that output fields are
truncated, if necessary, to stay within width limits.
6. Demonstrate to yourself that if the fail or bad bit is set, and
you subsequently turn on stream exceptions, that the stream will immediately
throw an exception.
7. String streams accommodate easy conversions, but they come with a
price. Write a program that races atoi( ) against the stringstream
conversion system to see the effect of the overhead involved with stringstream.
8. Make a Person struct with fields such as name, age,
address, etc. Make the string fields fixed-size arrays. The social security
number will be the key for each record. Implement the following Database
class:
class DataBase {
public:
// Find where a record is on disk
size_t query(size_t ssn);
// Return the person at rn (record number)
Person retrieve(size_t rn);
// Record a record on disk
void add(const Person& p);
};
Write some Person records to disk (do not keep them all in memory).
When the user requests a record, read it off the disk and return it. The I/O operations
in the DataBase class use read( ) and write( )
to process all Person records.
9. Write an operator<< inserter for the Person
struct that can be used to display records in a format for easy reading. Demonstrate
it by writing data out to a file.
10. Suppose the database for your Person structs was lost but
that you have the file you wrote from the previous exercise. Recreate your
database using this file. Be sure to use error checking.
11. Write size_t(-1) (the largest unsigned int on your
platform) to a text file 1,000,000 times. Repeat, but write to a binary file.
Compare the size of the two files, and see how much room is saved using the
binary format. (You may first want to calculate how much will be saved on your
platform.)
12. Discover the maximum number of digits of precision your
implementation of iostreams will print by repeatedly increasing the value of
the argument to precision( ) when printing a transcendental number
such as sqrt(2.0).
13. Write a program that reads real numbers from a file and prints
their sum, average, minimum, and maximum.
14. Determine the output of the following program before it is
executed:
//: C04:Exercise14.cpp
#include <fstream>
#include <iostream>
#include <sstream>
#include "../require.h"
using namespace std;
#define d(a) cout << #a " ==\t"
<< a << endl;
void tellPointers(fstream& s) {
d(s.tellp());
d(s.tellg());
cout << endl;
}
void tellPointers(stringstream& s) {
d(s.tellp());
d(s.tellg());
cout << endl;
}
int main() {
fstream in("Exercise14.cpp");
assure(in, "Exercise14.cpp");
in.seekg(10);
tellPointers(in);
in.seekp(20);
tellPointers(in);
stringstream memStream("Here is a
sentence.");
memStream.seekg(10);
tellPointers(memStream);
memStream.seekp(5);
tellPointers(memStream);
} ///:~
15. Suppose you are given line-oriented data in a file formatted as
follows:
//: C04:Exercise15.txt
Australia
5E56,7667230284,Langler,Tyson,31.2147,0.00042117361
2B97,7586701,Oneill,Zeke,553.429,0.0074673053156065
4D75,7907252710,Nickerson,Kelly,761.612,0.010276276
9F2,6882945012,Hartenbach,Neil,47.9637,0.0006471644
Austria
480F,7187262472,Oneill,Dee,264.012,0.00356226040013
1B65,4754732628,Haney,Kim,7.33843,0.000099015948475
DA1,1954960784,Pascente,Lester,56.5452,0.0007629529
3F18,1839715659,Elsea,Chelsy,801.901,0.010819887645
Belgium
BDF,5993489554,Oneill,Meredith,283.404,0.0038239127
5AC6,6612945602,Parisienne,Biff,557.74,0.0075254727
6AD,6477082,Pennington,Lizanne,31.0807,0.0004193544
4D0E,7861652688,Sisca,Francis,704.751,0.00950906238
Bahamas
37D8,6837424208,Parisienne,Samson,396.104,0.0053445
5E98,6384069,Willis,Pam,90.4257,0.00122009564059246
1462,1288616408,Stover,Hazal,583.939,0.007878970561
5FF3,8028775718,Stromstedt,Bunk,39.8712,0.000537974
1095,3737212,Stover,Denny,3.05387,0.000041205248883
7428,2019381883,Parisienne,Shane,363.272,0.00490155
///:~
The heading of each section is a region, and every line under that heading
is a seller in that region. Each comma-separated field represents the data
about each seller. The first field in a line is the SELLER_ID which
unfortunately was written out in hexadecimal format. The second is the
PHONE_NUMBER (notice that some are missing area codes). LAST_NAME and
FIRST_NAME then follow. TOTAL_SALES is the second to the last column. The last
column is the decimal amount of the total sales that the seller represents for
the company. You are to format the data on the terminal window so that an
executive can easily interpret the trends. Sample output is given below.
Australia
---------------------------------
*Last Name* *First
Name* *ID* *Phone* *Sales* *Percent*
Langler
Tyson 24150 766-723-0284 31.24 4.21E-02
Oneill
Zeke 11159 XXX-758-6701 553.43 7.47E-01
(etc.)