c++书中例题源代码第13章.docx

上传人:b****5 文档编号:3871935 上传时间:2022-11-26 格式:DOCX 页数:19 大小:18.99KB
下载 相关 举报
c++书中例题源代码第13章.docx_第1页
第1页 / 共19页
c++书中例题源代码第13章.docx_第2页
第2页 / 共19页
c++书中例题源代码第13章.docx_第3页
第3页 / 共19页
c++书中例题源代码第13章.docx_第4页
第4页 / 共19页
c++书中例题源代码第13章.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

c++书中例题源代码第13章.docx

《c++书中例题源代码第13章.docx》由会员分享,可在线阅读,更多相关《c++书中例题源代码第13章.docx(19页珍藏版)》请在冰豆网上搜索。

c++书中例题源代码第13章.docx

c++书中例题源代码第13章

【例13.1】使用预定义的插入符进行屏幕输出。

#include

#include

voidmain()

{

cout<<"Thelengthof\"Thisastring\"is:

"<

cout<<"Thesizeof\"Thisisaprogram\"is:

"<

}

【例13.2】分析下列程序的输出结果,该程序中使用了流对象cout和插入符<<。

#include

voidmain()

{

intx=90;

int*px=&x;

cout<<"x="<

cout<<"*px="<<*px<

}

【例13.3】分析下列程序的输出结果。

该程序中使用了流对象cout和插入运算符。

#include

voidfun(inti,intj);

voidmain()

{

fun(125,5);

fun(50,0);

}

voidfun(inti,intj)

{

if(j!

=0)

cout<

else

cout<<"zeroencountered.\n";

}

【例13.4】分析下列程序的输出结果。

#include

voidmain()

{

cout<<'B'<<'E'<<'I'<<'J'<<'I'<<'N'<<'G'<<'\n';

cout.put('B').put('E').put('I').put('J').put('I').put('N').put('G').put('\n');

charc1='A',c2='B',c3='C';

cout.put(c1).put(c2).put(c3).put('\n');

}

【例13.5】分析下列程序的输出结果,注意该程序中write()函数的用法。

#include

#include

voidprint(char*s)

{

cout.write(s,strlen(s)).put('\n');

cout.write(s,6)<<'\n';

}

voidmain()

{

char*str="IloveBeijing!

";

cout<<"Thestringis"<

print(str);

}

【例13.6】分析下列程序的输出结果。

#include

voidmain()

{

intx,y;

cout<<"Pleaseentertwointegers:

";

cin>>x>>y;

cout<<"("<

}

【例13.7】编程计算从键盘上输入的单词个数,并从中找出字符最多的单词,输出它的字符个数及单词的字符。

#include

#include

voidmain()

{

charbuf[40],largest[40];

intcurLen,maxLen=-1,cnt=0;

cout<<"Inputword:

\n";

while(cin>>buf)

{

curLen=strlen(buf);

cnt++;

if(curLen>maxLen)

{

maxLen=curLen;

for(inti=0;i<40;i++)

largest[i]=buf[i];

}

}

cout<

cout<

}

【例13.8】编程实现将从键盘输入的字符显示在屏幕上。

#include

voidmain()

{

charch;

cout<<"Input:

";

while((ch=cin.get())!

=EOF)

cout.put(ch);

}

【例13.9】编程统计从键盘输入的每一行字符的个数,并从中选出最长的行的字符个数,统计共输入多少行。

#include

constintSIZE=80;

voidmain()

{

charbuf[SIZE];

intlcnt=0,lmax=-1;

cout<<"Input...\n";

while(cin.getline(buf,SIZE))

{

intcount=cin.gcount();

lcnt++;

if(count>lmax)

lmax=count;

cout<<"Line#"<

"<

cout.write(buf,count).put('\n').put('\n');

}

cout<

cout<<"Longestline:

"<

cout<<"Totalline:

"<

}

【例13.10】编写从键盘上读取多行字符的程序。

#include

voidmain()

{

constintSIZE=80;

charbuf[SIZE]="";

cout<<"Input...\n";

cin.read(buf,SIZE);

cout<

cout<

}

【例13.11】分析下列程序的输出结果,熟悉使用成员函数设置标志字的方法。

#include

voidmain()

{

cout.setf(ios:

:

hex,ios:

:

basefield);

cout<<"HEX:

68->"<<68<

cout.setf(ios:

:

showbase);

cout<<"HEX:

68->"<<68<

cout.setf(ios:

:

uppercase);

cout<<"HEX:

68-68<

cout.setf(ios:

:

oct,ios:

:

basefield);

cout<<"OCT:

68->"<<68<

cout.setf(ios:

:

dec,ios:

:

basefield);

cout<<"DEC:

68->"<<68<

cout.setf(ios:

:

showpos);

cout<<"DEC:

68->"<<68<

}

【例13.12】分析下列程序的输出结果,学会使用控制输出格式的成员函数的方法。

#include

voidmain()

{

cout<<"12345678901234567890\n";

inti=12345;

cout<

cout.width(10);

cout<

cout.width(10);

cout.fill('#');

cout.setf(ios:

:

left,ios:

:

adjustfield);

cout<

cout.setf(ios:

:

right,ios:

:

adjustfield);

cout.precision(6);

doubled=123.456789;

cout<

cout.setf(ios:

:

scientific,ios:

:

floatfield);

cout<

cout<<"width:

"<

}

【例13.13】分析下列程序的输出结果,学会在程序中使用控制符。

#include

#include

voidmain()

{

cout<<"12345678901234567890\n";

inti=12345;

cout<

cout<

cout<

:

right)<

:

left)<

doubled=123.456789;

cout<

cout.setf(ios:

:

scientific,ios:

:

floatfield);

cout<

cout<<"width:

"<

}

【例13.14】分析下列程序的输出打印结果。

#include

#include

voidmain()

{

for(inti=7;i>1;i--)

cout<

for(i=1;i<8;i++)

cout<

}

【例13.15】编写程序,重载插入符和提取符,按中国习惯(yyyy/mm/dd)对日期进行输入和输出。

#include

classDate

{

public:

Date(inty,intm,intd)

{

Year=y;

Month=m;

Day=d;

}

friendostream&operator<<(ostream&stream,Date&date);

friendistream&operator>>(istream&stream,Date&date);

private:

intYear,Month,Day;

};

ostream&operator<<(ostream&stream,Date&date)

{

stream<

returnstream;

}

istream&operator>>(istream&stream,Date&date)

{

stream>>date.Year>>date.Month>>date.Day;

returnstream;

}

voidmain()

{

Dated(2000,6,10);

cout<<"Currentdate:

"<

cout<<"Enternewdate:

";

cin>>d;

cout<<"Newdate:

"<

}

【例13.16】编程将一些字符信息写入指定文件中。

#include

#include

#include

voidmain()

{

fstreamoutfile;

outfile.open("file1.dat",ios:

:

out);

if(!

outfile)

{

cout<<"file1.datcan'topen.\n";

abort();

}

outfile<<"thisisaprogram.\n";

outfile<<"thisisastring.";

outfile<<"\nok!

";

outfile.close();

}

【例13.17】编程序将例13.16中被写入文件中的字符信息读出并显示。

#include

#include

#include

voidmain()

{

fstreaminfile;

infile.open("file1.dat",ios:

:

in);

if(!

infile)

{

cout<<"file1.datcan'topen.\n";

abort();

}

chars[80];

while(!

infile.eof())

{

infile.getline(s,sizeof(s));cout<

}

infile.close();

}

【例13.18】编程使用get()函数和put()函数读写文本文件。

#include

#include

#include

#include

voidmain()

{

fstreamoutfile,infile;

outfile.open("file2.dat",ios:

:

out);

if(!

outfile)

{

cout<<"file2.datcan'topen.\n";

abort();

}

chars[]="IloveC++programing.";

for(inti=0;i<=(int)strlen(s);i++)

outfile.put(s[i]);

outfile.close();

infile.open("file2.dat",ios:

:

in);

if(!

infile)

{

cout<<"file2.datcan'topen.\n";

abort();

}

charch;

while(infile.get(ch))

cout.put(ch);

cout<

infile.close();

}

【例13.19】编程将一个文件内容复制到另一个文件中。

#include

#include

#include

voidmain()

{

fstreaminfile,outfile;

infile.open("file2.dat",ios:

:

in);

if(!

infile)

{

cout<<"file2.datcan'topen.\n";

abort();

}

outfile.open("file3.dat",ios:

:

out);

if(!

outfile)

{

cout<<"file3.datcan'topen.\n";

abort();

}

charch;

while(infile.get(ch))

outfile.put(ch);

infile.close();

outfile.close();

}

【例13.20】编程对一个二进制文件进行读/写操作。

#include

#include

#include

structperson

{

charname[80];

doubleheight;

unsignedshortage;

};

structpersonpeople[5]={"Ma",1.78,35,"Zhang",1.68,24,"Hu",1.90,40,

"Lu",1.89,50,"Lang",2.00,18};

voidmain()

{

fstreamfile;

file.open("file4.dat",ios:

:

in|ios:

:

out|ios:

:

binary);

if(!

file)

{

cout<<"file4.datcan'topen.\n";

abort();

}

for(inti=0;i<5;i++)

file.write((char*)&people[i],sizeof(people[i]));

file.seekp(0,ios:

:

beg);

for(i=0;i<5;i++)

{

file.read((char*)&people[i],sizeof(people[i]));

cout<

}

file.close();

}

【例13.21】按下列要求编写一个程序。

①该程序用来将一些大学生和硕士生的有关信息输入后存放在一个文件中。

②关于大学生的信息有:

姓名,学号,某门功课的成绩。

③关于硕士生的信息有:

除了大学生应有信息外,再增加一个导师姓名。

④将一个大学生和两个硕士生的信息写入程序中,也可从键盘输入。

#include

#include

#include

#include

classStudent

{

public:

Student(char*pN,unsignednum,doubleg)

{

strcpy(Name,pN);

uID=num;

grade=g;

}

virtualvoidPrint(ostream&out);

friendostream&operator<<(ostream&out,Student&st);

private:

charName[80];

unsigneduID;

doublegrade;

};

voidStudent:

:

Print(ostream&out)

{

out.setf(ios:

:

left,ios:

:

adjustfield);

out.width(15);

out<

out.setf(ios:

:

right,ios:

:

adjustfield);

out.width(8);

out<

}

ostream&operator<<(ostream&out,Student&st)

{

st.Print(out);

out<

returnout;

}

classMaster:

publicStudent

{

public:

Master(char*pN,unsignednum,doubleg,char*pdN):

Student(pN,num,g)

{

strcpy(dName,pdN);

}

voidPrint(ostream&out);

private:

chardName[80];

};

voidMaster:

:

Print(ostream&out)

{

Student:

:

Print(out);

out<<""<

}

voidmain()

{

ofstreamout("abc.txt");

Students1("Wangping",99001,96.5);

Masters2("Maguang",99056,84.8,"Hu");

Masters3("Jiangfang",99078,90.5,"Huang");

out<

out<

out<

}

【例13.22】分析下列程序的输出结果,学会随机读/写数据文件的方法。

#include

#include

#include

voidmain()

{

fstreamfile("file5.dat",ios:

:

in|ios:

:

out|ios:

:

binary);

if(!

file)

{

cout<<"file5.datcan'topen.\n";

abort();

}

for(inti=1;i<=20;i++)

file.write((char*)&i,sizeof(int));

longpos=file.tellp();

cout<<"Currentbytenumber:

"<

for(i=20;i<=50;i++)

file.write((char*)&i,sizeof(int));

file.seekp(pos);

file.read((char*)&i,sizeof(int));

cout<<"Thedatastoredis"<

file.seekg(0,ios:

:

beg);

for(i=50;i<=100;i++)

file.write((char*)&i,sizeof(int));

file.seekg(pos);

file.read((char*)&i,sizeof(int));

cout<<"Thedatastoredis"<

file.seekp(116,ios:

:

cur);

file.read((char*)&i,sizeof(int));

cout<<"Thedatastoredis"<

cout<<"Currentbytenumber:

"<

}

【例13.23】分析下列程序的输出结果,熟悉成员函数seekp()的用法。

#include

#include

#include

voidmain()

{

structstudent

{

charname[50];

longnumber;

doubletotalscord;

}

stu[5]={"Ma",98001,89.5,"Li",98023,82.9,"Gao",98045,90.2,

"Hu",98066,92.1,"Yan",98

展开阅读全文
相关搜索

当前位置:首页 > 小学教育 > 数学

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

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