C++语言程序设计实验报告 5.docx

上传人:b****5 文档编号:11750532 上传时间:2023-03-31 格式:DOCX 页数:18 大小:39.90KB
下载 相关 举报
C++语言程序设计实验报告 5.docx_第1页
第1页 / 共18页
C++语言程序设计实验报告 5.docx_第2页
第2页 / 共18页
C++语言程序设计实验报告 5.docx_第3页
第3页 / 共18页
C++语言程序设计实验报告 5.docx_第4页
第4页 / 共18页
C++语言程序设计实验报告 5.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

C++语言程序设计实验报告 5.docx

《C++语言程序设计实验报告 5.docx》由会员分享,可在线阅读,更多相关《C++语言程序设计实验报告 5.docx(18页珍藏版)》请在冰豆网上搜索。

C++语言程序设计实验报告 5.docx

C++语言程序设计实验报告5

C++语言程序设计实验报告

基础题一

实验任务:

定义一个Cat类,拥有静态数据成员HowManyCats,记录Cat的个体数目;静态成员函数GetHowMany(),存取HowManyCats。

设计程序测试这个类,体会静态数据成员和静态成员函数的用法。

代码:

#include

usingnamespacestd;

classcat

{

public:

cat();

virtual~cat();

public:

floatweight;

staticinthowmanycats;

public:

voidprint();

}

;

intcat:

:

howmanycats=0;

cat:

:

cat()

{

weight=0;

howmanycats++;

}

cat:

:

~cat()

{

}

voidcat:

:

print()

{

cout<

}

intmain()

{

cata;

a.print();

catb;

b.print();

catc;

c.print();

return0;

}

结果:

基础题二

实验任务:

1)在点类中设计友员函数,求两点间的距离。

代码:

1)

#include

usingnamespacestd;

classPoint;

inthorizontalDistance(constPoint&first,constPoint&second);

intverticalDistance(constPoint&first,constPoint&second);

classPoint

{

private:

intx;

inty;

public:

Point(intx=0,inty=0)

{

this->x=x;

this->y=y;

}

friendinthorizontalDistance(constPoint&first,constPoint&second);

friendintverticalDistance(constPoint&first,constPoint&second);

};

inthorizontalDistance(constPoint&first,constPoint&second)

{

if(first.x-second.x>=0)

returnfirst.x-second.x;

else

returnsecond.x-first.x;

}

intverticalDistance(constPoint&first,constPoint&second)

{

if(first.y-second.y>=0)

returnfirst.y-second.y;

else

returnsecond.y-first.y;

}

intmain()

{

Pointa(1,2);

Pointb(0,0);

Pointc(-1,-1);

cout<<"ab间的竖直距离为:

"<

cout<<"ac间的竖直距离为:

"<

cout<<"ab间的水平距离为:

"<

cout<<"ac间的水平距离为:

"<

return0;

}

结果:

基础题三

实验任务:

定义一个学生类,数据成员有:

总人数、姓名、性别、成绩等,设计程序要求能统计学生的总人数、修改成绩并打印成绩。

代码:

#include

#include

usingnamespacestd;

classStudent

{

public:

intnAllNum;

charcName[5];

charcSex[2];

intnScore;

intnID;

Student()

{

nAllNum=0;

nScore=0;

}

protected:

private:

};

voidmain()

{

vectorStu;

StudentTmpStu;

intnFlag=1;

do

{

cout<<"请输入学生学号:

"<

cin>>TmpStu.nID;

cout<<"请输入学生姓名:

"<

cin>>TmpStu.cName;

cout<<"请输入学生性别:

"<

cin>>TmpStu.cSex;

cout<<"请输入学生分数:

"<

cin>>TmpStu.nScore;

Stu.push_back(TmpStu);

cout<<"继续输入?

(1继续,0停止)"<

cin>>nFlag;

}while(nFlag!

=0);

intnAllNum=Stu.size();

intChangeID;

cout<<"请输入想修改成绩的学生学号:

"<

cin>>ChangeID;

intnFlag1=0;

for(inti=0;i

{

if(Stu[i].nID==ChangeID)

{

cout<<"输入修改后的分数。

"<

cin>>Stu[i].nScore;

nFlag1=1;

break;

}

}

if(nFlag1!

=1)

{

cout<<"未找到该学生!

"<

}

FILE*fp=fopen("Result.txt","w");

fprintf(fp,"总人数为:

%d\n",nAllNum);

for(i=0;i

{

fprintf(fp,"%d\t",Stu[i].nID);

fprintf(fp,"%s\t",Stu[i].cName);

fprintf(fp,"%s\t",Stu[i].cSex);

fprintf(fp,"%d\n",Stu[i].nScore);

}

cout<<"统计结果在Result文件中!

"<

fclose(fp);

}

结果:

提高题一

实验任务:

设计一个类,要求类中至少有一个友员函数,然后在主程序中访问友员函数。

代码:

#include

#include

classPoint

{public:

Point(inta,intb){x=a;y=b;}

intx,y;

};

classLine

{inta,b,c;

public:

Line(intx,inty,intz){a=x;b=y;c=z;}

frienddoublelength(Linel,Pointp)

{

doubled;

d=abs((l.a*p.x*p.x+l.b*p.x+l.c)/sqrt(l.a*l.a+l.b*l.b));

returnd;

}

};

voidmain()

{

Pointp(10,10);

Linel(2,4,-3);

cout<<"长度为"<

}

结果:

提高题二

实验任务:

定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。

display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性。

实现并测试这个类。

代码:

#include

usingnamespacestd;

classEmployee{

private:

char*name,*address,*city,*postCode;

public:

Employee(char*_name,char*_address,char*_city,char*_postCode)

{

name=_name;

address=_address;

city=_city;

postCode=_postCode;

}

voidchange_name(char*_name){

name=_name;

}

voiddisplay()

{

cout<<"name:

"<

cout<<"address:

"<

cout<<"city:

"<

cout<<"postcode:

"<

}

};

intmain(intargc,char*argv[])

{

Employee*e=newEmployee("李雨阳","华师","武汉","430079");

e->display();

e->change_name("懒洋洋");

e->display();

deletee;

}

结果:

选做题:

设计一个用于人事管理的People(人员)类。

要求具有如下属性:

姓名charname[11]、编号charnumber[7]、性别charsex[3]、生日birthday、身份证号charid[16]。

其中“出生日期”定义为一个“日期”类内嵌对象。

用成员函数实现对人员信息的录入和显示。

要求包括:

构造函数和析构函数,拷贝构造函数、内联成员函数、聚集。

在测试程序中定义people类的对象数组,录入数据并显示。

代码:

#include"iostream.h"

#ifndefDATE_H

#defineDATE_H

classDate

{private:

intyear,month,day;

public:

Date():

year(0),month(0),day(0){}

Date(inty,intm,intd):

year(y),month(m),day(d){}

voidinput()

{cout<<"请输入生日的年月日:

"<

cin>>year>>month>>day;

}

voidprint()

{cout<<"生日:

"<

voidset(inty,intm,intd)

{year=y;

month=m;

day=d;}

};

#ifndefPEOPLE_H

#definePEOPLE_H

#include"Date.h"

#include"iostream.h"

#include"string.h"

classpeople

{private:

char*name;

intnumber;

char*id;

charsex;

Datebirthday;

staticintpsum;

public:

people();

people(char*,int,char*,char,int,int,int);

people(constpeople&init);

virtual~people(){delete[]name;delete[]id;}

inlinevoidprint();

inlinevoidinput();

intgetnum()

{returnnumber;}

char*getname()

{returnname;}

char*getID()

{returnid;}

friendvoidsearch(peoplep[],intnum,intn);

friendvoidsort(peoplep[],intn=4);

friendintoperator==(peoplea,peopleb);

people&operator=(constpeople&peo);

staticintgetpsum(){returnpsum;}

voidset(char*,int,char*,char,int,int,int);

char&operator[](intk);

};

#endif

#include"iostream.h"

#include"people.h"

#include"fstream.h"

#include

intpeople:

:

psum=0;

char&people:

:

operator[](intk)

{

charm[10]={""};

assert(k>=0&&k

:

psum);

returnm[k];

}

people:

:

people()

{name=newchar[9];

id=newchar[10];

}

people:

:

people(char*na,intnum,char*pid,chars,inty,intm,intd):

birthday(y,m,d),number(num),sex(s)

{name=newchar[strlen(na)+1];

strcpy(name,na);

id=newchar[strlen(pid)+1];

strcpy(id,pid);

psum++;

}

people:

:

people(constpeople&init)

{delete[]name;

delete[]id;

name=newchar[strlen(init.name)+1];

strcpy(name,init.name);

id=newchar[strlen(init.id)+1];

strcpy(id,init.id);

number=init.number;

birthday=init.birthday;

}

voidpeople:

:

print()

{cout<<"姓名:

"<

"<

"<

"<

birthday.print();

}

voidpeople:

:

input()

{cout<<"请输入姓名:

";cin>>name;

cout<<"请输入编号:

";cin>>number;

cout<<"请输入性别(男M,女W):

";cin>>sex;

cout<<"请输入身份证号:

";cin>>id;

birthday.input();

}

people&people:

:

operator=(constpeople&peo)

{if(&peo!

=this)

delete[]name;

delete[]id;

name=newchar[strlen(peo.name)+1];

strcpy(name,peo.name);

id=newchar[strlen(peo.id)+1];

strcpy(id,peo.id);

number=peo.number;

birthday=peo.birthday;

return*this;

}

voidsearch(peoplep[],intnum,intn)

{inti,z;

z=num;

for(i=0;i

if(z==p[i].number)

{cout<<"您要找的对象有如下信息:

"<

p[i].print();

cout<<"查找完毕!

请继续操作!

"<

break;

}

if(i==n)cout<<"对不起,没有找到合适的对象。

"<

}

voidsort(peoplep[],intn)

{inti,j;

peopletemp;

for(j=1;j<=n-1;j++)

for(i=0;i<=n-1-j;i++)

if(p[i].number>p[i+1].number)

{temp=p[i];p[i]=p[i+1];p[i+1]=temp;}

for(i=0;i

{p[i].print();}

cout<<"对people的排序完毕!

请继续操作"<

ofstreamostrm;

ostrm.open("people.dat");

if(!

ostrm)

{cout<<"people.datcan'topen.\n";

}

for(i=0;i

{ostrm<<"人员信息有如下:

"<

ostrm<<"\n";

ostrm<<"姓名:

"<

ostrm<<"编号:

"<

ostrm<<"身份证号码:

"<

}

}

intoperator==(peoplea,peopleb)

{if(!

strcmp(a.getID(),b.getID()))return1;

elsereturn0;

}

voidpeople:

:

set(char*na,intnum,char*pid,chars,inty,intm,intd)

{

psum++;

name=newchar[strlen(na)+1];

strcpy(name,na);

id=newchar[strlen(pid)+1];

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

if(p[i].id==pid)

{cout<<"对不起!

您输入的编号出现重复,请重输入:

";

cin>>pid;

delete[]id;

id=newchar[strlen(pid)+1];break;

}

strcpy(id,pid);

number=num;

sex=s;

birthday.set(y,m,d);

ofstreamostrm;

ostrm.open("people.dat",ios:

:

app);

ostrm<

ostrm<<"姓名:

"<

ostrm<<"编号:

"<

ostrm<<"身份证号码:

"<

}

代码有错误·····

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

当前位置:首页 > 人文社科 > 哲学历史

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

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