C++程序设计实验六参考答案.docx
- 文档编号:13488222
- 上传时间:2023-06-14
- 格式:DOCX
- 页数:28
- 大小:28.88KB
C++程序设计实验六参考答案.docx
《C++程序设计实验六参考答案.docx》由会员分享,可在线阅读,更多相关《C++程序设计实验六参考答案.docx(28页珍藏版)》请在冰点文库上搜索。
C++程序设计实验六参考答案
广东金融学院实验报告
课程名称:
C++程序设计
实验编号
实验6类和对象练习
及实验名称
姓名学号
实验地点新电403实验日期2011-10-27
指导教师曲政同组其他成员无
一、实验目的及要求
系别
班级
实验时数
成绩
参考答案
6
1.学习面向对象设计的基本概念:
模块分解的思路、类、函数、对象等的概念和编程。
2.实践简单类的设计,包括数据抽象、成员函数的编写、封装、重载、静态成员等,并应用这些类完成给定的计算任务。
二、实验环境及相关情况(包含使用软件、实验设备、主要仪器及材料等)
硬件设备:
联网的个人计算机
操作系统:
WindowsXP
编译系统:
VisualStudio2005
三、实验内容及步骤(包含简要的实验步骤流程)
1.关于控制结构的知识点和例题(略)
2.控制结构编程练习
⑴设计一个圆(Circle)类,包含计算其周长(perimeter)和面积(area)两项功能。
编写测试
程序,输入半径(radius),打印出类似“半径为1的圆,周长为6.2832,面积为3.1416”的信息。
⑵设计一个时间类MyTime,封装6个整型变量分别表示年(year)、月(month)、日(day)、时(hour)、
分(minute)、秒(second)。
仿照本课件例3.2取得当前系统时间,以1秒为间隔滚动显示。
⑶PointLine类
设计一个平面数据点类Point,封装double型坐标(x,y),并提供必要的入口和出口。
设计一个直线类Line,封装2个数据点对象,并提供必要的入口和出口。
设计主函数,从起点(0,0)开始到终点(π,0)结束,以横坐标π/20为步长,以端点坐标对形式
输出一条正弦曲线(通过在格式符的字段序号后加“:
fn”来控制小数位数,如{0:
f4}):
⑷Student类
设计一个学生类Student,包括学号ID、姓名name、平时成绩usual、考试成绩exam和总评成绩mark
(以String保存学号和姓名、以double保存成绩数据),以及一个静态整型成员变量counter(保存学生
人数)和一个静态浮点成员变量sum(保存总评成绩总和)。
编写属性函数代替来入口和出口,并编写
一个计算总评成绩的函数calcMark(按平时成绩占40%、考试成绩占60%的比例计算总评成绩)和一个
分数累积函数sumMark。
编写测试程序,仿照给定的演示程序StudentIO.cpp从给定的文本文件读入原始
数据、计算总评成绩、以列表的方式输出原始数据和计算结果。
最后计算总评平均成绩,并与学生人数一同输出到屏幕上。
第1页共10页
四、实验结果(源程序)
(1)
#include"stdafx.h"
usingnamespaceSystem;
refclassCircle{
private
:
double
m_radius;
public
:
Circle(
doubleradius){m_radius=radius;}
double
getRadius(){
return
m_radius;}
double
calcArea(){
return
Math:
:
PI*m_radius*m_radius;}
double
calcPerimeter(){
return2*Math:
:
PI*m_radius;}
};
intmain(array : String^>^args) { doubleradius; Console: : Write("半径="); radius=Convert: : ToDouble(Console: : ReadLine()); Circlecircle(radius); Console: : WriteLine("半径为{0}的圆,周长为{1},面积为{2}。 ",circle.getRadius(),circle.calcPerimeter(), circle.calcArea()); return0; } 第2页共10页 (2) #include"stdafx.h" usingnamespaceSystem; refclassMyDateTime{ private: intm_year,m_month,m_day,m_hour,m_minute,m_second; public: MyDateTime(intyear,intmonth,intday,inthour,intminute,intsecond){ m_year=year; m_month=month; m_day=day; m_hour=hour; m_minute=minute; m_second=second; } intgetYear(){returnm_year;} intgetMonth(){returnm_month;} intgetDay(){returnm_day;} intgetHour(){returnm_hour;} intgetMinute(){returnm_minute;} intgetSecond(){returnm_second;} boolUpdate(){ DateTimedt=DateTime: : Now; if(dt.Second! =m_second){ m_year=dt.Year; m_month=dt.Month; m_day=dt.Day; m_hour=dt.Hour; m_minute=dt.Minute; m_second=dt.Second; returntrue; } returnfalse; } }; 第3页共10页 intmain(array : String^>^args) { DateTimedt=DateTime: : Now; MyDateTime^time=gcnewMyDateTime(dt.Year,dt.Month,dt.Day,dt.Hour,dt.Minute,dt.Second); inti=0; while(i<60){ if(time->Update()){ Console: : WriteLine("{0}-{1}-{2}{3}: {4}: {5}",time->getYear(),time->getMonth(), time->getDay(),time->getHour(),time->getMinute(),time->getSecond()); i++; } } return0; } 第4页共10页 (3) #include"stdafx.h" usingnamespaceSystem; valueclassPoint{ private: doublem_x,m_y; public: Point(doublex,doubley){m_x=x;m_y=y;} propertydoubleX{ voidset(doublex){m_x=x;} doubleget(){returnm_x;} } propertydoubleY{ voidset(doubley){m_y=y;} doubleget(){returnm_y;} } voidsetPoint(doublex,doubley){m_x=x;m_y=y;} doublegetX(){returnm_x;} doublegetY(){returnm_y;} }; refclassLine{ private: Pointm_p0,m_p1; public: Line(Pointp0,Pointp1): m_p0(p0),m_p1(p1){} Line(){} ~Line(){} Line(Line%line){m_p0=line.m_p0;m_p1=line.m_p1;} Line%operator=(Line%line){m_p0=line.m_p0;m_p1=line.m_p1;return*this;} propertyPointP0{ voidset(Pointp0){m_p0=p0;} Pointget(){returnm_p0;} } propertyPointP1{ voidset(Pointp1){m_p1=p1;} Pointget(){returnm_p1;} } voidsetLine(Pointp0,Pointp1){m_p0=p0;m_p1=p1;} PointgetP0(){returnm_p0;} PointgetP1(){returnm_p1;} }; 第5页共10页 intmain(array : String^>^args) { Pointp0,p1; p0.X=0; p0.Y=Math: : Sin(p0.X); while(p0.X : PI){ p1.X=p0.X+Math: : PI/20; p1.Y=Math: : Sin(p1.X); Console: : WriteLine("Linefrom({0: f4},{1: f4})to({2: f4},{3: f4})",p0.X,p0.Y,p1.X,p1.Y); p0=p1; } return0; } 第6页共10页 (4) #include"stdafx.h" using namespaceSystem; using namespaceSystem: : IO; refclassStudent{ private : String^m_id; String^m_name; double m_usual; double m_exam; double m_mark; static intcounter; static double sum,sqrsum; public : Student(String^id,String^name, doubleusual,doubleexam){ m_id=id; m_name=name; m_usual=usual; m_exam=exam; counter++; } Student(){counter++;} ~Student(){} property String^Id{ String^get(){ return m_id;} voidset(String^id){m_id=id;} } property String^Name{ String^get(){ return m_name;} voidset(String^name){m_name=name;} } property doubleUsual{ doubleget(){ return m_usual;} voidset( double usual){m_usual=usual;} } property doubleExam{ doubleget(){ return m_exam;} voidset( double exam){m_exam=exam;} } property doubleMark{ doubleget(){ return m_mark;} } 第7页共10页 voidcalcMark(){ m_mark=m_usual*0.4+m_exam*0.6; } voidsumMark(){ sum+=m_mark; sqrsum+=m_mark*m_mark; } static intgetCounter(){ return counter;} static double getSum(){ return sum;} static double getSqrsum(){ returnsqrsum;} }; 第8页共10页 intmain(array : String^>^args) { doublesum,sqrsum,average,stddev; String^filename; String^str; Console: : Write(L"班级: ");filename=Console: : ReadLine()+L".txt"; if(! File: : Exists(filename)){//检测文件是否存在 Console: : WriteLine("Filenotfound.");//如果文件不存在则打印错误信息 return-1;//并退出程序 } FileStreamfi(filename,FileMode: : Open,FileAccess: : Read);//相当于: ifstreamfi; StreamReaderfr(%fi);//相当于: fi.open(fn); while((str=fr.ReadLine())! =nullptr) { Studentstudent; student.Id=str->Substring(0,9); student.Name=str->Substring(10,3); student.Usual=Convert: : ToDouble(str->Substring(14,3)); student.Exam=Convert: : ToDouble(str->Substring(18,3)); student.calcMark(); student.sumMark(); Console: : WriteLine("{0}{1}{2}{3}{4: F1}",student.Id,student.Name,student.Usual, student.Exam,student.Mark); } fi.Close();//关闭文件 intn=Student: : getCounter(); sum=Student: : getSum(); sqrsum=Student: : getSqrsum(); average=sum/n; stddev=Math: : Sqrt((n*sqrsum-sum*sum)/(n*(n-1))); Console: : WriteLine( "学生人数: {0}",n); Console: : WriteLine( "平均分数: {0: F1}" average); Console: : WriteLine( "标准偏差: {0: F2}" stddev); return0; } 第9页共10页 五、实验总结(包括心得体会、问题回答及实验改进意见,可附页) 六、教师评语 第10页共10页
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 程序设计 实验 参考答案