C++ primer plus中文编程练习答案第17章.docx
C+ primer plus中文编程练习答案第17章1、 /count.cpp #include <iostream> using namespace std; int main charch; int count = 0; cout<< "Enter a string($ to quit):n" while (cin.get(ch) if (ch != '$') count+; cout<<ch; else cin.putback(ch); break; cout<< "Count is " << count <<endl; system("pause"); return 0; 2、 /filein.cpp #include <iostream> #include <fstream> #include <cstdlib> int main(intargc, char *argv) using namespace std; if (argc = 1) cerr<< "Usage: " <<argv0 << " filenamesn" exit(EXIT_FAILURE); ofstreamfout(argv1, ios:out|ios:app); if (!fout.is_open) cerr<< "Can't open " <<argv1 << " file for output.n" exit(EXIT_FAILURE); charch; cout<< "Enter a string: n" while (cin.get(ch) if (ch != 'n') fout<<ch; else break; fout<<endl; fout.close; system("pause"); return 0; 3、 /copy.cpp #include <iostream> #include <fstream> #include <cstdlib> int main(intargc, char *argv) using namespace std; if (argc = 1) cerr<< "Usage: " <<argv0 << " filenamesn" exit(EXIT_FAILURE); ifstream fin; charch; fin.open(argv1, ios:in); if (!fin.is_open) cerr<< "Could not open " <<argv1 <<endl; exit(EXIT_FAILURE); ofstreamfout(argv2, ios:out | ios:trunc); if (!fout.is_open) cerr<< "Could not open " <<argv1 <<endl; exit(EXIT_FAILURE); cout<< "Here are the current contents of th " <<argv1 << " file:n" while (fin.get(ch) cout<<ch; fout<<ch; fin.close; fout.close; system("pause"); return 0; /a.txt Abcdefghijklmnopqrstuvwxyz 4、 /copy_cat.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; const char *file1 = "a.txt" const char *file2 = "b.txt" const char *file3 = "c.txt" int main ifstream fin1, fin2; fin1.open(file1); fin2.open(file2); ofstreamfout; fout.open(file3, ios_base:out | ios_base:trunc); string s1, s2; if (!fin1.is_open | !fin2.is_open) cerr<< "Can't open " << file1 << " or " << file2 <<endl; exit(EXIT_FAILURE); if (!fout.is_open) cerr<< "Can't open " << file3 <<endl; exit(EXIT_FAILURE); while (!fin1.eof && !fin2.eof) getline(fin1, s1); cout<< "File1 " << s1 <<endl; getline(fin2, s2); cout<< "File2 " << s2 <<endl; fout<< s1 << " " << s2 <<endl; while (!fin1.eof) getline(fin1, s1); cout<< "More file1 " << s1 <<endl; fout<< s1 <<endl; while (!fin2.eof) getline(fin2, s2); cout<< "More file2 " << s2 <<endl; fout<< s2 <<endl; fin1.close; fin2.close; fout.close; system("pause"); return 0; /a.txt eggs kites donuts balloons hammers stones /b.txt zero lassitude finance drama 5、 /name.cpp #include <iostream> #include <string> #include <set> #include <algorithm> #include <iterator> #include <fstream> const char *file1 = "mat.dat" const char *file2 = "pat.dat" const char *file3 = "matnpat.dat" int main using namespace std; string fname1; set<string> A; ifstreamfinA, finB; finA.open(file1); finB.open(file2); ofstreamfout; ostream_iterator<string, char>out1(cout, " "), out2(fout, "n"); fout.open(file3, ios_base:out | ios_base:trunc); if (!finA.is_open | !finB.is_open) cerr<< "Can't open " << file1 << " or " << file2 <<endl; exit(EXIT_FAILURE); if (!fout.is_open) cerr<< "Can't open " << file3 <<endl; exit(EXIT_FAILURE); while (!finA.eof) getline(finA, fname1); A.insert(fname1); cout<< "Mat's friends: n" copy(A.begin, A.end, out1); cout<<endl; string fname2; set<string> B; while (!finB.eof) getline(finB, fname2); B.insert(fname2); cout<< "Pat's friends: n" copy(B.begin, B.end, out1); cout<<endl; cout<< "Union of Mat and Pat's friends: " set_union(A.begin, A.end, B.begin, B.end, out1); set_union(A.begin, A.end, B.begin, B.end, out2); cout<<endl; finA.close; finB.close; fout.close; system("pause"); return 0; 6、 /emp.h #ifndef EMP_H_ #define EMP_H_ #include <iostream> #include <string> #include <fstream> using namespace std; enumclasstype Employee, Manager, Fink, Highfink ; classabstr_emp private: stringfname; stringlname; string job; public: abstr_emp; abstr_emp(const string &fn, const string &ln, const string &j); virtual void ShowAllconst; virtual void SetAll; friendostream&operator<<(ostream&os, constabstr_emp&e); virtual abstr_emp = 0; virtual void writeall(ofstream&ofs); virtual void getall(ifstream&ifs); ; class employee :public abstr_emp public: employee; employee(const string &fn, const string &ln, const string &j); virtual void ShowAllconst; virtual void SetAll; virtual void writeall(ofstream&ofs); virtual void getall(ifstream&ifs); ; class manager :virtual public abstr_emp private: intinchargeof; protected: intInChargeOfconst return inchargeof; int&InChargeOf return inchargeof; public: manager; manager(const string &fn, const string &ln, const string &j, intico = 0); manager(constabstr_emp&e, intico = 0); manager(const manager &m); virtual void ShowAllconst; virtual void SetAll; virtual void writeall(ofstream&ofs); virtual void getall(ifstream&ifs); voidgetInCharge cout<< "Enter inchargeof: " cin>>inchargeof; voidwriteInCharge(ofstream&ofs) ofs<<inchargeof<<endl; voidreadInCharge(ifstream&ifs) ifs>>inchargeof; ; class fink :virtual public abstr_emp private: stringreportsto; protected: const string ReportsToconst return reportsto; string&ReportsTo return reportsto; public: fink; fink(const string &fn, const string &ln, const string &j, const string &rpo); fink(constabstr_emp&e, const string &rpo); fink(const fink &e); virtual void ShowAllconst; virtual void SetAll; virtual void writeall(ofstream&ofs); virtual void getall(ifstream&ifs); voidgetReportsTo cout<< "Enter reportsto: " cin>>reportsto; voidwriteReportsTo(ofstream&ofs) ofs<<reportsto<<endl; voidreadReportsTo(ifstream&ifs) ifs>>reportsto; ; classhighfink :public manager, public fink public: highfink; highfink(const string &fn, const string &ln, const string &j, const string &rpo, intico = 0); highfink(constabstr_emp&e, const string &rpo, intico = 0); highfink(const fink &f, intico = 0); highfink(const manager &m, const string &rpo); highfink(consthighfink&h); virtual void ShowAllconst; virtual void SetAll; virtual void writeall(ofstream&ofs); virtual void getall(ifstream&ifs); ; #endif /emp.cpp #include "emp.h" abstr_emp:abstr_emp :fname("no one"), lname("no one"), job("no job") abstr_emp:abstr_emp(const string &fn, const string &ln, const string &j) : fname(fn), lname(ln), job(j) voidabstr_emp:ShowAllconst cout<< "Firstname: " <<fname<<endl; cout<< "Lastname: " <<lname<<endl; cout<< "Job is: " << job <<endl; voidabstr_emp:SetAll cout<< "Enter firstname: " getline(cin, fname); cout<< "Enter lastname: " getline(cin, lname); cout<< "Enter position: " getline(cin, job); ostream&operator<<(ostream&os, constabstr_emp&e) os<<e.fname<< " " <<e.lname<< ", " <<e.job<<endl; returnos; abstr_emp:abstr_emp voidabstr_emp:writeall(ofstream&ofs) ofs<<fname<< "n" <<lname<< "n" << job << "n" voidabstr_emp:getall(ifstream&ifs) getline(ifs, fname); getline(ifs, lname); getline(ifs, job); employee:employee :abstr_emp employee:employee(const string &fn, const string &ln, const string &j) : abstr_emp(fn, ln, j) void employee:ShowAllconst abstr_emp:ShowAll; void employee:SetAll abstr_emp:SetAll; void employee:writeall(ofstream&ofs) ofs<< Employee <<endl; abstr_emp:writeall(ofs); void employee:getall(ifstream&ifs) abstr_emp:getall(ifs); manager:manager :abstr_emp manager:manager(const string &fn, const string &ln, const string &j, intico) : abstr_emp(fn, ln, j), inchargeof(ico) manager:manager(constabstr_emp&e, intico) : abstr_emp(e), inchargeof(ico) manager:manager(const manager &m) : abstr_emp(m) void manager:ShowAllconst abstr_emp:ShowAll; cout<< "Inchargeof: " <<InChargeOf <<endl; void manager:SetAll abstr_emp:SetAll; cout<< "Enter inchargeof: " (cin>>inchargeof).get; void manager:writeall(ofstream&ofs) ofs<< Manager <<endl; abstr_emp:writeall(ofs); ofs<<inchargeof<<endl; void manager:getall(ifstream&ifs) abstr_emp:getall(ifs); ifs>>inchargeof; fink:fink :abstr_emp fink:fink(const string &fn, const string &ln, const string &j, const string &rpo) : abstr_emp(fn, ln, j), reportsto(rpo) fink:fink(constabstr_emp&e, const string &rpo) : abstr_emp(e), reportsto(rpo) fink:fink(const fink &e) : abstr_emp(e) void fink:ShowAllconst abstr_emp:ShowAll; cout<< "Reportsto: " <<ReportsTo <<endl; void fink:SetAll abstr_emp:SetAll; cout<< "Enter reportsto: " cin>>reportsto; void fink:writeall(ofstream&ofs) ofs<< Fink <<endl; abstr_emp:writeall(ofs); ofs<<reportsto<<endl; void fink:getall(ifstream&ifs) abstr_emp:getall(ifs); ifs>>reportsto; highfink:highfink :abstr_emp, manager, fink highfink:highfink(const string &fn, const string &ln, const string &j, const string &rpo, intico) : abstr_emp(fn, ln, j), manager(fn, ln, j, ico), fink(fn, ln, j, rpo) highfink:highfink(constabstr_emp&e, const string &rpo, intico) : abstr_emp(e), manager(e, ico), fink(e, rpo) highfink:highfink(const fink &f, intico) : abstr_emp(f), manager(f, ico), fink(f) highfink:highfink(const manager &m, const string &rpo) : abstr_emp(m), manager(m), fink(m, rpo) highfink:highfink(consthighfink&h) : abstr_emp(h), manager(h), fink(h) voidhighfink:ShowAllconst abstr_emp:ShowAll; cout<< "InChargeOf: " cout<< manager:InChargeOf <<endl; cout<< "ReportsTo: " cout<< fink:ReportsTo <<endl; voidhighfink:SetAll abstr_emp:SetAll; manager:getInCharge; fink:getReportsTo; voidhighfink:writeall(ofstream&ofs) ofs<<Highfink<<endl; abstr_emp:writeall(ofs); manager:writeInCharge(ofs); fink:writeReportsTo(ofs); voidhighfink:getall(ifstream&ifs) abstr_emp:getall(ifs); manager:readInCharge(ifs); fink:readReportsTo(ifs); /useemp.cpp #include "emp.h" constint MAX = 10; const char *file = "a.txt" int main(void) abstr_emp *pcMAX; charch; ifstream fin; fin.open(file); inti = 0; if (fin.is_open) cout<< "Here are the current contents of the " << file <<" file:n" intclasstype; i = 0; while (fin >>classtype).get(ch) switch (classtype) case Employee: pci = new employee; break; case Manager: pci = new manager; break; case Fink: pci = new fink; break; caseHighfink: pci = new highfink; break; cout<<classtype<<endl; pci->getall(fin); pci+->ShowAll; fin.close; ofstreamfout; fout.open(file, ios_base:out | ios_base:app); if (!fout.is_open) cerr<< "Can't open " << file << " file for output.n" exit(EXIT_FAILURE); cout<< "e to employee, m to managern" << "f to fink, h to highfinkn" << "q to quit: " int index = 0; while (cin.get(ch).get &&ch != 'q'&&index < MAX) switch (ch) case'e': pcindex = new employee; pcindex->SetAll; index+; break; case'm': pcindex = new manager; pcindex->SetAll; index+; break; case'f': pcindex = new fink; pcindex->SetAll; index+; break; case'h': pcindex = new highfink; pcindex->SetAll; index+; break; default: cout<< "Try again" break; if (index > MAX) break; cout<< "e to employee, m to managern" << "f to fink, h to highfinkn" << "q to quit: " for (i = 0; i< index; i+) pci->writeall(fout); fout.close; fin.clear; fin.open(file); if (fin.is_open) cout<< "Here are the current contents of the " << file <<" file:n" intclasstype; i = 0; while (fin >>classtype).get(ch) switch (classtype) case Employee: pci = new employee; break; case Manager: pci = new manager; break; case Fink: pci = new fink; break; caseHighfink: pci = new highfink; break; cout<<classtype<<endl; pci->getall(fin); pci+->ShowAll; cout<< "Done.n" system("pause"); return 0; 7、 /vcopy.cpp #include <iostream> #include <vector> #include <string> #include <iterator> #include <algorithm> #include <fstream> #include <cstdlib> using namespace std; voidShowStr(const string &); voidGetStrs(ifstream&, vector<string>&); class Store ofstream&fout; public: Store(ofstream&os) :fout(os) void operator(const string &str); ; void Store:operator(const string &str) size_tlen = str.size; fout.write(char *)&len, sizeof(size_t); fout.write(str.data, len); int main vector<string>vostr; string temp; cout<< "Enter strings (empty line to quit):n" while (getline(cin, temp) && temp0 != '0') vostr.push_back(temp); cout<< "Here is your input.n" for_each(vostr.begin, vostr.end, ShowStr); ofstreamfout("strings.dat", ios_base:out | ios_base:binary); for_each(vostr.begin, vostr.end, Store(fout); fout.close; vector<string>vistr; ifstream fin("strings.dat", ios_base:in | ios_base:binary); if (!fin.is_open) cerr<< "Could not open 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); system("pause"); return 0; voidShowStr(const string &str) cout<<str<<endl; voidGetStrs(ifstream&fin, vector<string>&str) char *s; size_tlen; while (fin.read(char *)&len, sizeof(size_t) s = new charlen; fin.read(s, len); slen + 1 = '0' str.push_back(s);