实验7输入输出流库.docx

上传人:b****0 文档编号:9732117 上传时间:2023-05-20 格式:DOCX 页数:12 大小:17.43KB
下载 相关 举报
实验7输入输出流库.docx_第1页
第1页 / 共12页
实验7输入输出流库.docx_第2页
第2页 / 共12页
实验7输入输出流库.docx_第3页
第3页 / 共12页
实验7输入输出流库.docx_第4页
第4页 / 共12页
实验7输入输出流库.docx_第5页
第5页 / 共12页
实验7输入输出流库.docx_第6页
第6页 / 共12页
实验7输入输出流库.docx_第7页
第7页 / 共12页
实验7输入输出流库.docx_第8页
第8页 / 共12页
实验7输入输出流库.docx_第9页
第9页 / 共12页
实验7输入输出流库.docx_第10页
第10页 / 共12页
实验7输入输出流库.docx_第11页
第11页 / 共12页
实验7输入输出流库.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

实验7输入输出流库.docx

《实验7输入输出流库.docx》由会员分享,可在线阅读,更多相关《实验7输入输出流库.docx(12页珍藏版)》请在冰点文库上搜索。

实验7输入输出流库.docx

实验7输入输出流库

实验内容

程序Ex_File:

用文件来实现一个学生记录的添加、查找等操作。

提示:

学生记录用类CstudenRec表示,它的数据成员有:

姓名、学号、3门课的成绩以及总平均分,成员函数记录显示Print、记录键盘输入Input和数据校验Validate以及“<<””>>”运算符重载等。

文件操作用CStuFile类定义,成员函数有数据的添加Add、查找Seek、显示List等。

实验准备和说明

(1)在学习完第2章内容之后进行本次实验。

(2)编写本次上机所需要的程序。

实验步骤

1.创建工作文件夹

打开计算机,在“D:

\VisualC++程序、LiMing”文件夹中创建一个新的子文件夹“实验7”。

2.创建控制台应用程序项目Ex_File

创建控制台应用程序项目Ex_File的具体步骤如下。

(1)启动VisualC++6.0。

(2)选择“文件”→“新建”菜单命令,显示出“新建”对话框。

从列表框中选中Win32ConsoleApplication项。

(3)在“工程”编辑框中输入控制台应用程序项目名称Ex_File,并将项目文件夹定位到“D:

\VisualC++程序\LiMing\实验7”。

(4)单击“确定”按钮,显示Win32应用程序向导对话框。

选中Anemptyproject项。

单击“完成”按钮,系统将显示向导创建的信息,单击“确定”按钮将自动创建此应用程序。

3.添加Ex_File.h文件

添加Ex_File.h文件的具体步骤如下。

(1)选择“文件”→“新建”菜单命令,将“新建”对话框切换到“文件”标签页面,选中“C/C++HeaderFile”文件类型,在文件编辑框中输入Ex_File.h,单击“确定”按钮。

(2)在Ex_File.h中添加CstudentRec类代码:

#include

#include

#include

#include

classCstudentRec

{

public:

CstudentRec(char*name,char*id,floatscore[]);

CstudentRec(){chFlag=`N`;};∥默认构造函数

~CStudentRec(){};//默认析构函数

voidInput(void);//键盘输入,返回记录

floatValidate(void);//成绩数据的输入验证,返回正确值

voidPrint(boolisTitle=false);//记录显示

friendostream&operator<<(ostream&os,CStudentRec&stu);

friendistream&operator>>(istream&is,CStudentRec&stu);

charchFlag;//标志,‘A’表示正常,‘N’表示空

charstrName[20];//姓名

charstrID[10];//学号

floatfScore[3];//3门成绩

floatfAve;//总平均分

};

//CStudent类的实现

CStudentRec:

:

CStudentRec(char*name,char*id,floatscore[])

{

strncpy(strName,name,20);

strncpy(strID,id,10);

fAve=0;

for(onti=0;i<3;i++)

{

fScore[i]=score[i];fAve+=fScore[i];

}

fAve=float(fAve/3.0);

chFlag='A';

}

voidCStudentRec:

:

Input(void)

{

cout<<"姓名";cin>>strName;

cout<<"学号";cin>>strID;

floatfSum=0;

for9inti=0;i<3;i++)

{

cout<<"成绩"<

";

fScore[i]+Validate();fSum+=fScore[i];

}

fAve=(float)(fSum/3.0);

chFlag='A';

}

floatCStudentRec:

:

Validate(void)

{

ints;l

charbuf[80];

floatres;

for(;;)

{

cin>>res;

s=cin.rdstate();

while(s)

{

cin.clear();

cin.getline(buf,80);

cout<<"非法输入,重新输入:

";

cin>>res;

s=cin.rdstate();

}

if((res<=100.0)&&(res>=0.0))break;

else

cout<<"输入的成绩超过范围!

请重新输入:

";

}

returnres;

}

voidCStudentRec:

:

Print(boolisTitle)

{

cout.setf(ios:

:

left);

if(isTitle)

cout<

cout<

for(inti=0;i<3;i++)cout<<"\t"<

cout<<"\t"<

}

ostream&operator<<(ostream&os,CStudentRec&stu)

{

os.write(&stu.chFlag,sizeof(char));

os.write(stu.strName,sizeof(stu.strName));

os.write(stu.strID,sizeof(stu.strID));

os.write((char*)stu.fScore,sizeof(float)*3;

os.write((char*)&stu.fAve,sizeof(float));

returnos;

}

istrean&operator>>(istream&is,CStudentRec&stu)

{

charname[20],id[10];

is.read(&stu.chFlag,sizeof(char));

is.read(name,sizeof(name));

is,read(id,sizeof(id));

is.read((char*)stu.fScore,sizeof(float)*3);

is.read((char*)&stu.fAve,sizeof(float));

strncpy(stu.strName,name,sizeof(name));

strncpy(stu.strID,id,sizeof(name));

returnis;

}

4.添加Ex_File.cpp文件,测试CStudentRec类

添加Ex_File.cpp文件,测试CStudentRec类的具体步骤如下。

(1)选择“文件”→“新建”菜单命令,将“新建”对话框切换到“文件”标签页面,选中“C++SourceFile”文件类型,在文件编辑框中输入Ex_File.cpp,单击“确定”按钮。

(2)在Ex_File.cpp中添加CStudentRec类的测试代码:

#include

#include”Ex_File.h”

voidmain()

{floatfScore[]={80,90,92};

CStudentRecrec1(“Ding”,”21050101”,fScore);

Rec1.Print(true);

CStudentRecrec2;

Rec2.Input();

Rec2.Print(true);

}

(3)编译运行并测试,CStudentRec类的测试结果如图T7.1所示。

试一试若将输入的学生记录保存在文件中,并从文件中读取记录,则这样的功能应如何实现?

5.添加CStuFile类代码

添加CStuFile类代码的具体步骤如下。

(1)将工作区窗口切换到FileView页面,展示所有结点,双击Ex_File.h结点。

(2)在Ex_File.h文件后面添加以下CStuFile类代码:

classCStuFile

{

public:

CStuFile(char*filename);

~CStuFile();

voidAdd(CStudentRec&stu);

intSeek(char*id,CStudentRec&stu);

intList(intnNum=-1);

private:

char*strFileName;

};

CStuFile:

:

:

CStuFile(char*filename)

{

strFileName=newchar[strlen(Filename)+1];

strcpy(strFileName,filename);

}

CStuFile:

:

~CStuFile()

{

if(strFileName)delete[]strFileName;

}

voidCStuFile:

:

Add(CStudentRecstu)

{

Fstreamfile(strFileName,ios:

:

outlios:

:

applios:

:

binary);

file<

file.close();

}

intCStuFile:

:

Seek(char*id,CStudentRec&stu)

{

intnRec=-1;

Fstreamfile(strFileName,ios:

:

inlios:

:

nocreate);

if(!

file)

{

cout<<”文件”<

\n”;

returnnRec;

}

inti=0;

while(!

file.eof())

{

file>>stu;

if((strcmp(id,stu.strID)==0)&&(stu.chFlag!

=’N’))

{nRec=I;break;

i++;

}

file.close();

returnnRec;

}

//列表显示nNum个记录,-1时全部显示,并返回文件中的记录数

intCstuFile:

List(intnNum)

{

Fstreamfile(strFileName,ios:

:

inlios:

:

nocreate);//打开文件用于只读

if(!

file)

{

cout<<”文件”<

\n”;

return0;

}

intnRec=0;

if((nNum==-1)!

!

(nNum>0))

{

cout_setf(ios:

:

left);

cout<

<<”\t成绩1\t成绩2\t成绩3\t平均分”<

}

while(!

file.eof())

{

CstudentRecdata;

file>>data;

if(data.chFlag==”A”)

{

nRec++;

if((nNum==-1)||(nRec<=nNum))

{

cout.setf(ios:

:

left);

cout<

data.Print();

}

}

}

file.close();

returnnRec;

}

6.添加CstuFile类的测试代码

添加CstuFile类的测试代码的具体步骤如下。

(1)在工作区窗口的FileView页面中,双击Ex_File.cpp结点。

(2)修改Ex_File.cpp文件的代码:

#include

#include“Ex_File.h”

CstuFiletheStu(“student.txt”);//定义一个全局对象

voidAddTo(intnNum)//输入多个记录

{

CstudentRecstu;

for(inti=0;i

{

cout<<”请输入第”<

”<

stu.Input();

theStu.Add(stu);

}

}

voidmain()

{

AddTo(3);

theStu.List();

Cstudentone;

if(heStu.Seek(“21050102”,one)>=0)

onePrint(true);

else

cout<<”没有找到!

\n”;

theStu.List();

}

(3)编译运行并测试,按运行的提示内容输入下列3哥记录数据:

MaWenTao21050101809085

LiMing21050102758183

YangYang21050103806576

想一想若上述程序在重新运行一次,且输入3个相同的记录数据,则运行的结果将会如何?

若在输入成绩时,故意输成字符或不在0~100的数直,则运行结果又将如何?

7.退出VisualC++6.0

8.写出实验报告

结合上述分析和试一试内容,写出实验报告。

思考与练习

若CStuFile类还能实现记录的修改、删除、排序功能(按平均分高低),则应如何编程?

(提示:

对于删除来说,由于文件中的记录删除需要移动大量数据,因此为避免这种情况发生,删除时只需将文件中要删除的标志成员chFlag变成N即可。

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

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

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2