丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx

上传人:b****5 文档编号:20354713 上传时间:2023-01-22 格式:DOCX 页数:37 大小:101.03KB
下载 相关 举报
丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx_第1页
第1页 / 共37页
丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx_第2页
第2页 / 共37页
丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx_第3页
第3页 / 共37页
丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx_第4页
第4页 / 共37页
丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx_第5页
第5页 / 共37页
点击查看更多>>
下载资源
资源描述

丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx

《丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx》由会员分享,可在线阅读,更多相关《丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx(37页珍藏版)》请在冰豆网上搜索。

丽水学院期末C++复习 信息09 史杨杨Word文档格式.docx

intx=0;

inty=0;

intr=0;

}

floatCircle_GetArea();

voidCircle_show();

voidCircle_set(inta,intb,intc)

x=a;

y=b;

r=c;

}

};

floatCircle:

:

Circle_GetArea()

floats;

s=3.14*r*r;

returns;

voidCircle:

Circle_show()

{floats;

if(r>

0)

cout<

<

"

("

x<

"

y<

)"

setw(6)<

r<

2*r<

2*3.14*r<

s<

endl;

else

输入的数据错误!

//矩形的面积

classRectangle

intlength;

intwidth;

public:

Rectangle()

intlength=0;

intwidth=0;

floatRectangle_GetArea();

voidRectangle_show();

voidRectangle_set(intx1,inty1,intx2,inty2,intx3,inty3,intx4,inty4)

inta,b,c,t;

a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));

c=sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));

if(a>

=b)

{a=b;

}

=c)

{

a=c;

if(b>

{

b=c;

}

if((x1-x4)*(x1-x2)+(y1-y4)*(y1-y2)==0&

&

(x1-x2==x4-x3)&

(y1-y2==y4-y3))

{length=a;

width=b;

floatRectangle:

Rectangle_GetArea()

{floatz;

z=length*width;

returnz;

voidRectangle:

Rectangle_show()

if(z>

矩形的长:

length<

setw

(2)<

宽:

"

width<

面积:

z<

输入数据错误!

//三角形的面积

classTriangle

intm1,n1,m2,n2,m3,n3;

floatl1,l2,l3;

Triangle()

intm1=0,n1=0,m2=0,n2=0,m3=0,n3=0;

floatl1=0,l2=0,l3=0;

floatTriangle_GetArea()

{floats,d;

if(l1+l2>

l3&

l1+l3>

l2&

l2+l3>

l1)

d=(l1+l2+l3)/2;

s=sqrt(d*(d-l1)*(d-l2)*(d-l3));

else

s=0;

voidTriangle_show();

voidTriangle_set(intx1,inty1,intx2,inty2,intx3,inty3)

{m1=x1;

n1=y1;

m2=x2;

n2=y2;

m3=x3;

n3=y3;

floata,b,c;

c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));

l1=a;

l2=b;

l3=c;

};

voidTriangle:

Triangle_show()

intc;

c=!

s;

switch(c)

case0:

三角形三点坐标;

m1<

n1<

m2<

n2<

m3<

n3<

cout<

三角形边长:

l1<

l2<

l3<

三角形的面积:

break;

case1:

break;

intmain()

{

Circlec1,c2,c3;

Rectangler;

Trianglet1,t2;

intx1,x2,x3;

inty1,y2,y3;

intr1,r2,r3;

inta1,b1,a2,b2,a3,b3,a4,b4;

intk1,l1,k2,l2,k3,l3;

intk4,l4,k5,l5,k6,l6;

cin>

>

x1>

y1>

r1;

x2>

y2>

r2;

x3>

y3>

r3;

a1>

b1;

a2>

b2;

a3>

b3;

a4>

b4;

k1>

l1>

k2>

l2>

k3>

l3;

k4>

l4>

k5>

l5>

k6>

l6;

c1.Circle_set(x1,y1,r1);

c2.Circle_set(x2,y2,r2);

c3.Circle_set(x3,y3,r3);

r.Rectangle_set(a1,b1,a2,b2,a3,b3,a4,b4);

t1.Triangle_set(k1,l1,k2,l2,k3,l3);

t2.Triangle_set(k4,l4,k5,l5,k6,l6);

--------------------------------圆----------------------------------"

cout<

圆心坐标"

直径"

半径"

周长"

面积"

c1.Circle_GetArea();

c1.Circle_show();

c2.Circle_GetArea();

c2.Circle_show();

c3.Circle_GetArea();

c3.Circle_show();

----------------------------------矩形-------------------------------"

r.Rectangle_GetArea();

r.Rectangle_show();

-----------------------------三角形------------------------------------"

<

t1.Triangle_GetArea();

t1.Triangle_show();

t2.Triangle_GetArea();

t2.Triangle_show();

return0;

}运行结果:

题目2学生类设计

设计一个Studnet(学生)类

1、基本信息:

学号、姓名、性别、出生日期、年级、班级、院系、专业;

基本信息为private属性,成员函数为public属性;

2、Student类有多个构造函数:

缺省构造函数、带参数的构造函数、带默认参数的构造

函数

3、基本功能:

1)可以从键盘输入学生的基本信息;

2)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:

班级、专业等;

3)Show()函数显示学生信息;

4)当输入数据为不合理数据时,提示用户输入错误;

1、学号:

2005001姓名:

zhangesan出生日期:

85年11月年级:

05级

班级:

jy01院系:

computer专业:

application

2、学号:

2005002姓名:

lihuan出生日期:

84年11月

3、学号:

2005003姓名:

wang出生日期:

85年7月年级:

software

4、学号:

2005004姓名:

zhan出生日期:

jy02

5、学号:

2005005姓名:

song出生日期:

85年11月

[思考与扩展]

采用什么方式可以让学号自动按输入顺序生成?

源程序代码:

string>

classStudent

intnumber;

stringname;

stringyear;

stringgrade;

stringclas;

stringdepartment;

stringprofession;

Student()

grade="

05级"

;

}

Student(stringcla);

//Student:

Student(intnum=2005001):

number(num){}

voidsetInto(intb,stringb1,stringb2,stringb3,stringb4,stringb5,stringb6)

{

number=b;

name=b1;

year=b2;

grade=b3;

clas=b4;

department=b5;

profession=b6;

voidshow();

Student:

Student(stringcla)

clas=cla;

voidStudent:

show()

cout<

学号:

number<

姓名:

name<

setw(10)<

出生日期:

year<

年级:

grade<

班级:

clas<

院系:

department<

专业:

profession<

endl<

Studentstud[5];

inti,j,num;

for(i=0,num=2005001;

i<

5;

i++,num++)

{stringa1,a2,a3,a4,a5,a6;

cin>

a5>

a6;

stud[i].setInto(num,a1,a2,a3,a4,a5,a6);

stud[i].show();

 

实验二C++中的构造函数与析构函数

5.掌握C++中使用构造函数创建对象;

6.掌握C++中使用析构函数释放对象;

7.掌握构造函数的重载特性;

8.掌握类相关的指针及数组的使用方法;

9.掌握const成员及static成员的特性;

10.掌握友元的特性。

题目人员管理

设计某小型公司的employee(人员)类

(1)类

1)employee类:

基本信息:

编号、姓名、性别、出生日期、职位等;

出生日期使用自定义的Date(日期)类;

多个构造函数:

缺省构造函数、带参数的构造函数、带默认参数的构造函数;

可以从外部访问类成员的友员函数;

2)Date类:

成员变量:

年、月、日

成员函数:

SetYear(intyear)、SetMonth(intmonth)、SetDay(intday)

GetYear()、GetMonth()、GetDay()

(2)基本功能:

1)职工信息的录入;

2)职工信息的显示;

3)用对象数组保存已输入的职工对象;

4)可以修改人员的基本信息,如:

姓名、职位等;

5)可以通过编号或姓名进行人员查询;

(3)实验提示

1)注意带参数的构造函数和带默认参数的构造函数的声明与定义;

2)定义employee类的成员变量时,应注意变量类型的声明;

3)在查询时,通过申明成employee类的友元函数来访问类的成员变量;

[实验提示]

1、注意带参数的构造函数和带默认参数的构造函数的声明与定义;

2、定义employee类的成员变量时,应注意变量类型的声明;

3、在查询时,通过申明成employee类的友元函数来访问类的成员变量;

1、编号:

10001姓名:

jack出生日期:

80年11月1日职位:

普通

2、编号:

10002姓名:

andy出生日期:

75年1月9日职位:

经理

3、编号:

10003姓名:

alex出生日期:

81年4月3日职位:

秘书

4、编号:

10005姓名:

lili出生日期:

82年10月1日职位:

技师

源代码:

#include<

#include<

#defineN100

classDate

private:

intyear;

intmonth;

intday;

public:

voidSetYear(intx){year=x;

voidSetMonth(intx){month=x;

voidSetDay(intx){day=x;

intGetYear(){returnyear;

intGetMonth(){returnmonth;

intGetDay(){returnday;

Date():

year(2000),month

(1),day

(1){}

Date(intx,inty,intz)

year=x;

month=y;

day=z;

classemployee

intno;

stringsex;

Datebirth;

stringpos;

employee():

no(0),birth(2000,1,1)

name="

Andy"

sex="

male"

pos="

intern"

employee(intn,stringstr,stringy,Dateday,stringp)

no=n;

name=str;

sex=y;

birth=day;

pos=p;

friendstringGetname(employeex)

stringname1;

name1=x.name;

returnname1;

friendintGetno(employeex)

returnx.no;

friendstringGetsex(employeex)

stringsex1;

sex1=x.sex;

returnsex1;

friendDateGetbirth(employeex)

returnx.birth;

friendstringGetpos(employeex)

stringpos1;

pos1=x.pos;

returnpos1;

friendvoiddisplay()

\n\t\t======================================="

\t\t1:

录入2:

显示3:

修改4:

查找0:

退出"

\t\t======================================="

\n输入您要进行的操作:

employee*persons[N];

employeetemp;

Dateday;

intnum=0;

inti,choose=1,choose1=0;

intn,y,m,d;

stringstr;

strings;

stringp;

display();

choose;

while(choose!

=0)

switch(choose)

case1:

if(num==100)

full!

\n"

continue;

输入员工编号:

n;

输入员工生日:

y>

m>

d;

输入员工姓名:

str;

输入员工性别:

输入员工职位:

p;

persons[num++]=newemployee(n,str,s,Date(y,m,d),p);

break;

case2:

编号\t"

姓名\t"

性别\t"

生日\t\t"

职务"

for(i=0;

num;

i++)

Getno(*persons[i])<

\t"

Getname(*persons[i])<

Getsex(*persons[i])<

Getbirth(*persons[i]).GetYear()

年"

Getbirth(*persons[i]).GetMonth()<

月"

Getbirth(*persons[i]).GetDay()<

日"

Getpos(*persons[i])<

case3:

输入员工编号:

=num-1;

if(n==Getno(*persons[i]))

{cout<

\n\t\t=================================="

名字修改2:

职位修改"

\t\t===================================="

选择您要进行的修改方式:

choose1;

switch(choose1)

输入员工姓名:

n=Getno(*persons[i]);

s=Getsex(*persons[i]);

y=Getbirth(*persons[i]).GetYear();

m=Getbirth(*persons[i]).GetMonth();

d=Getbirth(*persons[i]).GetDay();

p=Getpos(*persons[i]);

deletepersons[i];

persons[i]=newemployee(n,str,s,Date(y,m,d),p);

输入员工职位:

n=Getno(*

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

当前位置:首页 > 高等教育 > 研究生入学考试

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

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