C语言程序设计

上传人:仙*** 文档编号:82406090 上传时间:2022-04-29 格式:DOC 页数:32 大小:900.04KB
收藏 版权申诉 举报 下载
C语言程序设计_第1页
第1页 / 共32页
C语言程序设计_第2页
第2页 / 共32页
C语言程序设计_第3页
第3页 / 共32页
资源描述:

《C语言程序设计》由会员分享,可在线阅读,更多相关《C语言程序设计(32页珍藏版)》请在装配图网上搜索。

1、 计算机科学与技术系上机实验报告课 程:C+语言程序设计老 师:姓 名:(老挝留学生)班 级:计科 101 班学 号:学 院:计算机科学与信息学院 实验日期: 年 月 日实验一一、实验名称类和对象二、实验目的及要求设计一个类,并对其属性进行操作。三、实验环境Microsoft Visual Studio 2010四、实验内容1,定义一个dog类,包含age,weight等属性。以及对这些属性的操作方法。实现并测试这个类。2,设计一个rectangle类,其属性为矩形的左下角与右上角的坐标,根据坐标计算矩形的面积。五、算法描述及实验步骤Dog+Dog(n: string , ag: int ,w

2、e: int)+get()+show()+Dog()-name: string-weight: int-age: intRectangle+get(): void+show(): void-X1:int-X2: int-Y1: int-Y2: int六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:the message of dog is:name:tutu age:2 weight:20input the name age and weight of dog花花 3 60the message of dog is:name:花花 age:3 weigh

3、t:60the message of dog is:name:hua age:4 weight:60calledcalled请按任意键继续. . .输入左下角的坐标:3 6输入右上角的坐标:4 7两点的坐标左下角的坐标:(3,6)右上角的坐标:(4,7)面积为:1输入左下角的坐标:1 0输入右上角的坐标:2 6两点的坐标左下角的坐标:(1,0)右上角的坐标:(2,6)面积为:6请按任意键继续. . .七、总结1,构造函数用于对对象的初始化,在定义类时,如果没有定义构造函数,系统将自动生成一个简单的构造函数。2,构造函数没有返回值,不允许显示调用,创建对象时,系统将自动调用相应的构造函数。3,析

4、构函数是对对象进行最后的清理工作,它不允许有参数,没有返回值。如果没有定义析构函数,系统将自动生成。4,对象的私有成员,可以通过成员函数访问,类外不能访问对象的私有成员。八、附录1, Dog.h#include#includeusing namespace std;class Dogpublic:Dog(string n,int ag,int we);void get();void show();Dog()coutcalled endl;private:string name;int weight;int age;Dog.cpp#include4-8.hDog:Dog(string n,int

5、 ag,int we):name(n),age(ag),weight(we)void Dog:get()coutinput the name age and weight of dognameageweight;void Dog:show()coutthe message of dog is:endl;coutname:name age:”age” weight:”weightendl;Dogmain.cpp#include4-8.hint main()Dog dog(tutu,2,20);dog.show();dog.get();dog.show();Dog dog1(hua,4,60);d

6、og1.show();return 0;2, rectangle.h#include#includeusing namespace std;class Rectanglepublic:void get();void show();private:int x1,x2,y1,y2;Rectangle.cpp#include4-9.hvoid Rectangle:get()cout输入左下角的坐标:x1y1;cout输入右上角的坐标:x2y2;void Rectangle:show()cout两点的坐标endl;cout左下角的坐标:(x1,y1)endl;cout右上角的坐标:(x2,y2)end

7、l;cout面积为:(x2-x1)*(y2-y1)endl;Rectangle main.cpp#include4-9.hint main()Rectangle r1;r1.get();r1.show();Rectangle r2;r2.get();r2.show();return 0;实验二一、实验名称类与对象二、实验目的及要求熟悉设计简单类及测试的方法。三、实验环境Microsoft Visual Studio 2010四、实验内容1,设计一个人事管理类,其属性有编号、性别、出生年月、身份证号等其中出生年月声明为一个日期类内嵌子对象。实现对成员信息的录入和输出。要求包括:构造函数析构函数、

8、复制构造函数、内联成员函数、带默认形参值的成员函数、类的组合。2,定义一个复数类complex,是下面的程序代码能够工作:Complex c1(3,5); /用复数5+3i初始化c1Complex c2=4.5; /用实数4.5初始化c2c1.add(c2); /将c1与c2相加,结果保留在c1中C1.show(); /将c1输出,结果应为7.5+5i五、算法描述及实验步骤1,Date+date(y:int=0,m:int=0,d:int=0)+get():void+show():void+date()-year:int-month:int -day:intHumain+humain()+hu

9、main(p: &humain)+get(p :&humain):void+show(p:&humain):void-berthday:date-name:string-CD:string-six:string-number:string2,Complex+complex(a:float=0,b:float=0)+add(p:&complex):void+show(p:&complex):void-real:float-imag:float六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:the first one before setname:six:C

10、D:number:berthday:0/0/0the first one after setinput the name six CD and number:name:喻福松six:男CD:5210452015number:1008060028input berthday:input year month and day1990 5 6name:喻福松six:男CD:5210452015number:1008060028berthday:1990/5/6p2name:喻福松six:男CD:5210452015number:1008060028berthday:1990/5/6called da

11、tecalled date请按任意键继续. . . number:7.5+5i请按任意键继续.七、总结1,类的组合 当当前类所使用的类还没有定义时,在当前类中只能建立此类的引用。如果已经定义在前,就可以直接建立对象。2,访问子对象中的可访问成员用“当前类子对象成员”的形式。3,内联成员函数:用关键字“inline”声明,在程序调用内联成员函数时,并不真正执行函数的调用过程,如保留返回地址等处理,而是把函数代码嵌入程序的调用点,这样可以减少调用成员函数的时间。4,带默认参数的函数: 由于函数调用时,实参和形参的结合是从左到右顺序进行的,因此指定默认值的参数必须放在参数列表的最右端。八、附录1,h

12、umainh 文件#include#includeusing namespace std;class datepublic:date(int y=0,int m=0,int d=0);void get ();void show();date()cout called dateendl;private:int year;int month;int day;lass humainpublic:humain();humain(humain &p);void get(humain &p);void show(humain &p);private:date berthday;string name;st

13、ring CD;string six;string number;Humaincpp文件#include4-10.hdate:date(int y,int m,int d):year(y),month(m),day(d)void inline date:get() coutinput year month and dayyearmonthday;void date:show() coutyear/month/dayendl;humain:humain()name= ;CD= ;six= ;number=;humain:humain(humain &p)berthday=p.berthday;n

14、ame=p.name;CD=p.CD;six=p.six;number=p.number;void humain:get(humain &p)coutinput the name six CD and number:endl;coutname;coutsix;coutCD;coutnumber;coutinput berthday:;p.berthday.get();void humain:show(humain &p)coutname:nameendl;cout six:sixendlCD:CDendlnumber:numberendl;coutberthday:;p.berthday.sh

15、ow();Humain maincpp文件#include4-10.hint main()humain p1;coutthe frist one befor setendl;p1.show(p1);coutthe frist one after setendl;p1.get(p1);p1.show(p1);humain p2(p1);coutp2endl;p2.show(p2);return 0;2,Complexh文件#includeusing namespace std;class complexpublic:complex(float a=0,float b=0);void add(co

16、mplex &p);void show();private:float real;float imag;Complexcpp文件#include4-20.hcomplex:complex(float a,float b):real(a),imag(b)void complex:add(complex &p)real=real+p.real;imag=imag+p.imag;void complex:show()coutreal+imagiendl;Complex maincpp文件#include4-20.hint main()complex c1(3,5);complex c2=4.5;c1

17、.add(c2);c1.show();return 0;实验三一、实验名称数据的共享与保护二、实验目的及要求掌握对静态成员、静态函数、友元函数的使用方法。三、实验环境Microsoft Visual Studio 2010四、实验内容1,定义一个cat类,拥有静态成员numofcat,记录cat的个体数目;静态成员函数getnumofcat(),读取numofcat。2,定义boat与car两个类,二者都有weight属性,定义二者的友元函数gettoalweight(),计算二者的重量和。五、算法描述及实验步骤1,cat+cat()+get(): void+show(): void+getn

18、umofcat(): void - numofcat: int- name: string- num : stringboat+boat(n: string, nu :string, we: string)+gettoalweight(p1: boat&,p2:car&)+get(): void+show(): void- name:string - num : string- weight: intcar+car(n: string, nu :string, we: string)+gettoalweight(p1: boat&,p2:car&)+get(): void+show(): vo

19、id- name:string - num : string- weight: int六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:1, input the name tutuname:tutunum:1numofcat:1call staticnumofcat:1input the name huaname:huanum:2numofcat:2call staticnumofcat:2请按任意键继续. . .2,boatname:fengnum:012002weight:60carname:bennum:0054weight:80140tutu 01

20、24 90baoma 0142453 100boatname:tutunum:0124weight:90carname:baomanum:0142453weight:100190请按任意键继续七、总结1,静态成员,不属于任何对象,系统单独非配空间。静态数据成员具有一般静态数据的特征,即只在第一次赋值。2,静态数据成员可以被所在类的成员函数访问,由于静态成员函数的形参中没有this指针,因此静态成员函数不能直接访问非静态成员数据,可以通过对象名的方式访问。3,如果一个函数被声明为以为类的友元函数,那么这个函数就可以访问这个类中的所有成员。4,有元不可以传递:如声明a是b的有元,b是c的有元。那么

21、b不是a的有元,c也不是b的有元,a不是c的有元。八、附录1, cath文件#include#includeusing namespace std;class catpublic:cat()name= ;numofcat+;num=numofcat;void get();void show();static void getnumofcat();private:static int numofcat;string name;string num;Catcpp文件#include5-7.hint cat:numofcat=0;void cat:get()coutname;void cat:sho

22、w()coutname:nameendl;coutnum:numendl;coutnumofcat:numofcatendl;void cat:getnumofcat()coutcall staticendl;coutnumofcat:numofcatendl;Cat maincpp文件#include5-7.hint main()cat cat1;cat1.get();cat1.show();cat1.getnumofcat();cat cat2;cat2.get();cat2.show();cat2.getnumofcat();return 0;2,Car and boath文件#incl

23、ude#includeusing namespace std;class car;class boatpublic:boat(string n,string nu,int we);void friend gettoalweight(boat &p1,car &p2);void get();void show();private:string name;string num;int weight;class carpublic:car(string n,string nu,int we);void friend gettoalweight(boat &p1,car &p2);void get()

24、;void show();private:string name;string num;int weight;Car and boatcpp文件#include5-14.hvoid gettoalweight(boat &p1,car &p2)coutp1.weight+p2.weightnamenumweight;void boat:show()coutboatendl;coutname:nameendlnum:numendlweight:weightnamenumweight;void car:show()coutcarendl;coutname:nameendlnum:numendlwe

25、ight:weight添加事件处理程)。 在消息类型中选择COMMAND,在类列表中选择CgdView,并在函数处理程序名称栏修改函数名。点击添加编辑添加相应的函数。六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:AB输出如下图:(点击运行菜单中的启动和停止按钮,或者双击鼠标的左右键,字幕会执行相应的动作)七、总结Microsoft Visual Studio 2008与Microsoft Visual Studio 2010有很大的差别。八、附录A 添加的类Max定义:class Maxdouble x1,x2,x3,x4;double Max2(do

26、uble,double);public:Max(double,double,double,double);double Max4();2添加的类Max实现部分:double Max:Max2(double a, double b)if(a=b) return a;else return b;Max:Max(double a, double b, double c, double d)x1=a;x2=b;x3=c;x4=d;double Max:Max4()return Max2(Max2(x1,x2),Max2(x3,x4);3. 添加的Find函数:void CPlotDoc:Find()M

27、ax a(110.5, 120.8, 110, 68);Max b(130, 256.5, 90, 200);Max c(125, 406.8, 350, 330);Max d(120, 356.8, 300, 280.5);Max e(102, 256.8, 120, 105);m_num0 = (int) a. Max4();m_num1 = (int) b. Max4();m_num2 = (int) c. Max4();m_num3 = (int) d. Max4();m_num4 = (int) e. Max4();4. 修改后的OnDraw函数:void CPlotView:OnD

28、raw(CDC* pDC)CPlotDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ TODO: add draw code for native data herepDC-SetMapMode(MM_ISOTROPIC);pDC-SetViewportOrg(50,250);pDC-MoveTo(0,0);pDC-LineTo(1100,0);pDC-MoveTo(0,0);pDC-LineTo(0,600);int width = 40;int ch = A; CString str1;CBrush brush;brush.CreateSolid

29、Brush(RGB(50, 250,0);pDC-SelectObject(brush);for(int i = 1; iRectangle(200*i, 0, 200*i+width, pDoc-m_numi-1); str1.Format(_T(%c),ch); /整型以字符格式赋给str1 pDC-TextOut(200*i+10,-10, str1); /输出ABCDECFont font;font.CreateFont(0,0,0,0,800,0,0,0,OEM_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALI

30、TY,DEFAULT_PITCH,_T(楷体));pDC-SelectObject(&font);pDC-TextOut(200,550, _T(各公司销售点水果月销售量直方图);B1. 在gdView.cpp文件中修改后的OnDraw函数:void CXxx2View:OnDraw(CDC* pDC)CXxx2Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc);if (!pDoc)return;pDC-SetTextColor(RGB(0,0,235);pDC-SetBkMode(TRANSPARENT);CFont font;font.CreateF

31、ont(28,15,0,0,FW_NORMAL,false,false,false,DEFAULT_CHARSET,OUT_DEVICE_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,_T(隶书);pDC-SelectObject(&font);pDC-TextOut(n,100,_T(世上无难事,只要肯登攀!);n=n+20;RECT r;GetClientRect(&r); /获得窗口if(nr.right-r.left)/窗口如果n 右坐标减去左坐标n=0; 添加的消息映射:void CgdView:OnLButtonD

32、blClk(UINT nFlags, CPoint point) /鼠标左双击函数/ TODO: Add your message handler code here and/or call defaultSetTimer(1,500,NULL);CView:OnLButtonDblClk(nFlags, point);void CgdView:OnRButtonDblClk(UINT nFlags, CPoint point) /鼠标右双击函数/ TODO: Add your message handler code here and/or call defaultKillTimer(1);

33、CView:OnRButtonDblClk(nFlags, point);Void CgdView:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call defaultInvalidate();CView:OnTimer(nIDEvent);添加的启动及停止对应的消息void CXxx2View:OnMove() /启动对应消息/ TODO: Add your command handler code hereSetTimer(1,300,NULL);void CXxx2View:OnStop

34、() /停止对应的消息/ TODO: Add your command handler code hereKillTimer(1);实验六一、实验名称单文档串行化编程二、实验目的及要求了解简单的单文档串行化编程方法。三、实验环境Microsoft Visual Studio 2010四、实验内容设计应用程序串行化一个矩形数据,用对话框修改数据,并显示图形。对话框如下:五、算法描述及实验步骤1. 用AppWizard建立一个普通单文档ff工程,按下一步,直到出现下图,将CffView的基类设为CFormView。2. 在对话框中添加相应的控件,并添加变量,如下图:其中第五个编辑框添加为:控件类型

35、,如下图:3.为四个编辑框添加事件处理程序,如下图:并添加相应的函数。3. 在Doc头文件ffDoc.h中添加变量,并在ffDoc.cpp的构造函数中初始化变量。4. 在ffDoc.cpp的串行化代码。5. 为ffView类添加OnDraw函数,添加方法如下图:选添加函数 六、调试过程及实验结果1, 保存源程序代码,并声称解决方案。2,调试并执行。3,输出为:输入不同的x1、y1、x2、y2,编辑框中会显示相应的图形。七、总结串行化代码“arx1y1x2x1y1x2y2”中的ar的作用和cin、cout相似。八、附录1.定义变量及初始化的代码为(添加的代码):/ffdoc.h 文件中class

36、 ffDoc : public CDocumentpublic:int x1, y1, x2, y2;/ffdoc.cpp 文件中CSerialRectDoc:CSerialRectDoc()x1=y1=x2=y2=0;2. 在ffView.cpp中修改的代码(即控件处理函数):Void ffView:OnChangeEdit1() UpdateData();CSerialRectDoc* pDoc= GetDocument();if(m_x1!=pDoc- x1) pDoc- x1 =m_x1;if(m_y1!=pDoc- y1) pDoc- y1 =m_y1;if(m_x2!=pDoc-

37、x2) pDoc- x2 =m_x2;if(m_y2!=pDoc- y2) pDoc- y2 =m_y2;Invalidate(true);Void ffView:OnChangeEdit2() OnChangeEdit1();Void ffView:OnChangeEdit3() OnChangeEdit1();Void ffView:OnChangeEdit5() OnChangeEdit1();void CSerialRectView:OnInitialUpdate() m_x1=GetDocument()-x1;m_x2=GetDocument()-x2;m_y1=GetDocumen

38、t()-y1;m_y2=GetDocument()-y2;UpdateData(false);void CSerialRectDoc:Serialize(CArchive& ar)3. 在ffDoc.cpp中修改的串行化代码:if (ar.IsStoring()/ TODO: add storing code herearx1y1x2x1y1x2y2;4. 在ffView.cpp文件中添加OnDraw函数void CSerialRectView:OnDraw(CDC *pDC)m_Ctrl.UpdateWindow();pDC=m_Ctrl.GetWindowDC();pDC-Rectangle(m_x1,m_y1,m_x2,m_y2);UpdateData(false);

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