地大C++课程实习报告.docx
《地大C++课程实习报告.docx》由会员分享,可在线阅读,更多相关《地大C++课程实习报告.docx(41页珍藏版)》请在冰豆网上搜索。
地大C++课程实习报告
计算机高级语言课程设计报告
姓名:
学号:
学院:
专业:
1、实习题目一………………………………3
2、实习题目二………………………………5
3、实习题目三………………………………9
4、实习题目四………………………………13
5、实习题目五………………………………17
6、实习题目六………………………………23
8、总结………………………………………36
实习题目一
【需求规格说明】
1、有1、2、3、4、5、6六个数,能组成多少个互不相同且无重复数字的三位数?
都是多少?
问题描述及分析:
这道题是数学中的排列组合问题,可以利用三个for循环来解决。
【算法设计】
(1)设计思想:
利用for循环,使输出三位数的百位、十位和个位都小于7,并用判断语句限定个、十、百位均不相同,然后输出百位数*100+十位数*10+个位数的结果。
而求输出数字的个数,只要从0开始,每次判断通过就加1,一直到循环结束即可。
最后利用MFC,将程序结果呈现出来。
(2)设计表示:
本程序结构较为简单,需注意细节。
有三个大循环,for(i=1;i<7;i++)for(j=1;j<7;j++)for(k=1;k<7;k++),用i!
=j&&j!
=k&&k!
=i限制ijk,输出结果100i+10j+k
然后再运用到MFC中保存在对话框的cpp源文件里。
【调试报告】
开始不知道怎么用MFC文本框,就直接用cpp运行保存。
后来学习了解了MFC对话框,就对原程序稍加更改,用MFC对话框运行程序。
--------------------Configuration:
1-Win32Debug--------------------
1.exe-0error(s),0warning(s)
【附录】
(1)源程序清单
//1Dlg.cpp:
implementationfile
#include"stdafx.h"
#include"1.h"
#include"1Dlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
//函数
voidCMy1Dlg:
:
OnButton1()
{
UpdateData();
CStringstr;
inti,j,k;
m_count=0;
//个十百位在1~6之间
for(i=1;i<7;i++)
for(j=1;j<7;j++)
for(k=1;k<7;k++)
if(i!
=i)//3个位数之间无重复
intb=i*100+j*10+k;
str.Format("%d",b);//10进制数转换为cstring
m_a.AddString(str);//将结果在编辑框中显示出来
m_count++;
}
UpdateData(FALSE);
//TODO:
Addyourcontrolnotificationhandlercodehere
(2)运行结果:
实习题目二
5、文本文件中有一组整数,以空格分隔,要求排序后输出到另一个文件中。
此题需要利用到输入输出流以及排序。
在.txt的文档中输入一组数字,以空格分开,保存后能在C++中将程序打开并读出这一列的数据在另一个txt文件中输出。
用输出输入流打开输入文件并用getline函数读取文档文本中的数字。
用输入输出流建立对象,然后调用这个对象的open,close函数来打开和关闭文件。
当打开文件后再用冒泡法排序文件里的整数,并输出到另一个文件中。
再用MFC中的ShellExecute函数将文本文件打开。
刚开始输入的数据之间只能用空格间隔,不支持换行,后来进行了改进。
刚开始对MFC不是很熟练的应用,致使开始无法用MFC中的函数打开文本文件,只能手动打开,后来用ShellExecute改进了。
05-Win32Debug--------------------
Compiling...
05Dlg.cpp
Linking...
LINK:
LNK6004:
Debug/05.exenotfoundornotbuiltbythelastincrementallink;performingfulllink
05.exe-0error(s),0warning(s)
//05Dlg.cpp:
//
#include"05.h"
#include"05Dlg.h"
#include"fstream.h"
//打开原文本
voidCMy05Dlg:
ShellExecute(NULL,"open","1.txt",NULL,NULL,SW_SHOWNORMAL);
//打开处理后的新文本
OnButton3()
ShellExecute(NULL,"open","2.txt",NULL,NULL,SW_SHOWNORMAL);
OnButton2()
ifstreaminfile;
infile.open("1.txt");
ofstreamoutfile;
outfile.open("2.txt");
inta=1;chars;
infile.get(s);
while(!
infile.eof())
if(s==''||s=='\n')
a++;
}infile.close();
int*p=newint[a];
for(inti=1;i{infile>>p[i];}for(intm=1;m{intj=m;intk=p[m];while(j>0&&k{p[j]=p[j-1];j--;}p[j]=k;}for(intn=1;n{outfile<}MessageBox("已经处理完毕!");}(2)运行结果实习题目三【需求规格说明】6、计算正方体、球体和圆柱体的表面积和体积(要求使用虚函数)。问题描述及分析:此题利用虚函数,计算表面积和体积,然后再MFC中编。【算法设计】(1)设计思想:这道题主要用到类的继承与派生、虚函数等在MFC中的应用。首先要理解虚函数,实现多态性,通过指向派生类的基类指针,访问派生类中同名覆盖成员函数。所以将这些基类与派生类、虚函数调用等代码写在里面,然后再在对话框中对各种按钮进行编写代码(2)设计表示:Base型的指针对象是指向派生类同名覆盖函数的基类指针。派生类、基类直接的关系图: 【调试报告】刚开始对调用成员函数产生困然,后来反复操作解决了。--------------------Configuration:06-Win32Debug--------------------Compiling...06Dlg.cppLinking...LINK:LNK6004:Debug/06.exenotfoundornotbuiltbythelastincrementallink;performingfulllink06.exe-0error(s),0warning(s) 【附录】//6_计算体积和表面积Dlg.cpp:implementationfile//#include"stdafx.h"#include"6.h"#include"6Dlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endifconstdoublePI=3.14;classShape//基类{public:virtualdoubleGetArea()=0;virtualdoubleGetVolume()=0;};classCube:publicShape//正方体的类{public:Cube(doublea):l(a){};doubleGetArea(){return6*l*l;}doubleGetVolume(){returnl*l*l;}private:doublel;};classSphere:publicShape//球的类{public:Sphere(doubleradius):r(radius){};doubleGetArea(){return4*PI*r*r;}doubleGetVolume(){return4*PI*r*r*r/3;}private:doubler;};classCylinder:publicShape//圆柱体的类{public:Cylinder(doublehigh,doubleradius):h(high),r(radius){};doubleGetArea(){return2*PI*r*r+2*PI*r*h;}doubleGetVolume(){returnPI*r*r*h;}private:doubleh;doubler;};doublefun1(Shape*str)//计算表面积{returnstr->GetArea();}doublefun2(Shape*str)//计算体积{returnstr->GetVolume();}voidCMy6_Dlg::OnButton1(){
infile>>p[i];
for(intm=1;m{intj=m;intk=p[m];while(j>0&&k{p[j]=p[j-1];j--;}p[j]=k;}for(intn=1;n{outfile<}MessageBox("已经处理完毕!");}(2)运行结果实习题目三【需求规格说明】6、计算正方体、球体和圆柱体的表面积和体积(要求使用虚函数)。问题描述及分析:此题利用虚函数,计算表面积和体积,然后再MFC中编。【算法设计】(1)设计思想:这道题主要用到类的继承与派生、虚函数等在MFC中的应用。首先要理解虚函数,实现多态性,通过指向派生类的基类指针,访问派生类中同名覆盖成员函数。所以将这些基类与派生类、虚函数调用等代码写在里面,然后再在对话框中对各种按钮进行编写代码(2)设计表示:Base型的指针对象是指向派生类同名覆盖函数的基类指针。派生类、基类直接的关系图: 【调试报告】刚开始对调用成员函数产生困然,后来反复操作解决了。--------------------Configuration:06-Win32Debug--------------------Compiling...06Dlg.cppLinking...LINK:LNK6004:Debug/06.exenotfoundornotbuiltbythelastincrementallink;performingfulllink06.exe-0error(s),0warning(s) 【附录】//6_计算体积和表面积Dlg.cpp:implementationfile//#include"stdafx.h"#include"6.h"#include"6Dlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endifconstdoublePI=3.14;classShape//基类{public:virtualdoubleGetArea()=0;virtualdoubleGetVolume()=0;};classCube:publicShape//正方体的类{public:Cube(doublea):l(a){};doubleGetArea(){return6*l*l;}doubleGetVolume(){returnl*l*l;}private:doublel;};classSphere:publicShape//球的类{public:Sphere(doubleradius):r(radius){};doubleGetArea(){return4*PI*r*r;}doubleGetVolume(){return4*PI*r*r*r/3;}private:doubler;};classCylinder:publicShape//圆柱体的类{public:Cylinder(doublehigh,doubleradius):h(high),r(radius){};doubleGetArea(){return2*PI*r*r+2*PI*r*h;}doubleGetVolume(){returnPI*r*r*h;}private:doubleh;doubler;};doublefun1(Shape*str)//计算表面积{returnstr->GetArea();}doublefun2(Shape*str)//计算体积{returnstr->GetVolume();}voidCMy6_Dlg::OnButton1(){
intj=m;
intk=p[m];
while(j>0&&k
p[j]=p[j-1];
j--;
p[j]=k;
for(intn=1;n{outfile<}MessageBox("已经处理完毕!");}(2)运行结果实习题目三【需求规格说明】6、计算正方体、球体和圆柱体的表面积和体积(要求使用虚函数)。问题描述及分析:此题利用虚函数,计算表面积和体积,然后再MFC中编。【算法设计】(1)设计思想:这道题主要用到类的继承与派生、虚函数等在MFC中的应用。首先要理解虚函数,实现多态性,通过指向派生类的基类指针,访问派生类中同名覆盖成员函数。所以将这些基类与派生类、虚函数调用等代码写在里面,然后再在对话框中对各种按钮进行编写代码(2)设计表示:Base型的指针对象是指向派生类同名覆盖函数的基类指针。派生类、基类直接的关系图: 【调试报告】刚开始对调用成员函数产生困然,后来反复操作解决了。--------------------Configuration:06-Win32Debug--------------------Compiling...06Dlg.cppLinking...LINK:LNK6004:Debug/06.exenotfoundornotbuiltbythelastincrementallink;performingfulllink06.exe-0error(s),0warning(s) 【附录】//6_计算体积和表面积Dlg.cpp:implementationfile//#include"stdafx.h"#include"6.h"#include"6Dlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endifconstdoublePI=3.14;classShape//基类{public:virtualdoubleGetArea()=0;virtualdoubleGetVolume()=0;};classCube:publicShape//正方体的类{public:Cube(doublea):l(a){};doubleGetArea(){return6*l*l;}doubleGetVolume(){returnl*l*l;}private:doublel;};classSphere:publicShape//球的类{public:Sphere(doubleradius):r(radius){};doubleGetArea(){return4*PI*r*r;}doubleGetVolume(){return4*PI*r*r*r/3;}private:doubler;};classCylinder:publicShape//圆柱体的类{public:Cylinder(doublehigh,doubleradius):h(high),r(radius){};doubleGetArea(){return2*PI*r*r+2*PI*r*h;}doubleGetVolume(){returnPI*r*r*h;}private:doubleh;doubler;};doublefun1(Shape*str)//计算表面积{returnstr->GetArea();}doublefun2(Shape*str)//计算体积{returnstr->GetVolume();}voidCMy6_Dlg::OnButton1(){
outfile<
MessageBox("已经处理完毕!
");
(2)运行结果
实习题目三
6、计算正方体、球体和圆柱体的表面积和体积(要求使用虚函数)。
此题利用虚函数,计算表面积和体积,然后再MFC中编。
这道题主要用到类的继承与派生、虚函数等在MFC中的应用。
首先要理解虚函数,实现多态性,通过指向派生类的基类指针,访问派生类中同名覆盖成员函数。
所以将这些基类与派生类、虚函数调用等代码写在里面,然后再在对话框中对各种按钮进行编写代码
Base型的指针对象是指向派生类同名覆盖函数的基类指针。
派生类、基类直接的关系图:
刚开始对调用成员函数产生困然,后来反复操作解决了。
06-Win32Debug--------------------
06Dlg.cpp
Debug/06.exenotfoundornotbuiltbythelastincrementallink;performingfulllink
06.exe-0error(s),0warning(s)
//6_计算体积和表面积Dlg.cpp:
#include"6.h"
#include"6Dlg.h"
constdoublePI=3.14;
classShape//基类
public:
virtualdoubleGetArea()=0;
virtualdoubleGetVolume()=0;
};
classCube:
publicShape//正方体的类
Cube(doublea):
l(a){};
doubleGetArea()
return6*l*l;
doubleGetVolume()
returnl*l*l;
private:
doublel;
classSphere:
publicShape//球的类
Sphere(doubleradius):
r(radius){};
return4*PI*r*r;
return4*PI*r*r*r/3;
doubler;
classCylinder:
publicShape//圆柱体的类
Cylinder(doublehigh,doubleradius):
h(high),r(radius){};
return2*PI*r*r+2*PI*r*h;
returnPI*r*r*h;
doubleh;
doublefun1(Shape*str)//计算表面积
returnstr->GetArea();
doublefun2(Shape*str)//计算体积
returnstr->GetVolume();
voidCMy6_Dlg:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1