《C++STL-数据结构与算法实现》课件第三章_第1页
《C++STL-数据结构与算法实现》课件第三章_第2页
《C++STL-数据结构与算法实现》课件第三章_第3页
《C++STL-数据结构与算法实现》课件第三章_第4页
《C++STL-数据结构与算法实现》课件第三章_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第三章课后习题一、概念理解题cin和cout:用于输入和输出的标准流对象cerr和clog用于输出错误和日志信息的标准流对象由于在用cin获取输入时,操作符>>默认是以空白字符(空格、tab、换行符)作为分隔符将字符串进行分割的,若用户输入的字符中含有分隔符则不能够获取到完整的字符。可以改用get系列函数。二进制文件是基于值编码的文件,文本文件是基于字符编码的文件。文本文件的存储与其读取基本上是个逆过程。而二进制文件的存取与文本文件的存取类似,但是编/解码方式不同。主要用到能在输入输出文件流中进行随机检索访问的相关成员函数,例如Ostream中的seekp(long,int)以及istream中的seekg(long,int)等。上机练习题略2.#include<iostream>#include<fstream>#include<string>usingnamespacestd;voidmain(){stringstr;fstreamiofile("text.txt",ios::out);if(!iofile){cout<<"Createfilefailure...\n";exit(0);}cout<<"请输入字符串内容:\n";while(1){getline(cin,str);if(str=="end")break;iofile<<str<<endl;}iofile.close();iofile.open("text.txt",ios::in);while(!iofile.eof()){iofile>>str;cout<<str<<endl;}iofile.close();}3.略4.略5.参考答案:#include<iostream>#include<fstream>Usingnamespacestd;Classstudent{public:charstrName;intnum;intgrade;Student(){strName=’’;num=0;grade=0;}Friendostream&operator<<(ostream&os,conststudent&s){os<<s.strName<<”+”<<s.num<<”+”<<s.grade;}};intmain(){Students1(zy,01,10),s2(zs,02,09),s3;ofstreamout;out.open(“c:\a.txt”);out<<s1<<endl;out<<s2<<endl;out<<s1<<endl;out.close();char*ch=newchar[10];stringname=”c;\\a.txt”;ifstreamin(name);while(in.getline(ch,10)){cout<<ch<<endl;}}6.略7.#include<iostream>#include<fstream>usingnamespacestd;#defineFILEPATH"test.txt"//文章信息结构体structTextInfo{ intnum_of_words; intnum_of_lines;};//判断是否为英文单词boolisEnglishWords(charc){ return((c>='A'&&c<='Z')||(c>='a'&&c<='z')||c=='\'');}//编写一程序,统计一篇英文文章中单词的个数与行数。TextInfocompteWordsAndLines(){ TextInfoti; ti.num_of_lines=0; ti.num_of_words=0; //单词的个数,有空格就算一个单词 //首先以只读的形式打开文章 ifstreamfin; fin.open(FILEPATH); if(fin.fail()) { cout<<"emptyfile1"<<endl; fin.close(); } charbuf[1024]={}; intlines=0; intwords=0; boolinword=false; inti=0; while(!fin.eof()) { fin.getline(buf,999); lines++; i=0; inword=false; while(buf[i]!=0)//不为行尾 { if(!isEnglishWords(buf[i])) inword=false; elseif(isEnglishWords(buf[i])&&inword==false){ words++; inword=true; } i++; } } ti.num_of_lines=lines; ti.num_of_words=words; fin.close(); returnti;}intmain(){ TextInfomyti=compteWordsAndLines(); cout<<myti.num_of_lines<<endl; cout<<myti.num_of_words<<endl; cout<<"helloworld!"<<endl; system("pause"); return0;}8.参考答案#include<fstream>#include<iostream>Usingnamespacestd;Intmain(){char*ch=newchar[80];Stringname=”c:\\namelist.txt”;Ifstreamin(name);If(in==0){Cout<<”文件打开失败!”;Return0;}While(in.getline(ch,80)){Cout<<ch<<end;}For(inti=0;i<3;i++){inta=randomint(0,80)Cout<<ch[a];Ofstreamout;Out.open(“c://winner.txt”);Out<<ch[a]<<endl;}Out.close;Return0}9.参考答案#include<fstream>#include<iostream>Usingnamespacestd;Intmain(){ intaverge;char*ch=newchar[80];Stringname=”c:\\grade.txt”;Ifstreamin(name);If(in==0){Cout<<”文件打开失败!”;Return0;}While(in.getline(ch,80)){Cout<<ch<<end;}Inttemp1,temp2,sum=0;Temp1=ch[0];Temp2=ch[0];For(i=0;i<ch.length;i++

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论