PRG 155 Course Notes.docx
- 文档编号:30431741
- 上传时间:2023-08-15
- 格式:DOCX
- 页数:68
- 大小:45.51KB
PRG 155 Course Notes.docx
《PRG 155 Course Notes.docx》由会员分享,可在线阅读,更多相关《PRG 155 Course Notes.docx(68页珍藏版)》请在冰豆网上搜索。
PRG155CourseNotes
PRG155
AnIntroductiontoCProgamming
CourseNotes
September2012
TopicHeadings
Page
Introduction3
DownloadingandUsingMicrosoftVisualStudio2010Express6
Topic1-Printing9
Topic2-VariablesandIdentifiers12
Topic3-GettingUserInput17
Topic4-DecisionMakingIf-Else26
Topic5-Loops37
Topic6-Functions44
Topic7-DefinesandMacros54
Topic8-UserDefinedHeaderFiles56
Topic9-DebuggingandTesting60
Introduction
WhatisthepurposeoftheCourseSupplement?
ThisCourseSupplementisNOTatextbook.ItisNOTintendedtocoverallthematerialthatwillbepresentedinyourlecturesorlabs.ItisNOTintendedtoreplacelecturesorcovereverythingyouneedtoknowtobecomeaproficientCprogrammer.
Itisabriefsummaryofsomeofthematerialthatyouwillbetaughtinclass.Itisintendedallowyoutoconcentrateontheconceptsandideasbeingpresentedintheclasswithoutyouhavingtowritedowneverythingthatissaidbyyourprofessor.Itsavesyoufromhavingtocopyeveryprogrampresentedandprovidesexamplesofprogramcodeyouwillneedtobefamiliarwith.
TheSupplementallowsyoutoreaduponthematerialthatwillbepresentedinupcomingclassesandhelpsyoureviewthematerialthatwascoveredinpreviousclasses.YoumaywanttotrytheexamplesprovidedheretohelpyoulearnthesyntaxoftheClanguage.
Youshouldbringthesenotestoyourclassesandlabs.IntheclassyoucanmakenotesintheSupplementifyouseefit.Inthelabitwillhelpyoureviewconceptsandtoassistyouinwriteprogramsandremindyouofprogramdetailsthataredifficulttorememberwhenyoustartout.
GabrielHetiSeptember2012
Whatisprogramming
Acomputerprogramisasequenceofinstructionsthatacomputercanunderstandandactupontocarryoutausefultask.Computerprogramsalsocontroltheoperationofmanyelectronicdevicessuchassmartphones,microwaveovens,carsandofcoursearewhatmakescomputerssuchusefultools.Simplecomputerswithlimitedmemorywerecriticaltothesuccessofthelandingonthemoonin1969.
Acomputerprogramisasequenceofinstructionswrittenbyprogrammerstocarryoutaspecifictask.Itmaybeassimpleasoperatingacalculatororascomplexasanautonomousrobot.Computersandtheirprogramscaninteractwiththeoutsideworldthroughkeyboards,mice,touchscreens,voicecommandsandelectricalconnectionsandcancommunicatebacktotheoutsideworldthroughtheirmonitors,printersandthroughvariousports.Theseportsmaybeusedtoreceiveinputsandtocontrolexternaldevicessuchasalarmsystemsorthearmsofmanufacturingrobots.
Almostallcomputerprogramswillhavethreekeyelements.
1.Input–thisallowstheusertoentervaluestobeusedbythecomputerormaybesensorinputsfromanalarmsystemorGPSinputtoprovidelocationinformation.
2.Calculationorprocessing–Theprogramnormallywillreacttoinputsitreceivestocarryoutitsintendedtask.Thisnormallyinvolvesoneofmoredecisionsandcalculations.
3.Output–thisistheactionthecomputertakesinresponsetotheinputitreceives.Itmaysimplybethedisplayoftheresultsofacalculationonthescreen(Thisisallyouwillbedoingthisterm).Infuturecoursesoratworkyourprogrammaycontrolabankofelevatorsortheoperationofanuclearpowerstation.
Normallytherequirementsofaprogramwillbegiventoyouasaproblemtobesolved.Thefirstthingyoumustdoisunderstandtheproblem.Figureoutwhatinputsyou’llneed,whatoutputyouarerequiredtoproduceandwhatcalculationorprocessyoumustfollowtoarriveatasolution.Itisagoodideatowritedownyourinputs,processandoutputinyourownwordstohelpyouwithactuallywritingtheprogram.
OnceyouunderstandhowyouwillarriveatyoursolutionyoumusttranslatethatsolutionintoC,thelanguageyouwillbelearningandintoasequenceofinstructionsthatwillgiveyouthecorrectresult.
Hereisanexample.
Let’ssayyouwanttowriteaprogramthatwillallowsomeonetocalculatehowmanypoundsofsomethingtheycanbuyforsomeamountofmoneytheywanttospend.Thiswillofcoursedependonthewhatthatsomethingcostsperkilogram.
Ifyouanalyzethisquestionyou’llrecognizethatyouneedtoknowtwothings.
1.Howmuchmoneythepersonhasorwantstospend.
2.Howmuchtheitemcostsperkilogram.
Sothosearegoingtobeyourinputs.
Nextyoumustdoacalculation.Tofigureouthowmanykilogramsyoucanbuyyouneedtodividetheavailablefundsbythecostperkilogramoftheitem.
Andfinallyyoumustprinttheresulttothescreen.
ThisthenhastobeconvertedtoaCprogramthatthecomputerwillunderstand.
Onceyou’vetakentheabovestepsyoucanthenwritetheprogrameitheronpaperordirectlycodeitonthecomputerusingVisualC++,theprogrammingenvironmentandcompilerwewillbeusing.
Pleaserememberonething.AcomputerisatheartaFASTIDIOT!
First,itwilldoEXACTLYwhatyoutellit.Andseconditisincapableofrecognizingoracceptingthesimplesterroryoumaymakesuchasleavingoutacomma,anapostropheorsemi-colon.Whenpresentedwithsuchanerroritwillsimplystoporgiveyouincorrectanswers.
Inordertolearnthelanguage,syntaxand“grammar”ofanycomputerlanguageyoumustPRACTICEasmuchaspossiblejustlikelearninganylanguage.ItistheONLYwaytolearnCanddowellinthiscourse.
DownloadingandUsingMicrosoftVisualStudio2010Express
AfreeversionofMicrosoftVisualStudio2010ExpressforC++maybedownloadedfreeofcharge.Theprogramisthesameasorverysimilartowhatwewillbeusinginthelab.DoaGooglesearchfor“MicrosoftVisualStudioExpress2010”
Aftertheprogramdownloadsonyourcomputer,clickonittosetuptheprogram.Youneedtothenregistertheprogramwithinthirtydays.
A.Tocreatethefirstproject
1.OpenVC++bydoubleclickingtheVC++icononthedesktop.
2.ClickonFile/New/ProjectorNewProjectifyouseetheStartWindow
3.SelectC++onleft(ifvisible)..
4.ClickonceonWin32ontheleft,thenWin32ConsoleApplication.
5.InNAME(below)enteranameforthealltheprojectsthatdaysuchasWeek1
6.Checkthe“Createdirectoryforsolution”boxundertheBrowsebutton.
7.LeaveLocationasisorselectalocationtostoreyourprojectfiles(suchasyourdesktop).
8.ClickOKorEnter.
10.IntheWin32ApplicationWizardWindowwhichpopsupclickonApplicationSettings.
11.CheckConsoleApplication,CheckEmptyProjectandclickonFinish
12.TheEditWindowshouldopenandyoushouldseeSolutionExplorerattheleftorright
andtheEditwindowfillingmostoftherestofthescreen.
12a.Ifit’snotopenClickonView/OtherWindows/SolutionExplorer
13.YoushouldalsoseetheOuputwindoworErrorWindowbelowtheEditwindow.
13a.Ifyoudon't,clickonView/OtherWindows/ErrorList.
14.UnderSolutionWeek1youshouldseeWeek1withseveralfoldsersbelowit,includingExternal
Dependencies,HeaderFiles,ResourceFilesandSourceFiles
15.RightClickSourceFilesandselectAdd,NewItem(orExistingItemto
continueworkingonanexistingprojectortodemonstrateanexistingprogram)
16.SelectC++FileandgiveitanamesuchasLab1_1.candEnter.
DONTFORGETTHE".C"EXTENSION
17.ThenenteryourCprogramintheEditwindow..
B.Tostartasecond,third,fourthetcprojects
EachprojectcanonlyhaveONEsourcecodefileassociatedwithit.SoifyouwanttowriteasecondprogramyouneedtostartanewProjectordeletethefirstsourcecodeandcopyanewsoucecodefileintoit.
TostartanewProject
1.RightclickSolution(Lab1),thenselectAdd,NewProject(orExistingProject)
2.IfyouselectNewProjectcontinuefromStep3above
3.CallyournewprojectsomethinglikeLab1_2
C.Toselectwhichprojecttorunorworkon
IfyouhavemorethanoneProjectintheSolutionWindowyoumustselectwhichoneyouwanttoworkon.Todothat:
1.RightclickontheProjectyouwanttoworkonsuchasLab1_1
2.ClickonSetasStartupProject
3.TheactiveProjectnameshouldnowbeBolded
4.DoubleclickonthenewSourcefileeg.Lab2_1.ctoloadintotheEditwindow
5.Youcannowrunorworkonthisproject’ssourcecode
D.Savingandretrievingyoursourcecode
1.Ifyouwanttosaveyoursourcecode,clickontheEditwindow.
2.ClickonFile,Savexxx.cAs
3.Enterthelocationwhereyouwanttosaveyourcodeandanamesuchas
d:
\\lab1_1.c
OR
Youcansimplyhighlightthecodeyouwanttosave,copyit,andthenpasteitintoNotepadoranotherwordprocessingprograminTEXTMODE.Youcanthencopyandthecodeandpasteitintoyoueditwindowtoberunifyouwanttoloadthesameprogram.
BothmethodswillsaveyourCsourcecodetoberetrievedlater.
ManagingDirectorieswithMicrosoftVisualStudio2010Express(VS)
UsingVSisnotasimplematter.LearningtouseitmaybemoredifficultatfirstthanwritingCprograms.
Hereisanoverview.
VSorganizesworkintoSolutions,ProjectsandSourceCodeFiles.EverythingisautomaticallysavedtoadirectorywhenaProjectisrun.AProjectconsistsofCsourcecodeandpossiblyotherfilesthatyoumayneedlaterinthecourse.
ThedefaultdirectorywheretheSolutionisautomaticallysavedisintheProgramfileinthefilewhereyouinstalledVS.ThedirectorymaybechangedhowevereachtimewhenyoustartupVC.
SinceVisualStudioautomaticallysavesyourentireprojectwhenyouruntheprogram,ifyouwishtoprotectyourcodeinthel
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- PRG 155 Course Notes
![提示](https://static.bdocx.com/images/bang_tan.gif)