下列语句正确的有()
第1题:
下面程序的预设功能是:统计文件abc.txt中的字符个数。
include <iostream.h>
include <fstream.h>
include <stdlib.h>
void main()
{
fstream file;
file.open( "abc.txt", ios::in);
if ( !file )
{
cout<<"Can not open abc.txt"<<end1;
abort();
}
char ch;
int i = O;
while (______________)
{
file.get(ch);
i++;
}
cout<<"Characters : "<<i<<end1;
file.close();
}
则程序中空白处应该填入的语句是【 】。
第2题:
有以下程序:
include <iostream.h>
include <fstream.h>
include <stdlib.h>
int main()
{
fstream filel,file2;
char line[100];
filel.open("source.txt",ios::in);
if(!file1)
{
cout<<"Can't open file source.txt!"<<end1;
abort();
}
file2.open("dest.txt",ios::out);
if(!file2)
{
cout<<"Can't open file dest.txt!"<<end1;
abort();
}
while(!file1.eof())
{
filel.getline(1ine,100);
file2<<line;
file2<<end1;
}
filel.close();
file2.close();
return 0;
}
此程序实现的功能是【 】。
第3题:
【C++代码】
#include <list>
#include <iostream>
#include <string>
using namespace std;
class AbstractFile{
protected:
string name; //文件或目录名称
public:
void printName(){cout<<name;} //打印文件或目录名称
virtual void addChild(AbstractFile *file)=0; //给一个目录增加子目录或文件}
virtual void removeChild(AbstractFile *file)=0; //删除一个目录的子目录或文件
virtual list<AbstractFile*> *getChildren()=0; //获得一个目录的子目录或文件
};
class File:public AbstractFile{
public:
File(string name) {(1) = name;}
void addChild(AbstractFile *file){return;}
void removeChild(AbstractFile *file){return;}
(2) getChildren(){return (3) ;}
};
class Folder :public AbstractFile{
private:
list <AbstractFile*> childList: //存储子目录或文件
public:
Folder(string name){ (4) name;}
void addChild(AbstractFile*file){childList.push_back(file);}
void removeChild(AbstractFile*file){childList.remove(file);}
list<AbstractFile*>*getChildren(){return (5) ;}
};
void main(){
//构造一个树形的文件/目录结构
AbstractFile *rootFolder=new Folder("c:\\ ");
AbstractFile*compositeFolder=new Folder("composite");
AbstractFile *windowsFolder=new Folder("windows");
AbstractFile*file=new File("TestCompositejava");
rootFolder->addChild(compositeFolder);
rootFolder->addChild(windowsFolder);
compositeFolder->addChild(file);
}
第4题:
A.#inclue[string.h]
B.#include{string.h}
C.#include(string.h)
D.#include
第5题:
阅读下列说明和c++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6—7所示:
【c++代码】
include<1ist>
include
include
using namespace std;
class AbstractFile{
protected:
string name;//文件或目录名称
public:
void printName(){cout<*getChildren()=0; //获得一个目录的子目录或文件
};
class File:public AbstractFile{
public:
File(string name){ (1) =name;)
void addChild(AbstractFile*file){return ;)
void removeChiid(AbstractFile*file){return;}(2) getChildren(){return ( 3 ) ;}
};
class Folder:public AbstractFile{
private:
listchildList; //存储子目录或文件
public:
Folder(string name){ (4) =name;}
void addChild(AbstractFile*file){childList.push back(file);}
void removeChiid(AbstractFile*file)(chiidList.remove(file);}
list*getChildren(){return (5) ;)
};
voidmain(){
//构造一个树形的文件/目录结构
AbstractFile*rootFolder=new Folder(“C:\\”);
AbstractFile*compositeFolder=flew Folder(”composite”);
AbstractFile*windowsFolder=new Folder(”windows”);
AbstractFile*file=new File(”TestComposite.java”);
rootFolder->addChild(compositeFolder);
rootFolder->addChild (windowsFolder);
compositeFolder->addChiid(file);
)
第6题:
有以下程序:
include<iostream>
include<fstream>
using namespace std;
int main()
{
fstream file;
file.open("abc.txt", ios :: in);
if ( !file )
{
cout<<"Can not open abc.txt"<<end1;
abort();
}
char buf[ 80 ];
int i = 0;
while (!file.eof())
{
file.getline(buf,80);
i++;
}
cout<<"Lines :"<<i<<end1;
file.close();
return 0;
}
程序实现的功能是【 】。
第7题:
以下程序的执行结果是_______。
include<iostream.h>
include<fstream.h>
include<stdlib.h>
void main()
{
char ch:
fstream file:
file.open("abc.dat",ios::out1ios::inlios::binary);
if(! file)
{
cout<<“abc.dat文件不能打开”<<endl:
abort
第8题:
【Java代码】
import Java.util.ArrayList;
import java.util.List;
(1) class AbstractFile{
protected String name;
public void printName(){System.out.println(name);}
public abstract boolean addChild(AbstractFile file);
public abstract boolean removeChild(AbstractF ile file);
public abstract List<AbstractFile> getChildren();
}
class File extends AbstractFile{
public File(String name){this.name=name;}
public boolean addChild(AbstractFile file){return false;}
public boolean removeChild(AbstractFile file){return false;}
public List<AbstractFile> getChildren(){return (2) ;}
}
class Folder extends AbstractFile{
private List <AbslractFile> childList;
public Folder(String name){
this.name=name;
this.childList=new ArrayList<AbstractFile>();
}
public boolean addChild(AbstractFile file) { return childList.add(file);}
public boolean removeChild(AbstractFile file){return childList.remove(file);}
public (3) <AbstractFile> getChildren(){return (4) ;}
}
public class Client{
public static void main(String[] args){
//构造一个树形的文件/目录结构
AbstractFile rootFolder= new Folder("c:\\ ");
AbstractFile compositeFolder=new Folder("composite");
AbstractFile windowsFolder=new Folder("windows");
AbstractFile file=new File("TestComposite.java");
rootFolder.addChild(compositeFolder) ;
rootFolder.addChild(windowsFolder);
compositeFolder.addChild(file) ;
//打印目录文件树
printTree(rootFolder);
}
private static void printTree(AbslractFile ifile){
ifile.printName();
List <AbslractFile> children=ifile.getChildreno:
if(children==null) return;
for (AbstractFile file:children) {
(5) ;
}
}
}
该程序运行后输出结果为:
c:\
composite
TestComposite.java
Windows
第9题:
使用VC6打开考生文件夹下的工程test14_3,此工程包含一个test14_3.cpp,其中定义了类File,但类的定义并不完整。请按要求完成下列操作,将程序补充完整。
(1)完成构造函数的定义,使数据成员filename,content指向为空。请在注释“//**1**”之后添加适当的语句;
(2)完成函数set_contents的定义,该函数为content申请新的空间来存储new_content的内容,成功返回true,失败返回false。请在注释“//**2**”之后添加适当的语句;
(3)完成成员函数copy(File & source,File & target)的定义,该函数实现将source的内容拷贝至target的功能,注意必须使用已经定义的成员函数来实现。请在注释“//**3**”之后添加适当的语句:
(4)完成析构函数的定义,要求释放content指向的空间。请在注释“//**4**”之后添加适当的语句。
输出结果如下:
file2:file1 contents
file2:file2 contents
注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。
源程序文件testl4—3.cpp清单如下:
include<iostream.h>
include<string.h>
class File{
private:
char file_name[64];
char*contents;
public:
File(char*filename)
{
// ** 1 **
};
File::~File()
{
// ** 4 **
}
void printfile(){cout<<file_name<<":"<<contents<<endl;)
friend bool copy(File & source,File & target);
bool set_contents(char * new_contents);
};
bool File::set_contents(char*new_contents)
{
// ** 2 **
if(contents==NULL)
return false;
strcpy(contents,new_contents);
return true;
}
bool copy(File & source,File & target)
{
// ** 3 **
}
void main{)
{
File f1("filel"),f2("file2");
f1.set_contents("filel contents");
copy(f1,f2);
f2.printfile();
f2.set_contents("file2 contents,);
f2.printfile();
}
第10题:
下列#include命令中,正确的是()