C++语言程序设计实验报告 5Word文档下载推荐.docx

上传人:b****2 文档编号:5131241 上传时间:2023-05-04 格式:DOCX 页数:18 大小:39.90KB
下载 相关 举报
C++语言程序设计实验报告 5Word文档下载推荐.docx_第1页
第1页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第2页
第2页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第3页
第3页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第4页
第4页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第5页
第5页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第6页
第6页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第7页
第7页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第8页
第8页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第9页
第9页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第10页
第10页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第11页
第11页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第12页
第12页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第13页
第13页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第14页
第14页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第15页
第15页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第16页
第16页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第17页
第17页 / 共18页
C++语言程序设计实验报告 5Word文档下载推荐.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C++语言程序设计实验报告 5Word文档下载推荐.docx

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

C++语言程序设计实验报告 5Word文档下载推荐.docx

结果:

基础题二

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);

代码有错误·

·

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

当前位置:首页 > 党团工作 > 入党转正申请

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

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