C++语言程序设计实验报告 5Word文档下载推荐.docx
《C++语言程序设计实验报告 5Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C++语言程序设计实验报告 5Word文档下载推荐.docx(18页珍藏版)》请在冰点文库上搜索。
结果:
基础题二
1)在点类中设计友员函数,求两点间的距离。
1)
classPoint;
inthorizontalDistance(constPoint&
first,constPoint&
second);
intverticalDistance(constPoint&
classPoint
private:
intx;
inty;
Point(intx=0,inty=0)
{
this->
x=x;
y=y;
}
friendinthorizontalDistance(constPoint&
friendintverticalDistance(constPoint&
};
inthorizontalDistance(constPoint&
second)
if(first.x-second.x>
=0)
returnfirst.x-second.x;
else
returnsecond.x-first.x;
}
if(first.y-second.y>
returnfirst.y-second.y;
returnsecond.y-first.y;
{
Pointa(1,2);
Pointb(0,0);
Pointc(-1,-1);
"
ab间的竖直距离为:
horizontalDistance(a,b)<
ac间的竖直距离为:
horizontalDistance(a,c)<
ab间的水平距离为:
verticalDistance(a,b)<
ac间的水平距离为:
verticalDistance(a,c)<
基础题三
定义一个学生类,数据成员有:
总人数、姓名、性别、成绩等,设计程序要求能统计学生的总人数、修改成绩并打印成绩。
#include<
vector>
classStudent
intnAllNum;
charcName[5];
charcSex[2];
intnScore;
intnID;
Student()
nAllNum=0;
nScore=0;
protected:
voidmain()
vector<
Student>
Stu;
StudentTmpStu;
intnFlag=1;
do
cout<
请输入学生学号:
cin>
>
TmpStu.nID;
请输入学生姓名:
TmpStu.cName;
请输入学生性别:
TmpStu.cSex;
请输入学生分数:
TmpStu.nScore;
Stu.push_back(TmpStu);
继续输入?
(1继续,0停止)"
nFlag;
}while(nFlag!
=0);
intnAllNum=Stu.size();
intChangeID;
请输入想修改成绩的学生学号:
cin>
ChangeID;
intnFlag1=0;
for(inti=0;
i<
nAllNum;
i++)
if(Stu[i].nID==ChangeID)
{
cout<
输入修改后的分数。
cin>
Stu[i].nScore;
nFlag1=1;
break;
}
if(nFlag1!
=1)
未找到该学生!
FILE*fp=fopen("
Result.txt"
"
w"
);
fprintf(fp,"
总人数为:
%d\n"
nAllNum);
for(i=0;
fprintf(fp,"
%d\t"
Stu[i].nID);
%s\t"
Stu[i].cName);
Stu[i].cSex);
Stu[i].nScore);
统计结果在Result文件中!
fclose(fp);
提高题一
设计一个类,要求类中至少有一个友员函数,然后在主程序中访问友员函数。
iostream.h>
math.h>
{public:
Point(inta,intb){x=a;
y=b;
intx,y;
classLine
{inta,b,c;
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;
Pointp(10,10);
Linel(2,4,-3);
cout<
长度为"
length(l,p)<
提高题二
定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name()和display()等函数。
display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性。
实现并测试这个类。
classEmployee{
char*name,*address,*city,*postCode;
Employee(char*_name,char*_address,char*_city,char*_postCode)
name=_name;
address=_address;
city=_city;
postCode=_postCode;
voidchange_name(char*_name){
voiddisplay()
cout<
"
name:
<
name<
endl;
address:
address<
city:
city<
postcode:
postCode<
intmain(intargc,char*argv[])
Employee*e=newEmployee("
李雨阳"
华师"
武汉"
430079"
e->
display();
change_name("
懒洋洋"
deletee;
选做题:
设计一个用于人事管理的People(人员)类。
要求具有如下属性:
姓名charname[11]、编号charnumber[7]、性别charsex[3]、生日birthday、身份证号charid[16]。
其中“出生日期”定义为一个“日期”类内嵌对象。
用成员函数实现对人员信息的录入和显示。
要求包括:
构造函数和析构函数,拷贝构造函数、内联成员函数、聚集。
在测试程序中定义people类的对象数组,录入数据并显示。
#include"
iostream.h"
#ifndefDATE_H
#defineDATE_H
classDate
{private:
intyear,month,day;
Date():
year(0),month(0),day(0){}
Date(inty,intm,intd):
year(y),month(m),day(d){}
voidinput()
{cout<
请输入生日的年月日:
year>
month>
day;
}
voidprint()
生日:
year<
-"
month<
day<
voidset(inty,intm,intd)
{year=y;
month=m;
day=d;
#ifndefPEOPLE_H
#definePEOPLE_H
Date.h"
#include"
string.h"
classpeople
char*name;
intnumber;
char*id;
charsex;
Datebirthday;
staticintpsum;
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
people.h"
fstream.h"
assert.h>
intpeople:
psum=0;
char&
people:
operator[](intk)
charm[10]={"
assert(k>
=0&
&
k<
psum);
returnm[k];
people()
{name=newchar[9];
id=newchar[10];
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(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<
姓名:
name<
编号:
number<
性别:
sex<
身份证号:
id<
birthday.print();
input()
请输入姓名:
cin>
name;
请输入编号:
number;
请输入性别(男M,女W):
sex;
请输入身份证号:
id;
birthday.input();
people&
operator=(constpeople&
peo)
{if(&
peo!
=this)
delete[]name;
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;
n;
i++)
if(z==p[i].number)
您要找的对象有如下信息:
p[i].print();
查找完毕!
请继续操作!
break;
if(i==n)cout<
对不起,没有找到合适的对象。
voidsort(peoplep[],intn)
{inti,j;
peopletemp;
for(j=1;
j<
=n-1;
j++)
=n-1-j;
if(p[i].number>
p[i+1].number)
{temp=p[i];
p[i]=p[i+1];
p[i+1]=temp;
{p[i].print();
对people的排序完毕!
!
请继续操作"
ofstreamostrm;
ostrm.open("
people.dat"
if(!
ostrm)
people.datcan'
topen.\n"
for(i=0;
{ostrm<
人员信息有如下:
ostrm<
\n"
ostrm<
p[i].name<
p[i].number<
身份证号码:
p[i].id<
intoperator==(peoplea,peopleb)
{if(!
strcmp(a.getID(),b.getID()))return1;
elsereturn0;
voidpeople:
set(char*na,intnum,char*pid,chars,inty,intm,intd)
name=newchar[strlen(na)+1];
id=newchar[strlen(pid)+1];
for(inti=0;
4;
if(p[i].id==pid)
对不起!
您输入的编号出现重复,请重输入:
pid;
break;
number=num;
sex=s;
birthday.set(y,m,d);
ofstreamostrm;
ostrm.open("
ios:
app);
代码有错误·
·