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

上传人:b****3 文档编号:2917041 上传时间:2022-11-16 格式:DOCX 页数:23 大小:19.64KB
下载 相关 举报
C++ primer plus第6版中文版编程练习答案第17章.docx_第1页
第1页 / 共23页
C++ primer plus第6版中文版编程练习答案第17章.docx_第2页
第2页 / 共23页
C++ primer plus第6版中文版编程练习答案第17章.docx_第3页
第3页 / 共23页
C++ primer plus第6版中文版编程练习答案第17章.docx_第4页
第4页 / 共23页
C++ primer plus第6版中文版编程练习答案第17章.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

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

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

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

C++primerplus第6版中文版编程练习答案第17章

1、

//count.cpp

#include

usingnamespacestd;

intmain()

{

charch;

intcount=0;

cout<<"Enterastring($toquit):

\n";

while(cin.get(ch))

{

if(ch!

='$')

{

count++;

cout<

}

else

{

cin.putback(ch);

break;

}

}

cout<<"Countis"<

system("pause");

return0;

}

2、

//filein.cpp

#include

#include

#include

intmain(intargc,char*argv[])

{

usingnamespacestd;

if(argc==1)

{

cerr<<"Usage:

"<

exit(EXIT_FAILURE);

}

ofstreamfout(argv[1],ios:

:

out|ios:

:

app);

if(!

fout.is_open())

{

cerr<<"Can'topen"<

exit(EXIT_FAILURE);

}

charch;

cout<<"Enterastring:

\n";

while(cin.get(ch))

{

if(ch!

='\n')

fout<

else

break;

}

fout<

fout.close();

system("pause");

return0;

}

3、

//copy.cpp

#include

#include

#include

intmain(intargc,char*argv[])

{

usingnamespacestd;

if(argc==1)

{

cerr<<"Usage:

"<

exit(EXIT_FAILURE);

}

ifstreamfin;

charch;

fin.open(argv[1],ios:

:

in);

if(!

fin.is_open())

{

cerr<<"Couldnotopen"<

exit(EXIT_FAILURE);

}

ofstreamfout(argv[2],ios:

:

out|ios:

:

trunc);

if(!

fout.is_open())

{

cerr<<"Couldnotopen"<

exit(EXIT_FAILURE);

}

cout<<"Herearethecurrentcontentsofth"

<

\n";

while(fin.get(ch))

{

cout<

fout<

}

fin.close();

fout.close();

system("pause");

return0;

}

//a.txt

Abcdefghijklmnopqrstuvwxyz

4、

//copy_cat.cpp

#include

#include

#include

#include

usingnamespacestd;

constchar*file1="a.txt";

constchar*file2="b.txt";

constchar*file3="c.txt";

intmain()

{

ifstreamfin1,fin2;

fin1.open(file1);

fin2.open(file2);

ofstreamfout;

fout.open(file3,ios_base:

:

out|ios_base:

:

trunc);

strings1,s2;

if(!

fin1.is_open()||!

fin2.is_open())

{

cerr<<"Can'topen"<

exit(EXIT_FAILURE);

}

if(!

fout.is_open())

{

cerr<<"Can'topen"<

exit(EXIT_FAILURE);

}

while(!

fin1.eof()&&!

fin2.eof())

{

getline(fin1,s1);

cout<<"File1"<

getline(fin2,s2);

cout<<"File2"<

fout<

}

while(!

fin1.eof())

{

getline(fin1,s1);

cout<<"Morefile1"<

fout<

}

while(!

fin2.eof())

{

getline(fin2,s2);

cout<<"Morefile2"<

fout<

}

fin1.close();

fin2.close();

fout.close();

system("pause");

return0;

}

//a.txt

eggskitesdonuts

balloonshammers

stones

//b.txt

zerolassitude

financedrama

5、

//name.cpp

#include

#include

#include

#include

#include

#include

constchar*file1="mat.dat";

constchar*file2="pat.dat";

constchar*file3="matnpat.dat";

intmain()

{

usingnamespacestd;

stringfname1;

setA;

ifstreamfinA,finB;

finA.open(file1);

finB.open(file2);

ofstreamfout;

ostream_iteratorout1(cout,""),out2(fout,"\n");

fout.open(file3,ios_base:

:

out|ios_base:

:

trunc);

if(!

finA.is_open()||!

finB.is_open())

{

cerr<<"Can'topen"<

exit(EXIT_FAILURE);

}

if(!

fout.is_open())

{

cerr<<"Can'topen"<

exit(EXIT_FAILURE);

}

while(!

finA.eof())

{

getline(finA,fname1);

A.insert(fname1);

}

cout<<"Mat'sfriends:

\n";

copy(A.begin(),A.end(),out1);

cout<

stringfname2;

setB;

while(!

finB.eof())

{

getline(finB,fname2);

B.insert(fname2);

}

cout<<"Pat'sfriends:

\n";

copy(B.begin(),B.end(),out1);

cout<

cout<<"UnionofMatandPat'sfriends:

";

set_union(A.begin(),A.end(),B.begin(),B.end(),out1);

set_union(A.begin(),A.end(),B.begin(),B.end(),out2);

cout<

finA.close();

finB.close();

fout.close();

system("pause");

return0;

}

6、

//emp.h

#ifndefEMP_H_

#defineEMP_H_

#include

#include

#include

usingnamespacestd;

enumclasstype{Employee,Manager,Fink,Highfink};

classabstr_emp

{

private:

stringfname;

stringlname;

stringjob;

public:

abstr_emp();

abstr_emp(conststring&fn,conststring&ln,

conststring&j);

virtualvoidShowAll()const;

virtualvoidSetAll();

friendostream&operator<<(ostream&os,constabstr_emp&e);

virtual~abstr_emp()=0;

virtualvoidwriteall(ofstream&ofs);

virtualvoidgetall(ifstream&ifs);

};

classemployee:

publicabstr_emp

{

public:

employee();

employee(conststrin

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

当前位置:首页 > 法律文书 > 调解书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1