58 C++ Primer Plus(第六版)第十七章 编程练习答案
0 复习题
exercise00.cpp
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
int main ( )
{
cout << "Enter you name: " ;
string name;
getline ( cin, name) ;
cout << "Enter your hourly wages: " ;
float wag;
cin >> wag;
cout << "Enter number of hours worked: " ;
float hour;
cin >> hour;
cout << "First format:\n" ;
cout. setf ( ios_base:: showpoint) ;
cout. setf ( ios_base:: right, ios_base:: adjustfield) ;
cout. setf ( ios_base:: fixed, ios_base:: floatfield) ;
cout << setw ( 20 ) << name << ": $" << setprecision ( 2 ) << setw ( 10 )
<< wag << ":" << setprecision ( 1 ) << setw ( 6 ) << hour << endl;
cout << "Second format:\n" ;
cout. setf ( ios_base:: left, ios_base:: adjustfield) ;
cout. setf ( ios_base:: fixed, ios_base:: floatfield) ;
cout << setw ( 20 ) << name << ": $" << setprecision ( 2 ) << setw ( 10 )
<< wag << ":" << setprecision ( 1 ) << setw ( 6 ) << hour << endl;
return 0 ;
}
1
exercise01.cpp
# include <iostream>
# include <iomanip>
using namespace std;
int main ( )
{
int num = 0 ;
char ch;
cout << "Enter a string:\n" ;
while ( cin. get ( ch) && ch != '$' )
{
num++ ;
}
cout << "Next char is: " << ch << endl;
cout << "The number of char berore '$': " << num << endl;
return 0 ;
}
2
exercise02.cpp
# include <iostream>
# include <fstream>
# include <string>
using namespace std;
int main ( )
{
string filename;
char ch;
cout << "Enter filename:\n" ;
cin >> filename;
cin. get ( ) ;
ofstream ofs ( filename. c_str ( ) ) ;
if ( ofs. is_open ( ) )
{
cout << "Enter information:\n" ;
while ( cin. get ( ch) && ch != EOF )
ofs << ch;
}
else
{
cout << "Can't open the file!\n" ;
exit ( EXIT_FAILURE) ;
}
ofs. close ( ) ;
return 0 ;
}
3
exercise03.cpp
# include <iostream>
# include <string>
# include <fstream>
using namespace std;
const string filename1 = "exercise02" ;
int main ( )
{
string filename2;
cout << "Enter the filename to write:\n" ;
cin >> filename2;
ifstream ifs ( filename1. c_str ( ) , ios:: in) ;
ofstream ofs ( filename2, ios:: out) ;
if ( ! ifs. is_open ( ) )
{
cout << "Can't open file " << filename1 << endl;
exit ( EXIT_FAILURE) ;
}
if ( ! ofs. is_open ( ) )
{
cout << "Can't open file " << filename2 << endl;
exit ( EXIT_FAILURE) ;
}
char ch;
while ( ifs. get ( ch) )
ofs << ch;
ifs. clear ( ) ;
ifs. close ( ) ;
ofs. close ( ) ;
cout << "Done!\n" ;
return 0 ;
}
4
exercise04.cpp
# include <iostream>
# include <string>
# include <fstream>
const std:: string file1 = "exercise041.txt" ;
const std:: string file2 = "exercise042.txt" ;
const std:: string file3 = "exercise043.txt" ;
int main ( )
{
using namespace std;
string st1, st2;
ifstream ifs1 ( file1, ios:: in) ;
ifstream ifs2 ( file2, ios:: in) ;
ofstream ofs ( file3, ios:: out) ;
if ( ! ifs1. is_open ( ) && ! ifs2. is_open ( ) && ofs. is_open ( ) )
{
cout << "Can't open file 1, 2 or 3!\n" ;
exit ( EXIT_FAILURE) ;
}
while ( getline ( ifs1, st1) && getline ( ifs2, st2) && ! ifs1. eof ( ) && ! ifs2. eof ( ) )
ofs << st1 << " " << st2 << endl;
while ( ! ifs1. eof ( ) && getline ( ifs1, st1) )
ofs << st1 << endl;
while ( ! ifs2. eof ( ) && getline ( ifs2, st2) )
ofs << st2 << endl;
cout << "Done!\n" ;
ifs1. close ( ) ;
ifs2. close ( ) ;
ofs. close ( ) ;
return 0 ;
}
5
exercise05.cpp
# include <iostream>
# include <string>
# include <fstream>
# include <vector>
# include <algorithm>
const std:: string file1 = "mat.dat" ;
const std:: string file2 = "pat.dat" ;
const std:: string file3 = "matnpat.dat" ;
int main ( )
{
using namespace std;
ifstream ifs1 ( file1, ios:: in) ;
ifstream ifs2 ( file2, ios:: in) ;
ofstream ofs3 ( file3, ios:: out) ;
ifstream ifs3 ( file3, ios:: in) ;
if ( ! ifs1. is_open ( ) && ! ifs2. is_open ( ) && ! ofs3. is_open ( ) && ! ifs3. is_open ( ) )
{
cout << "Can't open file 1 or 2!\n" ;
exit ( EXIT_FAILURE) ;
}
string st2;
vector< string> mat;
vector< string> pat;
vector< string> matnpat;
cout << "Mat' friend:\n" ;
while ( getline ( ifs1, st2) )
{
mat. push_back ( st2) ;
cout << st2 << endl;
}
cout << "Pat' friend:\n" ;
while ( getline ( ifs2, st2) )
{
pat. push_back ( st2) ;
cout << st2 << endl;
}
set_union ( mat. begin ( ) , mat. end ( ) , pat. begin ( ) , pat. end ( ) ,
insert_iterator< vector< string>> ( matnpat, matnpat. begin ( ) ) ) ;
for ( string st : matnpat)
{
ofs3 << st << endl;
}
cout << "Done!\n" ;
cout << "Mat and Pat' friend:\n" ;
while ( getline ( ifs3, st2) )
{
cout << st2 << endl;
}
ifs1. close ( ) ;
ifs2. close ( ) ;
ofs3. close ( ) ;
return 0 ;
}
6
emp.h
# pragma once
# include <iostream>
# include <string>
# include <fstream>
enum classkind { Employee, Manager, Fink, Highfink } ;
class abstr_emp
{
private :
std:: string fname;
std:: string lname;
std:: string job;
public :
abstr_emp ( ) ;
abstr_emp ( const std:: string& fn, const std:: string& ln,
const std:: string& j) ;
virtual void ShowAll ( ) const ;
virtual void SetAll ( ) ;
virtual void GetAll ( std:: ifstream& ifs) ;
virtual void WriteAll ( std:: ofstream& ofs) ;
friend std:: ostream& operator << ( std:: ostream& os, const abstr_emp& e) ;
virtual ~ abstr_emp ( ) = 0 ;
} ;
class employee : public abstr_emp
{
public :
employee ( ) ;
employee ( const std:: string& fn, const std:: string& ln,
const std:: string& j) ;
virtual void ShowAll ( ) const ;
virtual void SetAll ( ) ;
virtual void GetAll ( std:: ifstream& ifs) ;
virtual void WriteAll ( std:: ofstream& ofs) ;
} ;
class manager : virtual public abstr_emp
{
private :
int inchargeof;
protected :
int InChargeOf ( ) const { return inchargeof; }
int & InChargeOf ( ) { return inchargeof; }
public :
manager ( ) ;
manager ( const std:: string& fn, const std:: string& ln,
const std:: string& j, int ico = 0 ) ;
manager ( const abstr_emp& e, int ico) ;
manager ( const manager& m) ;
virtual void ShowAll ( ) const ;
virtual void SetAll ( ) ;
virtual void GetAll ( std:: ifstream& ifs) ;
virtual void WriteAll ( std:: ofstream& ofs) ;
} ;
class fink : virtual public abstr_emp
{
private :
std:: string reportsto;
protected :
const std:: string ReportsTo ( ) const { return reportsto; }
std:: string& ReportsTo ( ) { return reportsto; }
public :
fink ( ) ;
fink ( const std:: string& fn, const std:: string& ln,
const std:: string& j, const std:: string& rpo)
: abstr_emp ( fn, ln, j) , reportsto ( rpo) { }
fink ( const abstr_emp& e, const std:: string& rpo)
: abstr_emp ( e) , reportsto ( rpo) { }
fink ( const fink& e) ;
virtual void ShowAll ( ) const ;
virtual void SetAll ( ) ;
virtual void GetAll ( std:: ifstream& ifs) ;
virtual void WriteAll ( std:: ofstream& ofs) ;
} ;
class highfink : public manager , public fink
{
public :
highfink ( ) : abstr_emp ( ) , manager ( ) , fink ( ) { }
highfink ( const std:: string& fn, const std:: string& ln,
const std:: string& j, std:: string& rpo,
int ico)
: abstr_emp ( fn, ln, j) , manager ( fn, ln, j, ico) , fink ( fn, ln, j, rpo) { }
highfink ( const abstr_emp& e, const std:: string& rpo, int ico)
: abstr_emp ( e) , manager ( e, ico) , fink ( e, rpo) { }
highfink ( const fink& f, int ico) : abstr_emp ( f) , manager ( f, ico) , fink ( f) { }
highfink ( const manager& m, const std:: string& rpo)
: abstr_emp ( m) , manager ( m) , fink ( m, rpo) { }
highfink ( const highfink& h) : abstr_emp ( h) , manager ( h) , fink ( h) { }
virtual void ShowAll ( ) const ;
virtual void SetAll ( ) ;
virtual void GetAll ( std:: ifstream& ifs) ;
virtual void WriteAll ( std:: ofstream& ofs) ;
} ;
exercise06_emp.cpp
# include <iostream>
# include "emp.h"
# include <fstream>
using namespace std;
abstr_emp:: abstr_emp ( )
{
fname = lname = job = "NULL" ;
}
abstr_emp:: abstr_emp ( const std:: string& fn, const std:: string& ln,
const std:: string& j) : fname ( fn) , lname ( ln) , job ( j)
{
}
void abstr_emp:: ShowAll ( ) const
{
cout << "Name: " << lname << " " << fname << endl;
cout << "Job: " << job << endl;
}
void abstr_emp:: SetAll ( )
{
cout << "Enter firstname:\n" ;
getline ( cin, fname) ;
cout << "Enter lastname:\n" ;
getline ( cin, lname) ;
cout << "Enter job:\n" ;
getline ( cin, job) ;
}
void abstr_emp:: GetAll ( std:: ifstream& ifs)
{
getline ( ifs, lname) ;
getline ( ifs, fname) ;
getline ( ifs, job) ;
}
void abstr_emp:: WriteAll ( std:: ofstream& ofs)
{
ofs << lname << endl << fname << endl << job << endl;
}
std:: ostream& operator << ( std:: ostream& os, const abstr_emp& e)
{
os<< "Name: " << e. lname << " " << e. fname << endl;
os << "Job: " << e. job << endl;
return os;
}
abstr_emp :: ~ abstr_emp ( )
{
}
employee:: employee ( ) : abstr_emp ( )
{
}
employee:: employee ( const std:: string& fn, const std:: string& ln,
const std:: string& j) : abstr_emp ( fn, ln, j)
{
}
void employee:: ShowAll ( ) const
{
cout << "Employee:\n" ;
abstr_emp:: ShowAll ( ) ;
}
void employee:: SetAll ( )
{
cout << "Enter employee information:\n" ;
abstr_emp:: SetAll ( ) ;
}
void employee:: GetAll ( std:: ifstream& ifs)
{
abstr_emp:: GetAll ( ifs) ;
}
void employee:: WriteAll ( std:: ofstream& ofs)
{
ofs << Employee << endl;
abstr_emp:: WriteAll ( ofs) ;
}
manager:: manager ( ) : abstr_emp ( )
{
}
manager:: manager ( const std:: string& fn, const std:: string& ln,
const std:: string& j, int ico) : abstr_emp ( fn, ln, j) , inchargeof ( ico)
{
}
manager:: manager ( const abstr_emp& e, int ico) : abstr_emp ( e) , inchargeof ( ico)
{
}
manager:: manager ( const manager& m) : abstr_emp ( m)
{
inchargeof = m. inchargeof;
}
void manager:: ShowAll ( ) const
{
cout << "Manager:\n" ;
abstr_emp:: ShowAll ( ) ;
cout << "Inchargeof: " << inchargeof << endl;
}
void manager:: SetAll ( )
{
cout << "Enter manager information:\n" ;
abstr_emp:: SetAll ( ) ;
cout << "Enter inchargeof:\n" ;
cin >> inchargeof;
}
void manager:: GetAll ( std:: ifstream& ifs)
{
abstr_emp:: GetAll ( ifs) ;
ifs >> inchargeof;
}
void manager:: WriteAll ( std:: ofstream& ofs)
{
ofs << Manager << endl;
abstr_emp:: WriteAll ( ofs) ;
ofs << inchargeof << endl;
}
fink:: fink ( ) : abstr_emp ( )
{
reportsto = "NULL" ;
}
fink:: fink ( const fink& e) : abstr_emp ( e)
{
reportsto = e. reportsto;
}
void fink:: ShowAll ( ) const
{
cout << "Fink:\n" ;
abstr_emp:: ShowAll ( ) ;
cout << "Reportsto: " << reportsto << endl;
}
void fink:: SetAll ( )
{
cout << "Enter fink information:\n" ;
abstr_emp:: SetAll ( ) ;
cout << "Enter reportsto:\n" ;
getline ( cin, reportsto) ;
}
void fink:: GetAll ( std:: ifstream& ifs)
{
abstr_emp:: GetAll ( ifs) ;
getline ( ifs, reportsto) ;
}
void fink:: WriteAll ( std:: ofstream& ofs)
{
ofs << Fink << endl;
abstr_emp:: WriteAll ( ofs) ;
ofs << reportsto << endl;
}
void highfink:: ShowAll ( ) const
{
cout << "Highfink:\n" ;
abstr_emp:: ShowAll ( ) ;
cout << "Inchargeof: " << InChargeOf ( ) << endl;
cout << "Reportsto: " << ReportsTo ( ) << endl;
}
void highfink:: SetAll ( )
{
cout << "Highfink information:\n" ;
abstr_emp:: SetAll ( ) ;
cout << "Enter inchargeof:\n" ;
cin >> manager:: InChargeOf ( ) ;
cin. get ( ) ;
cout << "Enter reportsto:\n" ;
getline ( cin, fink:: ReportsTo ( ) ) ;
}
void highfink:: GetAll ( std:: ifstream& ifs)
{
abstr_emp:: GetAll ( ifs) ;
ifs >> manager:: InChargeOf ( ) ;
ifs. get ( ) ;
getline ( ifs, fink:: ReportsTo ( ) ) ;
}
void highfink:: WriteAll ( std:: ofstream& ofs)
{
ofs << Highfink << endl;
abstr_emp:: WriteAll ( ofs) ;
ofs << manager:: InChargeOf ( ) << endl
<< fink:: ReportsTo ( ) << endl;
}
exercise06.cpp
# include <iostream>
# include <fstream>
# include <cstdlib>
# include <string>
# include "emp.h"
using namespace std;
const int MAX = 10 ;
const string file = "employee.txt" ;
int main ( )
{
abstr_emp* pc[ MAX] ;
int kind;
int size = 0 ;
char choice;
ifstream ifs ( file, ios:: in) ;
if ( ! ifs. is_open ( ) )
{
cout << "Can't open the file " << file << "!\n" ;
exit ( EXIT_FAILURE) ;
}
cout << "Here are the contents in file " << file << ":\n" ;
while ( ifs >> kind && size < MAX)
{
ifs. get ( ) ;
switch ( kind)
{
case Employee:
pc[ size] = new employee;
break ;
case Manager:
pc[ size] = new manager;
break ;
case Fink:
pc[ size] = new fink;
break ;
case Highfink:
pc[ size] = new highfink;
break ;
default :
cout << "Error!\n" ;
break ;
}
pc[ size] -> GetAll ( ifs) ;
pc[ size] -> ShowAll ( ) ;
size++ ;
}
ifs. close ( ) ;
cout << "-----------------------------\n" ;
size = 0 ;
ofstream ofs ( file, ios:: out | ios:: app) ;
if ( ! ofs. is_open ( ) )
{
cout << "Can't open the file " << file << "!\n" ;
exit ( EXIT_FAILURE) ;
}
cout << "Choice the type of object to create(q to quit):\n"
<< "a.employee \t b.manager\t c.fink \t d.highfink\n" ;
while ( cin >> choice && choice != 'q' && size< MAX)
{
cin. get ( ) ;
switch ( choice)
{
case 'a' :
pc[ size] = new employee;
break ;
case 'b' :
pc[ size] = new manager;
break ;
case 'c' :
pc[ size] = new fink;
break ;
case 'd' :
pc[ size] = new highfink;
break ;
default :
cout << "Error!\n" ;
break ;
}
pc[ size] -> SetAll ( ) ;
cout << "Choice the type of object to create(q to quit):\n"
<< "a.employee \t b.manager\t c.fink \t d.highfink\n" ;
size++ ;
}
for ( int i = 0 ; i < size; i++ )
pc[ i] -> WriteAll ( ofs) ;
ofs. close ( ) ;
cout << "-----------------------------\n" ;
ifs. clear ( ) ;
ifs. open ( file, ios:: in) ;
if ( ! ifs. is_open ( ) )
{
cout << "Can't open the file " << file << "!\n" ;
exit ( EXIT_FAILURE) ;
}
cout << "Here are the contents in file " << file << ":\n" ;
size = 0 ;
kind = 0 ;
while ( ifs >> kind && size < MAX)
{
ifs. get ( ) ;
switch ( kind)
{
case Employee:
pc[ size] = new employee;
break ;
case Manager:
pc[ size] = new manager;
break ;
case Fink:
pc[ size] = new fink;
break ;
case Highfink:
pc[ size] = new highfink;
break ;
default :
cout << "Error!\n" ;
break ;
}
pc[ size] -> GetAll ( ifs) ;
pc[ size] -> ShowAll ( ) ;
size++ ;
}
ifs. close ( ) ;
return 0 ;
}
7
exercise07.cpp
# include <iostream>
# include <vector>
# include <string>
# include <fstream>
# include <algorithm>
const std:: string file = "exercise07.dat" ;
void ShowStr ( const std:: string& st) ;
void GetStrs ( std:: ifstream& ifs, std:: vector< std:: string> & vst) ;
class Store
{
private :
std:: ofstream& ofs;
public :
Store ( std:: ofstream& of) : ofs ( of) { }
void operator ( ) ( const std:: string& st) ;
} ;
int main ( )
{
using namespace std;
vector< string> vostr;
string temp;
cout << "Enter strings (empty line to quit):\n" ;
while ( getline ( cin, temp) && temp[ 0 ] != '\0' )
vostr. push_back ( temp) ;
cout << "Here is your input.\n" ;
for_each ( vostr. begin ( ) , vostr. end ( ) , ShowStr) ;
ofstream fout ( file, ios_base:: out | ios_base:: binary) ;
for_each ( vostr. begin ( ) , vostr. end ( ) , Store ( fout) ) ;
fout. close ( ) ;
vector< string> vistr;
ifstream fin ( file, ios_base:: in | ios_base:: binary) ;
if ( ! fin. is_open ( ) )
{
cerr << "Could not open the file for input.\n" ;
exit ( EXIT_FAILURE) ;
}
GetStrs ( fin, vistr) ;
cout << "\nHere are the strings read from the file:\n" ;
for_each ( vistr. begin ( ) , vistr. end ( ) , ShowStr) ;
return 0 ;
}
void ShowStr ( const std:: string& st)
{
std:: cout << st << std:: endl;
}
void Store :: operator ( ) ( const std:: string& st)
{
size_t len = st. size ( ) ;
ofs. write ( ( char * ) & len, sizeof ( std:: size_t) ) ;
ofs. write ( st. data ( ) , len) ;
}
void GetStrs ( std:: ifstream& ifs, std:: vector< std:: string> & vst)
{
size_t len;
while ( ifs. read ( ( char * ) & len, sizeof ( size_t) ) )
{
char * temp = new char [ len + 1 ] ;
ifs. read ( temp, len) ;
temp[ len] = '\0' ;
vst. push_back ( temp) ;
}
}