C++ primer plus中文编程练习答案第17章.docx

上传人:牧羊曲112 文档编号:3153747 上传时间:2023-03-11 格式:DOCX 页数:13 大小:39.50KB
返回 下载 相关 举报
C++ primer plus中文编程练习答案第17章.docx_第1页
第1页 / 共13页
C++ primer plus中文编程练习答案第17章.docx_第2页
第2页 / 共13页
C++ primer plus中文编程练习答案第17章.docx_第3页
第3页 / 共13页
C++ primer plus中文编程练习答案第17章.docx_第4页
第4页 / 共13页
C++ primer plus中文编程练习答案第17章.docx_第5页
第5页 / 共13页
亲,该文档总共13页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《C++ primer plus中文编程练习答案第17章.docx》由会员分享,可在线阅读,更多相关《C++ primer plus中文编程练习答案第17章.docx(13页珍藏版)》请在三一办公上搜索。

1、C+ primer plus中文编程练习答案第17章1、 /count.cpp #include using namespace std; int main charch; int count = 0; cout Enter a string($ to quit):n; while (cin.get(ch) if (ch != $) count+; coutch; else cin.putback(ch); break; cout Count is count endl; system(pause); return 0; 2、 /filein.cpp #include #include #in

2、clude 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 Cant open argv1 file for output.n; exit(EXIT_FAILURE); charch; cout Enter a string: n; while (cin.get(ch) if (ch != n

3、) foutch; else break; foutendl; fout.close; system(pause); return 0; 3、 /copy.cpp #include #include #include 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

4、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) coutch; foutch; fin.close; fout.close; system(pause); return 0; /a.txt A

5、bcdefghijklmnopqrstuvwxyz 4、 /copy_cat.cpp #include #include #include #include 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_bas

6、e:trunc); string s1, s2; if (!fin1.is_open | !fin2.is_open) cerr Cant open file1 or file2 endl; exit(EXIT_FAILURE); if (!fout.is_open) cerr Cant 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

7、 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 d

8、rama 5、 /name.cpp #include #include #include #include #include #include const char *file1 = mat.dat; const char *file2 = pat.dat; const char *file3 = matnpat.dat; int main using namespace std; string fname1; set A; ifstreamfinA, finB; finA.open(file1); finB.open(file2); ofstreamfout; ostream_iterato

9、rout1(cout, ), out2(fout, n); fout.open(file3, ios_base:out | ios_base:trunc); if (!finA.is_open | !finB.is_open) cerr Cant open file1 or file2 endl; exit(EXIT_FAILURE); if (!fout.is_open) cerr Cant open file3 endl; exit(EXIT_FAILURE); while (!finA.eof) getline(finA, fname1); A.insert(fname1); cout

10、Mats friends: n; copy(A.begin, A.end, out1); coutendl; string fname2; set B; while (!finB.eof) getline(finB, fname2); B.insert(fname2); cout Pats friends: n; copy(B.begin, B.end, out1); coutendl; cout Union of Mat and Pats friends: ; set_union(A.begin, A.end, B.begin, B.end, out1); set_union(A.begin

11、, A.end, B.begin, B.end, out2); coutendl; finA.close; finB.close; fout.close; system(pause); return 0; 6、 /emp.h #ifndef EMP_H_ #define EMP_H_ #include #include #include using namespace std; enumclasstype Employee, Manager, Fink, Highfink ; classabstr_emp private: stringfname; stringlname; string jo

12、b; 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

13、: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: intI

14、nChargeOfconst 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(ofstr

15、eam&ofs); virtual void getall(ifstream&ifs); voidgetInCharge coutinchargeof; voidwriteInCharge(ofstream&ofs) ofsinchargeofinchargeof; ; class fink :virtual public abstr_emp private: stringreportsto; protected: const string ReportsToconst return reportsto; string&ReportsTo return reportsto; public: f

16、ink; 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 coutreportsto; v

17、oidwriteReportsTo(ofstream&ofs) ofsreportstoreportsto; ; 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, intic

18、o = 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) ab

19、str_emp:abstr_emp(const string &fn, const string &ln, const string &j) : fname(fn), lname(ln), job(j) voidabstr_emp:ShowAllconst cout Firstname: fnameendl; cout Lastname: lnameendl; cout Job is: job endl; voidabstr_emp:SetAll cout Enter firstname: ; getline(cin, fname); cout Enter lastname: ; getlin

20、e(cin, lname); cout Enter position: ; getline(cin, job); ostream&operator(ostream&os, constabstr_emp&e) ose.fname e.lname , e.jobendl; returnos; abstr_emp:abstr_emp voidabstr_emp:writeall(ofstream&ofs) ofsfname n lname n job n; voidabstr_emp:getall(ifstream&ifs) getline(ifs, fname); getline(ifs, lna

21、me); 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:wr

22、iteall(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:man

23、ager(const manager &m) : abstr_emp(m) void manager:ShowAllconst abstr_emp:ShowAll; cout Inchargeof: InChargeOf endl; void manager:SetAll abstr_emp:SetAll; coutinchargeof).get; void manager:writeall(ofstream&ofs) ofs Manager endl; abstr_emp:writeall(ofs); ofsinchargeofinchargeof; fink:fink :abstr_emp

24、 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: Re

25、portsTo endl; void fink:SetAll abstr_emp:SetAll; coutreportsto; void fink:writeall(ofstream&ofs) ofs Fink endl; abstr_emp:writeall(ofs); ofsreportstoreportsto; highfink:highfink :abstr_emp, manager, fink highfink:highfink(const string &fn, const string &ln, const string &j, const string &rpo, intico

26、) : 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, c

27、onst 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:Se

28、tAll; manager:getInCharge; fink:getReportsTo; voidhighfink:writeall(ofstream&ofs) ofsHighfinkendl; 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 #

29、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 classtype).get(ch) switch (classtype) case Employee: pci = new employee; break; case Manager: pc

30、i = new manager; break; case Fink: pci = new fink; break; caseHighfink: pci = new highfink; break; coutclasstypegetall(fin); pci+-ShowAll; fin.close; ofstreamfout; fout.open(file, ios_base:out | ios_base:app); if (!fout.is_open) cerr Cant open file file for output.n; exit(EXIT_FAILURE); cout e to em

31、ployee, m to managern f to fink, h to highfinkn q to quit: ; int index = 0; while (cin.get(ch).get &ch != q&index SetAll; index+; break; casem: pcindex = new manager; pcindex-SetAll; index+; break; casef: pcindex = new fink; pcindex-SetAll; index+; break; caseh: pcindex = new highfink; pcindex-SetAl

32、l; index+; break; default: cout MAX) break; cout e to employee, m to managern f to fink, h to highfinkn q to quit: ; for (i = 0; iwriteall(fout); fout.close; fin.clear; fin.open(file); if (fin.is_open) cout Here are the current contents of the file classtype).get(ch) switch (classtype) case Employee

33、: pci = new employee; break; case Manager: pci = new manager; break; case Fink: pci = new fink; break; caseHighfink: pci = new highfink; break; coutclasstypegetall(fin); pci+-ShowAll; cout Done.n; system(pause); return 0; 7、 /vcopy.cpp #include #include #include #include #include #include #include u

34、sing namespace std; voidShowStr(const string &); voidGetStrs(ifstream&, vector&); 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.da

35、ta, len); int main vectorvostr; 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

36、, vostr.end, Store(fout); fout.close; vectorvistr; 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) coutstrendl; voidGetStrs(ifstream&fin, vector&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);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号