面向对象实习报告.docx
- 文档编号:17277099
- 上传时间:2023-07-23
- 格式:DOCX
- 页数:21
- 大小:279.04KB
面向对象实习报告.docx
《面向对象实习报告.docx》由会员分享,可在线阅读,更多相关《面向对象实习报告.docx(21页珍藏版)》请在冰点文库上搜索。
面向对象实习报告
实习题目一
1.三个自定义类
本题有三个自定义的类:
点类VecPosition,圆类circle,矩形类Rect,运用类的继承思想从这三个类中抽象出公共基类base。
其中base中有一个纯虚函数virtualvoiddraw()=0;
VecPosition类主要实现各个运算符的点的构造,运算符的重载,求两点距离,判断一个点是否在给定其他几个点的范围内,以及与向量有关的几何运算。
Circle类主要实现圆对象的构造,圆的相关参数设置,求取圆的周长函数(doubleCircle:
:
getCircumference())、面积函数(doubleCircle:
:
getArea()),判断一个点是否在圆内boolCircle:
:
isInside(VecPositionpos),求两个圆交点函数intCircle:
:
getIntersectionPoints(Circlec,VecPosition*p1,VecPosition*p2)返回加点个数。
求两个圆的相交面积doubleCircle:
:
getIntersectionArea(Circlec)。
矩形由左上点坐标和右下点坐标确定。
包括矩形的构造,参数的设置修改,求矩形的面积,周长,以及判断一个点是否在矩形内。
2.可视化界面
建立单文档工程,添加画图菜单,如下图:
三个菜单项分别画点,画线,画矩形。
在view.h中添加如下变量:
CPoint类的m_oldpt,m_pt用于记录画图的起点坐标和终点坐标。
Circlem_circle,Rectm_rect,用于构造圆和矩形。
afx_msgvoidOnLButtonDown(UINTnFlags,CPointpoint)是响应鼠标按下的消息,并将当前坐标传给m_oldPoint。
用switch分支控制画图种类,在view类中加入变量i,添加三个菜单消息响应函数,对i进行赋值;当点击菜单项“点”时,i=0;当点击菜单项“圆”时,i=1;当点击菜单项“矩形”时,i=2;
在view中添加voidC实º习一View:
:
OnLButtonUp(UINTnFlags,CPointpoint)用来响应鼠标左键弹起消息。
三种图形的绘制在该函数中完成。
具体绘图代码如下:
voidC实习一View:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
CStringstr="",str1="";
//TODO:
在此添加消息处|理程序代码和或调用默认值
VecPositionpt1,pt2;
Circlecir1;
RectRect1;
pt1.setVecPosition(m_pt.x,m_pt.y);
pt2.setVecPosition(point.x,point.y);
CClientDCpDC(this);///////客户区绘图对象
CBrush*poidBrush=CBrush:
:
FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//设置透明画刷,以免绘制的图形之间相互覆盖
/*CBrush*poidBrush=*/pDC.SelectObject(poidBrush);//将画刷选入设备环境
switch(i)
{
case0:
/////////////////////点和圆关系
str="该点的坐标为\r\n";
str1.Format("%s",pt1.str().c_str());
str+=str1;
if(m_ncir!
=0&&m_nrect!
=0)
{
{
if(m_circle.isInside(pt1))
str+="\r\n点在圆的内部?
";
else
str+="\r\n点不在圆的内部?
";
}
{
if(m_rect.isInside(pt1))
str+="\r\n点在矩形的内部";
else
str+="\r\n点不在矩形的内部";
}
}
elseif(m_ncir!
=0&&m_nrect==0)
{
if(m_circle.isInside(pt1))
str+="\r\n点在圆的内部";
else
str+="\r\n点不在圆的内部";
}
elseif(m_ncir==0&&m_nrect>0)
{
if(m_rect.isInside(pt1))
str+="\r\n点在矩形的内部";
else
str+="\r\n点不在矩形的内部";
}
MessageBox(str,"显示:
");
break;
case1:
///////////两个圆的关系
doubleradius;
radius=pt1.getDistanceTo(pt2);
point=m_pt;
m_pt.x-=radius;
m_pt.y-=radius;
point.x+=radius;
point.y+=radius;
pDC.Ellipse(m_pt.x,m_pt.y,point.x,point.y);
cir1.setCircle(pt1,radius);
m_circle.setCircle(pt1,radius);
str1="圆心坐标:
\r\n";
str+=str1;
str1.Format("%s",cir1.getCenter().str().c_str());
str+=str1;
str1="\r\n圆的半径:
";
str+=str1;
str1.Format("%f",cir1.getRadius());
str+=str1+"\r\n";
if(m_ncir>0)
{
inta=m_circle.getIntersectionPoints(cir1,&pt1,&pt2);////两元的交点p1,p2,反回交点个数a
if(a==1)
{
str1="\r\n两个圆有一个交点:
\r\n";
str+=str1;
str1.Format("%s",pt1.str().c_str());
str+=str1;
}
elseif(a==2)
{
str1="\r\n两个圆有两个交点:
\r\n";
str+=str1;
str1.Format("%s",pt1.str().c_str());
str=str+str1+"\r\n";
str1.Format("%s",pt2.str().c_str());
str=str+str1+"\r\n";
doubled=cir1.getIntersectionArea(m_circle);////相交面积
str1="两圆的相交面积为:
\r\n";
str+=str1;
str1.Format("%f",d);
str+=str1;
MessageBox(str);
}
else
{
}
}
m_ncir++;//记录圆的个数
MessageBox(str);
break;
case2:
////////////////////矩形
pDC.Rectangle(CRect(m_pt,point));
VecPositionp3,p4;
p3.setVecPosition(m_pt.x,m_pt.y);
p4.setVecPosition(point.x,point.y);
m_rect.setPosLeftTop(p3);
m_rect.setPosRightBottom(p4);
doublearea=m_rect.getArea();
doubleCircumference=m_rect.getCircumference();
CStringstr1,str2,str3,str4,str5,str6,str7,str8;
str1="左上角坐标:
\r\n";
str2.Format("%s",pt1.str().c_str());
str3="\r\n右下角坐标:
\r\n";
str4.Format("%s",pt2.str().c_str());
str5="\r\n矩形周长:
\r\n";
str6.Format("%f",Circumference);
str7="\r\n矩形面积:
\r\n";
str8.Format("%f",area);
MessageBox(str1+str2+str3+str4+str5+str6+str7+str8,"显示:
");
m_nrect++;
break;
}
CView:
:
OnLButtonUp(nFlags,point);
}
在画图的同时以Messagebox弹出相关图形的信息以及计算结果。
3运行结果截图:
画点(点画的比较小,不容易看清)
画圆
实习题目二
本题主要涉及文件的读写,字符串的分割与数据的提取,以及对线性群体数据的查找,添加,删除,排序等功能。
1.数据组织
建立一个Student结构体来存储与学生相关信息,具体如下:
typedefstruct{
CStringnum;//学号
CStringname;
CStringmajor;
floatmathgrade;
floatEnglishgrade;
floatphysicalgrade;
floatgrade;
}Student;
建立一个Student数组来逐个保存每一个学生信息。
2.可视化操作:
创建基于对话框的MFC工程,对话框类为C成绩管理系统Dlg,将Student数组添加为该类的成员变量。
对话框如下:
分别为6个编辑框添加变量并绑定。
3.算法设计
给对话框添加Read函数,用于将文件中的数据读到数组中。
在读文件是由于一行一行读取时不知道每一行的字节长度,所以用了CFile类的派生类CStdioFile,该类的ReadString能够读取一行,遇到换行符即停止。
在字符串的分割时候也遇到了一些问题,找资料后得以解决。
str.Left(str.Find("\t"))以空格为界读取左侧内容。
str=str.Mid(str.Find("\t"));str=str.Trim();用于找到空格,并移除。
多次这样操作便可将文本文件中的数据提取出来
voidC成绩管理系统Dlg:
:
Read(void)
{
CStdioFilefile("a.txt",CFile:
:
modeRead);
if(file)
{
inti=0;
CStringstr,str1,str2,str3,str4,str5,str6;
while(file.ReadString(str))
{
str1=str.Left(str.Find("\t"));
//stu[i].num=str1;
str=str.Mid(str.Find("\t"));
str=str.Trim();
str2=str.Left(str.Find("\t"));
//stu[i].name=str2;
str=str.Mid(str.Find("\t"));
str=str.Trim();//str2读出后删掉,便于读取str3
//str.TrimLeft();//缺省时删除空格
str3=str.Left(str.Find("\t"));
//stu[i].major=str3;
str=str.Mid(str.Find("\t"));
str=str.Trim();
//str.TrimLeft();
str4=str.Left(str.Find("\t"));
//stu[i].mathgrade=atof(str4);
str=str.Mid(str.Find("\t"));
str=str.Trim();
//str.TrimLeft();
str5=str.Left(str.Find("\t"));
//stu[i].Englishgrade=atof(str5);
str=str.Mid(str.Find("\t"));
str=str.Trim();
//str.TrimLeft();
str6=str;
stu[i].num=str1;////////对student赋值
stu[i].name=str2;
stu[i].major=str3;
stu[i].mathgrade=atof(str4);
stu[i].Englishgrade=atof(str5);
stu[i].physicalgrade=atof(str6);
stu[i].grade=stu[i].mathgrade+stu[i].Englishgrade+stu[i].physicalgrade;
i++;
//stu[i].physicalgrade=atof(str6);
//str.Trim();
//str.TrimLeft();
//MessageBox(stu[i-1].mathgrade);
}
file.Close();
}
else
MessageBox("文件打开失败!
");
}
查找功能实现:
采用循环遍历的方法依次比较所查找的关键字是否与学生的学号或姓名相同。
查找后的信息用MessageBox显示。
voidC成绩管理系统Dlg:
:
OnBnClickedButton1()////查找
{
UpdateData(TRUE);
for(inti=0;stu[i].num!
="";i++)
{
if(stu[i].name==m_name||stu[i].num==m_num)
{CStringptr,ptr1,ptr2,ptr3;
ptr1.Format("%f",stu[i].mathgrade);
ptr2.Format("%f",stu[i].Englishgrade);
ptr3.Format("%f",stu[i].physicalgrade);
ptr="姓名:
"+stu[i].name+"\r\n"+"学号:
"+stu[i].num+"\r\n"
+"专业号:
"+stu[i].major+"\r\n"+"数学成绩:
"+ptr1+"\r\n"
+"外语成绩:
"+ptr2+"\r\n"+"物?
理成绩:
"+ptr3;
MessageBox(ptr,"该学生信息为:
");
break;}
else{
if(stu[i+1].num=="")
{
MessageBox("没有此人!
","",MB_ICONWARNING);
break;
}
}
}
//TODO:
在此添加控件通知理程序代码
}
添加功能实现:
将六个编辑框中的数据组合成字符串,写到文件末尾。
voidC成绩管理系统Dlg:
:
OnBnClickedButton2()//添加
{UpdateData(TRUE);
Read();
for(intj=0;stu[j].num!
="";)
{j++;
m_count=j;
}
for(inti=0;i { if(m_num==stu[i].num) { MessageBox("不能重复添加! "); return; } } if(m_num==""||m_name==""||m_major=="0"|| m_mathGrade==0||m_EnglishGrade==0||m_physicalGrade==0) { MessageBox("请完善输入信息! "); return; } CStdioFileof; CStringstr,str1; str=str1=""; if(of.Open("a.txt",CFile: : modeReadWrite|CFile: : modeCreate|CFile: : modeNoTruncate)) while(of.ReadString(str)) { str1=str.Left(str.Find("\t")); ints=atoi(str1); intn=atoi(m_num); } of.SeekToEnd(); CStringstr3,str4,str5; str3.Format("%d",m_mathGrade); str4.Format("%d",m_EnglishGrade); str5.Format("%d",m_physicalGrade); str=m_num+"\t"+m_name+"\t"+m_major+"\t\t"+str3+"\t"+ str4+"\t"+str5+"\r\n"; of.Write(str,str.GetLength()); of.Close(); MessageBox("添加成功! "); m_num=m_name=m_major=""; m_mathGrade=m_EnglishGrade=m_physicalGrade=0; UpdateData(0); } 删除功能的实现: 现根据学号或者姓名在数组中找到要删除的学生,然后依次用后一个学生数据覆盖前一个数据,这样就将该学生从数组中去掉了。 再将所有学生的数据重新写入文件“a.txt”,这样文件中的该学生也没有了。 voidC成¦绩管理系统Dlg: : OnBnClickedButton3() { UpdateData(TRUE); for(inti=0;stu[i].num! ="";i++) { if(stu[i].num==m_num) {MessageBox("以查找到此人,正在删除"); for(i;stu[i].num! ="";i++) {stu[i]=stu[i+1];} MessageBox("删除成功! "); break; } else{ if(stu[i+1].num=="") {MessageBox("没有此人! ","",MB_ICONWARNING);} } } CStdioFilefile("a.txt",CFile: : modeWrite); file.SeekToBegin(); for(inti=0;stu[i].num! ="";i++){ CStringstr,str1,str2,str3,str4,str5,str6; str1=stu[i].num; str2=stu[i].name; str3=stu[i].major; str4.Format("%f",stu[i].mathgrade); str5.Format("%f",stu[i].Englishgrade); str6.Format("%f",stu[i].physicalgrade); str=str1+""+str2+""+str3+""+str4+""+ str5+""+str6+"\r\n"; file.Write(str,str.GetLength()); } file.Close(); } 排序功能的实现: 使用冒泡排序的算法将学生按照总分高低排序,将排序后的学生数据写入到文件“b.txt”中。 voidC成绩管理系统Dlg: : OnBnClickedButton4() { intj=0; for(j=0;stu[j].num! ="";) j++; m_count=j; for(inti=0;stu[i].num! ="";i++) { for(intb=0;b { if(stu[b].grade {Studentst; st=stu[b]; stu[b]=stu[b+1]; stu[b+1]=st; } elseif(stu[b].grade==stu[b+1].grade&& stu[b].mathgrade { Studentst=stu[b]; stu[b]=stu[b+1]; stu[b+1]=st; } } }///////////冒泡排序 CStdioFileis("b.txt",CFile: : modeWrite|CFile: : modeCreate);////写入b.txt if(is){ for(inti=0;stu[i+1].num! ="";i++) { CStringstr,str1,str2,str3,str4,str5,str6; str1=stu[i].num; str2=stu[i].name; str3=stu[i].major; str4.Format("%f",stu[i].mathgrade); str5.Format("%f",stu[i].Englishgrade); str6.Format("%f",stu[i].physicalgrade); str=str1+""+str2+""+str3+""+str4+""+ str5+""+str6+"\r\n"; is.Write(str,str.GetLength()); } MessageBox("排序成功! "); is.Close(); } else { MessageBox("排序写入失败! "); return; } } 4运行结果截图 另外,添加,排序,删除的结果写
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 实习 报告