丽水数理c实验报告二

上传人:沈*** 文档编号:71346102 上传时间:2022-04-07 格式:DOC 页数:24 大小:262.50KB
收藏 版权申诉 举报 下载
丽水数理c实验报告二_第1页
第1页 / 共24页
丽水数理c实验报告二_第2页
第2页 / 共24页
丽水数理c实验报告二_第3页
第3页 / 共24页
资源描述:

《丽水数理c实验报告二》由会员分享,可在线阅读,更多相关《丽水数理c实验报告二(24页珍藏版)》请在装配图网上搜索。

1、实验一 C+类的定义及成员引用一、实验目的:1. 掌握类和对象的概念及特性;2. 掌握C+类和对象的定义;3. 掌握对象成员的基本引用方法;4. 掌握简单的面向对象程序的编写;二、实验任务题目1 图形类设计设计三个图形类:Circle(圆)、Rectangle(矩形)、Triangle(三角形);1、Cirlce 类基本信息:圆心坐标、半径;Rectangle 类基本信息:长、宽;Triangle 类基本信息:三个顶点坐标;其中:成员变量为 private 属性,成员函数为public 属性;2、每个图形类有计算图形的面积GetArea(),显示图形的基本信息函数Show(),修改基本信息的函

2、数Set(形参)。以Circle 类为例:通过GetArea()计算圆的面积,Show()函数中显示圆心坐标、直径、周长、面积等基本信息;Set(int x,int y, int r)函数可以修改圆心坐标和半径。3、当输入数据为不合理数据时(例如:输入的三角形的顶点是否能组成一个三角形),提示用户输入错误;测试数据1、Circle 类测试数据:圆心:(20,30) 半径:5(-20,40) 30(40,-10) -52、Rectangle 类测试数据:顶点坐标:20,5040,5-9,1043,-83、Triangle 类测试数据:顶点:(20,30)、(40,50)、(40,20)(10,1

3、0)、(50,10)、(35,10)源程序:#include#include#includeusing namespace std;/圆的面积 class Circleprivate:int x;int y;int r;public:Circle ()int x=0;int y=0;int r=0;float Circle_GetArea();void Circle_show(); void Circle_set(int a,int b,int c)x=a;y=b;r=c;float Circle:Circle_GetArea()float s;s=3.14*r*r;return s;void

4、 Circle:Circle_show() float s;if(r0)cout(x,y)setw(6)rsetw(6)2*rsetw(6)2*3.14*rsetw(6)sendl;elsecout输入的数据错误!=b) a=b; if(a=c)a=c;if(b=c)b=c; if(x1-x4)*(x1-x2)+(y1-y4)*(y1-y2)=0&(x1-x2=x4-x3)&(y1-y2=y4-y3) length=a; width=b; ;float Rectangle: Rectangle_GetArea() float z;z=length*width;return z;void Rec

5、tangle:Rectangle_show() float z;if(z0)cout矩形的长:lengthsetw(2) 宽: widthsetw(2) 面积: zendl; elsecout输入数据错误!l3&l1+l3l2&l2+l3l1) d=(l1+l2+l3)/2; s=sqrt(d*(d-l1)*(d-l2)*(d-l3); else s=0;void Triangle_show();void Triangle_set(int x1,int y1,int x2,int y2,int x3,int y3) m1=x1;n1=y1;m2=x2;n2=y2;m3=x3;n3=y3; fl

6、oat a,b,c;a=sqrt(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);b=sqrt(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3); c=sqrt(x3-x2)*(x3-x2)+(y3-y2)*(y3-y2); l1=a; l2=b; l3=c; ;void Triangle:Triangle_show()float s;int c;c=!s;switch(c)case 0: cout三角形三点坐标;setw(2)(m1,n1)setw(2)(m2,n2)setw(2)(m3,n3)endl; cout三角形边长:setw(2)l1 l2 l3endl;

7、 cout三角形的面积:setw(2)sendl; break;case 1: cout输入数据错误!x1y1r1; cinx2y2r2;cinx3y3r3;cina1b1;cina2b2;cina3b3;cina4b4;cink1l1k2l2k3l3;cink4l4k5l5k6l6; 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)

8、;t2.Triangle_set(k4,l4,k5,l5,k6,l6);cout-圆-endl;cout圆心坐标setw(6)直径setw(6)半径setw(6)周长setw(6)面积endl;c1.Circle_GetArea();c1.Circle_show();c2.Circle_GetArea();c2.Circle_show();c3.Circle_GetArea();c3.Circle_show();cout-矩形-endl;r.Rectangle_GetArea();r.Rectangle_show();cout-三角形- endl;t1.Triangle_GetArea();t

9、1.Triangle_show();t2.Triangle_GetArea();t2.Triangle_show();return 0;运行结果: 题目2 学生类设计设计一个 Studnet(学生)类1、基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业;其中:基本信息为 private 属性,成员函数为public 属性;2、Student 类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数3、基本功能:1)可以从键盘输入学生的基本信息;2)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:班级、专业等;3)Show()函数显示学生信息;4) 当输入

10、数据为不合理数据时,提示用户输入错误;测试数据1、学号:2005001 姓名:zhangesan 出生日期:85 年11 月年级:05 级班级:jy01 院系:computer 专业:application2、学号:2005002 姓名:lihuan 出生日期:84 年11 月3、学号:2005003 姓名:wang 出生日期:85 年7 月年级:05 级班级:jy01 院系:computer 专业:software4、学号:2005004 姓名:zhan 出生日期:85 年11 月年级:05 级班级:jy025、学号:2005005 姓名:song 出生日期:85 年11 月思考与扩展采用什

11、么方式可以让学号自动按输入顺序生成?源程序代码:#include#include#includeusing namespace std;class Studentprivate:int number;string name;string year;string grade;string clas;string department;string profession;public:Student( )grade=05级; Student(string cla); /Student:Student(int num=2005001):number(num)void setInto( int b,

12、string b1,string b2,string b3,string b4,string b5,string b6) number=b;name=b1;year=b2;grade=b3;clas=b4;department=b5;profession=b6;void show();Student:Student(string cla)clas=cla; void Student:show() cout学号:numbersetw(6)姓名:namesetw(10)出生日期:yearsetw(6)年级:gradesetw(6)班级:classetw(6)院系:departmentsetw(6)

13、专业:professionendlendl; int main() Student stud5; int i,j,num;for(i=0,num=2005001;ia1a2a3a4a5a6;studi.setInto(num,a1, a2, a3,a4, a5,a6);studi.show(); return 0;操作结果:实验二 C+中的构造函数与析构函数一、实验目的:5. 掌握C+中使用构造函数创建对象;6. 掌握C+中使用析构函数释放对象;7. 掌握构造函数的重载特性;8. 掌握类相关的指针及数组的使用方法;9. 掌握const成员及static成员的特性;10. 掌握友元的特性。二、实

14、验任务1. 人员管理设计某小型公司的 employee(人员)类(1)类1)employee 类:基本信息:编号、姓名、性别、出生日期、职位等;出生日期使用自定义的 Date(日期)类;其中:基本信息为 private 属性,成员函数为public 属性;多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造函数;可以从外部访问类成员的友员函数;2)Date 类:成员变量:年、月、日成员函数:SetYear(int year)、SetMonth(int month)、SetDay(int day)GetYear()、GetMonth()、GetDay()(2)基本功能:1)职工信息的录

15、入;2)职工信息的显示;3)用对象数组保存已输入的职工对象;4)可以修改人员的基本信息,如:姓名、职位等;5)可以通过编号或姓名进行人员查询;(3)实验提示1)注意带参数的构造函数和带默认参数的构造函数的声明与定义;2)定义employee 类的成员变量时,应注意变量类型的声明;3)在查询时,通过申明成employee 类的友元函数来访问类的成员变量;*/#include #include #define N 100using namespace std;class Dateprivate: int year; int month; int day;public: void SetYear(i

16、nt x) year=x; void SetMonth(int x) month=x; void SetDay(int x) day=x; int GetYear() return year; int GetMonth() return month; int GetDay() return day; Date():year(2000),month(1),day(1) Date(int x, int y,int z) year=x; month=y; day=z; ;class employeeprivate: int no; char name16; char sex8; Date birth

17、; char pos32;public: void Setname(char* n) strcpy(name,n); void Setpos(char* n) strcpy(pos,n); employee():no(0),birth(2000,1,1) strcpy(name,Andy); strcpy(sex,male); strcpy(pos,intern); employee& operator=(employee& x) no=x.no; birth.SetYear(x.birth.GetYear(); birth.SetMonth(x.birth.GetMonth(); birth

18、.SetDay(x.birth.GetDay(); strcpy(name,x.name); strcpy(sex,x.sex); strcpy(pos,x.pos); employee(int n, char* str, char* y,Date day, char* p) no = n; strcpy(name,str); strcpy(sex,y); birth=day; strcpy(pos,p); friend string Getname(employee x) string name; int i; for(i=0;x.namei!=0;i+) name+=x.namei; re

19、turn name; friend int Getno(employee x) return x.no; friend string Getsex(employee x) string sex; int i; for(i=0;x.sexi!=0;i+) sex+=x.sexi; return sex; friend Date Getbirth(employee x) return x.birth; friend string Getpos(employee x) string pos; int i; for(i=0;x.posi!=0;i+) pos+=x.posi; return pos;

20、;void main() employee *personsN; employee temp; Date day; int num=0; int i,choose=1,choose1=0; int n,y,m,d; char str16; char s8; char p32; coutntt=endl; couttt1:录入 2:显示 3:修改 4:查找 0:退出endl; couttt=endl; coutchoose; while(choose!=0) switch(choose) case 1: if(num=15) coutfull!n; continue; coutn; coutym

21、d; coutstr; couts; coutp; personsnum+=new employee(n,str,s,Date(y,m,d),p); break; case 2: cout编号t姓名t性别t生日tt职务endl; for(i=0;inum;i+) coutGetno(*personsi)tGetname(*personsi)tGetsex(*personsi)tGetbirth(*personsi).GetYear() Getbirth(*personsi).GetMonth() Getbirth(*personsi).GetDay()tGetpos(*personsi)end

22、l; break; case 3: coutn; for(i=0;i=num;i+) if(n=Getno(*personsi) coutnn1:名字修改 2:职位修改endl; cout选择您要进行的修改方式:choose1; switch(choose1) case 1: coutstr; personsnum-1=new employee(n,str,s,Date(y,m,d),p); break; case 2: coutp; personsnum-1=new employee(n,str,s,Date(y,m,d),p); break; break; else cout该员工不存在e

23、ndl; break; case 4: coutn; for(i=0;i=num;i+) if(n=Getno(*personsi) cout编号t姓名t性别t生日tt职务endl; coutGetno(*personsi)tGetname(*personsi)tGetsex(*personsi)tGetbirth(*personsi).GetYear() Getbirth(*personsi).GetMonth() Getbirth(*personsi).GetDay()tGetpos(*personsi)endl; break; break; coutntt=endl; couttt1:录入 2:显示 3:修改 4:查找 0:退出endl; couttt=endl; coutchoose; 操作结果: = 1:录入 2:显示 3:修改 4:查找 0:退出 =输入您要进行的操作:

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!