《程序设计基础及语言》实验指导手册.docx
- 文档编号:7757497
- 上传时间:2023-01-26
- 格式:DOCX
- 页数:37
- 大小:984.69KB
《程序设计基础及语言》实验指导手册.docx
《《程序设计基础及语言》实验指导手册.docx》由会员分享,可在线阅读,更多相关《《程序设计基础及语言》实验指导手册.docx(37页珍藏版)》请在冰豆网上搜索。
《程序设计基础及语言》实验指导手册
程序设计基础及语言
实验指导手册
东南大学
计算机科学与工程学院
软件学院
2012年9月
第一部分实验环境的使用方法
VisualC++2008(简称VC++)是Microsoft公司开发的基于C/C++的集成开发工具,它是VisualStudio中功能强大、代码效率最高的开发工具。
利用VisualC++可以采用两种方式编写Win32应用程序:
第一种:
基于WindowsAPI的编程方式(又称为控制台编程方式)。
第二种:
基于MFC的C++编程方式。
本节介绍VC++语言的最基本的编程环境和控制台编程方式,通过简单实例介绍VC++的基本使用方法,使学生尽快学会和掌握如何在VC++环境下,采用控制台编程方式进行课程实验。
1.1目的
1.了解VisualC++2008的特点。
2.熟悉VisualC++2008的控制台开发环境。
3.学习和掌握VisualC++2008的编写控制台程序的方法
1.2VisualC++2008ExpressEditionManual
CreatedwithatrialversionofScreenSteps
1.2.1FirstRun
点击windows左下角,开始-程序,找到新安装的MicrosoftVisualStudio2008。
点击第三个图标选项,就可以启动VisualC++2008。
顺利的话,会看到这样的界面,如果没有看见,一定是安装过程出问题了。
点击右上角小叉,将小窗口关闭。
1.2.2Newproject
点击左上角,File->New->Project,新建项目,将弹出如下对话框。
如上图所示,选择CLR->CLR空项目(或emptyproject)->输入工程名称->选择保存位置(注意:
不要把保存位置设在C盘,以免关机后被还原卡删除)。
1.2.3Addnewsourcefile
如上图所示,在solutionexplorer中找到并右键点击SourceFile->Add->NewItem,将弹出如下对话框,选择代码->C++文件->文件名->Add。
接下来就可以在文本编辑区写代码了。
填写:
#include
voidmain(){
std:
:
cout<<"Hello,MyfirstCPPprogram.";
}
接着,如上图所示,可以生成解决方案,点击Build->BuildSolutionF7。
正常就可以看到,编译成功的消息。
我们可以看到上图中显示,"1succeeded,0failed,0skipped",表示成功生成解决方案。
如果说你的源程序有明显的语法错误,按F7编译,则会提示编译失败。
编译出来的结果放在这里了。
这个路径信息很重要。
编译成功之后,就可以运行源程序查看结果了,具体的有下面四种方法运行源程序。
1、按快捷键F5运行
2、点击菜单栏的Debug->StartingDebuging
3、点击工具栏的
按钮。
4、按快捷键Ctrl+F5,这种方法在程序运行完后控制台窗口不会自动退出,而是等待你按下任意键退出,借此可以看到运行结果。
1.2.5FirstDebug
这一节讲解如何使用调试功能来调试程序。
首先修改源代码为:
#include
voidmain(){
inti=0;
for(i=0;i<10;i++){
std:
:
cout<<"Hello,time(s):
"<
:
endl;
}
}
调试步骤如下
1、设置断点
断点以行为单位,程序在运行到断点所在行时会暂停运行,直到你给出继续运行的命令,程序才会接着运行。
设置断点非常容易,只要双击所选行的左侧灰色区域即可,如下图所示,断点设置成功后,左侧灰色区域会出现一个红色的小点。
2、启动调试功能
按F5启动调试功能,程序会自动运行到第一个断点处停下。
继续按F5会自动跳到下一个断点,按F10则按顺序运行下一条语句。
3、查看变量
程序暂停在某个断点时,可以查看相关变量的值,以检查程序的运行状态是否和预想的一致。
如下图所示,有三个主要的查看窗口,自动窗口,局部窗口,监视窗口。
特别的,我们可以在监视窗口输入表达式,随时检查表达式的值是否符合预想。
如上图所示。
4、条件断点
VC还允许我们为断点设置中断条件,当运行到断点时会检查当前状态是否满足指定条件,满足才会在此断点暂停。
如下图所示,在断点所在行右键->断点->条件。
接着将弹出下面的对话框,输入“i==5”,则程序会在i==5且经过此断点时暂停。
此外,更多的调试技巧需要进一步学习。
第二部分实验作业及课后习题
Lab1IntroductiontoC++Programming
Objectivities:
1.TowritesimplecomputerprogramsinC++.
2.Towritesimpleinputandoutputstatements.
3.Tousefundamentaltypes.
4.Basiccomputermemoryconcepts.
5.Tousearithmeticoperators.
6.Theprecedenceofarithmeticoperators.
7.Towritesimpledecision-makingstatements
Experiment
●Ex1:
输入书上p38例fig02_03.cpp,熟悉编程环境。
●Ex2:
DescriptionoftheProblem
Writeaprogramthatinputsthreeintegersfromthekeyboard,andprintsthesum,average,product,smallestandlargestofthesenumbers.Thescreendialogueshouldappearasfollows:
[Note:
13,27and14areinputbytheuser.]
SampleOutput
Inputthreedifferentintegers:
132714
Sumis54
Averageis18
Productis4914
Smallestis13
Largestis27
Problem-SolvingTips
1.Prompttheusertoinputthreeintegervalues.Youwilluseasinglecinstatementtoreadallthreevalues.
2.Sometimesitisusefultomakeanassumptiontohelpsolveorsimplifyaproblem.Forexample,weassumenumber1isthelargestofthethreevaluesandassignittolargest.Youwilluseifstatementstodeterminewhethernumber2ornumber3arelarger.
3.Usinganifstatement,comparelargesttonumber2.Ifthecontentofnumber2islarger,thenstorethevariable’svalueinlargest.
4.Usinganifstatement,comparelargesttonumber3.Ifthecontentofnumber3islarger,thenstorethevariable’svalueinlargest.Atthispointyouareguaranteedtohavethelargestvaluestoredinlargest.
5.PerformsimilarstepstothoseinSteps2–4todeterminethesmallestvalue.
6.Writeacoutstatementthatoutputsthesum,average,product(i.e.,multiplication),largestandsmallestvalues.
7.Besuretofollowthespacingandindentationconventionsmentionedinthetext.
8.Ifyouhaveanyquestionsasyouproceed,askyourlabinstructorforassistance.
Follow-UpQuestionsandActivities
1.Modifyyoursolutiontousethreeseparatecinstatementsratherthanone.Writeaseparatepromptforeachcin.
2.Doesitmatterwhether Whichdidyouuseandwhy? ●Ex3: DescriptionoftheProblem Writeaprogramthatreadsintwointegersanddeterminesandprintswhetherthefirstisamultipleofthesecond.[Hint: Usethemodulusoperator.] SampleOutput Entertwointegers: 228 22isnotamultipleof8 Problem-SolvingTips 1.Theinputdataconsistsoftwointegers,soyouwillneedtwointvariablestostoretheinputvalues. 2.Usecintoreadtheuserinputintotheintvariables. 3.Useanifstatementtodeterminewhetherthefirstnumberinputisamultipleofthesecondnumberinput.Usethemodulusoperator,%.Ifonenumberdividesintoanotherevenly,themodulusoperationresultsin0.Iftheresultis0,displayamessageindicatingthatthefirstnumberisamultipleofthesecondnumber. 4.Useanifstatementtodeterminewhetherthefirstnumberinputisnotamultipleofthesecondnumberinput.Ifonenumberdoesnotdivideintoanotherevenly,themodulusoperationresultsinanon-zerovalue.Ifnon-zero,displayamessageindicatingthatthefirstnumberisnotamultipleofthesecond. 5.Besuretofollowthespacingandindentationconventionsmentionedinthetext. 6.Ifyouhaveanyquestionsasyouproceed,askyourlabinstructorforassistance. ●Ex4: ProblemDescription Writeaprogramthatinputsafive-digitnumber,separatesthenumberintoitsindividualdigitsandprintsthedigitsseparatedfromoneanotherbythreespaceseach.[Hint: Useintegerdivisionandthemodulusoperator.]Forexample,iftheuserinputs42339,theprogramshouldprintwhatisshowninthethesampleoutput. SampleOutput 42339 Problem-SolvingTips 1.Theinputdataconsistsofoneinteger,soyouwilluseanintvariable(number)torepresentit.Notethatthedescriptionindicatesthatonefive-digitnumberistobeinput—notfiveseparatedigits. 2.Youwilluseaseriesofstatementsto“breakdown”thenumberintoitsindividualdigitsusingmodulus(%)anddivision(/)calculations. 3.Afterthenumberhasbeeninputusingcin,dividethenumberby10000togettheleftmostdigit.Whydoesthiswork? InC++,dividinganintegerbyanintegerresultsinaninteger.Forexample,42339/10000evaluatesto4because10000dividesevenlyinto42339fourtimes.Theremainder2339istruncated. 4.Changethenumbertoa4-digitnumberusingthemodulusoperator.Thenumbermodulus10000evaluatestotheintegerremainder—inthiscase,theright-mostfourdigits.Forexample,42339%10000resultsin2339.Assigntheresultofthismodulusoperationtothevariablethatstoresthefive-digitnumberinput. 5.Repeatthispatternofdivisionandmodulusreducingthedivisorbyafactorof10eachtime(i.e.,1000,100,10).Afterthenumberischangedtoafour-digitnumber,divide/modulusby1000.Afterthenumberischangedtoathree-digitnumber,divide/modulusby100.Andsoon. 6.Besuretofollowthespacingandindentationconventionsmentionedinthetext. 7.Ifyouhaveanyquestionsasyouproceed,askyourlabinstructorforassistance. Follow-UpQuestionsandActivities 1.Whathappenswhentheuserinputsanumberwhichhasfewerthanfivedigits? Why? Whatistheoutputwhen1763isentered? 2.Theprogramyoucompletedinthislabexerciseinputsanumberwithmultipledigitsandseparatesthedigits。 Writetheinverseprogram,aprogramwhichaskstheuserforthreeone-digitnumbersandcombinesthemintoasinglethree-digitnumber.[Hint: Usemultiplicationandadditiontoformthethree-digitnumber.] . Homework 2.10 2.24 2.27 Lab2IntroductiontoClassesandObjects Objectivities 1.Howtodefineaclassanduseittocreateanobject 2.Howtodefinememberfunctionsinaclasstoimplementtheclass’sbehaviors. 3.Howtodeclaredatamembersinaclasstoimplementtheclass’sattributes. 4.Howtocallamemberfunctionofanobjecttomakethatmemberfunctionperformitstask. 5.Thedifferencesbetweendatamembersofaclassandlocalvariablesofafunction. 6.Howtouseaconstructortoensurethatanobject’sdataisinitializedwhentheobjectiscreated. 7.Howtoengineeraclasstoseparateitsinterfacefromitsimplementationandencouragereuse. Experiments ●Ex1(p.99--3.11) DescriptionoftheProblem ModifyclassGradeBook(Figs.3.11andFigs.3.12).Includeasecondstringdatamemberthatrepresentsthenameofthecourse’sinstructor.Provideasetfunctiontochangetheinstructor’snameandagetfunctiontoretrieveit.Modifytheconstructortospecifytwoparameters—oneforthecoursenameandonefortheinstructor’sname.ModifymemberfunctiondisplayMessagesuchthatitfirstoutputsthewelcomemessageandcoursename,thenoutputs"Thiscourseispresentedby: "followedbytheinstructor’sname.Modifythetestapplicationtodemonstratetheclass’snewcapabilities. SampleOutput Welcometothegradebookfor CS101IntroductiontoC++Programming! Thiscourseispresentedby: SamSmith ChanginginstructornametoJudyJones Welcometothegradebookfor CS101IntroductiontoC++Programming! Thiscourseispresentedby: JudyJones Problem-SolvingTips 1.InclassGradeBook,declareastringdatamembertorepresenttheinstructor’sname. 2.Declareapublicsetfunctionfortheinstructor’snamethatdoesnotreturnavalueandtakesastringasaparameter.Inthebodyofthesetfunction,assigntheparameter’svaluetothedatamemberthatrepresentstheinstructor’sname. 3.Declareapublicgetfunctionthatreturnsastringandtakesnoparameters.This
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 程序设计基础及语言 程序设计 基础 语言 实验 指导 手册
![提示](https://static.bdocx.com/images/bang_tan.gif)