面向对象程序设计(C++)自我测试练习参考答案 联系客服

发布时间 : 星期三 文章面向对象程序设计(C++)自我测试练习参考答案更新完毕开始阅读6d699ec07cd184254a353549

#include int a[5]={1,2,3,4,5}; int fnPrint(int i) { ( if(i>=5) ) Throw “错误”; return a[i]; }

void main() { try { ( for(int =0;i<5;i++) cout<

三、编程题

1. 设计一个函数模板求x3对,并以整型和双精度型进行调用。 #include \template T fnPower(T x) { T sum=1; sum = x*x*x; return sum; }

void main() {

cout<

template

void fnMaxMinCount(T1 *x,T2 *maxcount,T2 *mincount,int count) {

T2 max=0,min=0;

for(int i=0;i0)max++;

if(x[i]<0)min++; } *maxcount=max; *mincount=min; }

void main() { int max,min; int a[]={1,5,33,34,-89,86,-31,9}; fnMaxMinCount(a,&max,&min,8); cout<<\ 数组a中,正数元素的个数和负数元素的个数分别为:\ cout<

第10章 文 件

一、单选题

1.当已存在一个abc.txt文件时,执行函数open(\的功能是( D )。

A. 打开abc.txt文件,清除原有的内容 B. 打开abc.txt文件,只能写入新的内容 C. 打开abc.txt文件,只能读取原有内容

D. 打开abc.txt文件,可以读取和写入新的内容

2. 若用open()函数打开一个已存在的二进制文件,保留该文件原有内容,且可以读、可以写。则文件打开模式是( B )。

A. ios::in B. ios::in|ios::out|ios::binary C. ios::out D. ios::in|ios::out 3.关于文件定位,下面选项中错误的是( A )

A. seekp(long streampos,int cur)是用于输入流的定位函数;

B. seekg(long streampos)该函数执行后,读指针定位到距离文件开始streampos个字节的位置。

C. tellg()成员函数返回当前输入流读指针的位置

D.seekg(long streampos,int cur)函数调用时,参数cur可选为0,1,2 4. 下列选项中错误的注释是( A )。

A. file1.seekg(1234L,ios::cur);//把文件的写指针从当前位置向后移1234个字节 B. file1.seekp(123L,ios::beg);//把文件的写指针从文件开头位置后移123个字节 C. file1.seekg(1024L,ios::end);//把文件的读指针从文件末尾向前移1024个字节 D. file1.seekg(1234L,1); //把文件的读指针从当前位置向后移1234个字节 5.下程序运行后,文件t1.dat中的内容是( C )。 #include

void WriteStr(char *fn,char *str) { }

void main() { }

WriteStr(\WriteStr(\ofstream outfile;

outfile.open(fn,ios::out); outfile<

A. Good B. Good morning! C. Good night! D. Good morning!Good night!

二、填空题

1. fstream类中打开文件的成员函数名为( open ),该函数的3个参数分别描述要打开文件的( 文件名 )、( 打开模式 )和( 保户方式 )。

2.对于一个打开的ifstream对象,测试是否读到文件尾的函数是( get() )。 3.用get()函数读取文件内容,到达文件尾时,返回( EOF )。

4.C++的文件定位分为读位置和写位置的定位,与之相对应的成员函数分别是( seekg() )和( seekp() )。

5. 对于定位函数seekg(long streampos,int cur),当希望从文件起始位置定位到文件的第100个字节时,cur参数应该设置为( ios::beg )。

三、编程题

1. 编一程序,将两个文本文件的内容合并后存入另一文件中。

#include #include

void main(int argc,char *argv[]) {

char ch;

if(argc != 4) {

Cout<<\格式错误! \\n\ exit(1); }

ifstream inputfile1(argv[1],ios::in|ios::nocreate); //打开读文件

if(!inputfile1) { }

if(!inputfile2) {

cout<<\打开文件失败!\return;

ifstream inputfile2(argv[2],ios::in|ios::nocreate); //打开读文件

}

if(!outputfile) { }

while((ch = inputfile1.get()) != EOF)outputfile.put(ch); while((ch = inputfile2.get()) != EOF)outputfile.put(ch); cout<<\文件复制成功!\inputfile1.close(); inputfile2.close(); outputfile.close();

//关闭已打开的读文件 //关闭已打开的读文件 //关闭已打开的写文件

cout<<\打开文件失败!\outputfile1.close (); outputfile2.close (); return;

//关闭已打开的读文件

cout<<\打开文件失败!\inputfile1.close(); return;

ofstream outputfile(argv[3],ios::out|ios::trunc);//打开写文件

}

2. 编程序实现一个文本文件中所有的小写字母转为大写字母后打印出来。 #include void main() {

char ch;

ifstream inputfile(\打开读文件 if(!inputfile) {

cout<<\打开文件失败!\ return; }

while((ch = inputfile.get()) != EOF) {

if(ch>='a' && ch<='z')ch -= 32; //小写字母转为大写字母 cout<

inputfile.close(); //关闭已打开的读文件 }

3.从键盘输入若干名学生的信息(姓名、学号和成绩),写入二进制文件,再从该文件中读出学生信息,输出在屏幕上。 #include \#include \#include \class Student