版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AFirstBookofANSIC
FourthEditionChapter3ProcessingandInteractiveInputAFirstBookofANSIC,FourthEdition2ObjectivesAssignmentMathematicalLibraryFunctionsInteractiveInputFormattedOutputAFirstBookofANSIC,FourthEdition3Objectives(continued)SymbolicConstantsCaseStudy:InteractiveInputCommonProgrammingandCompilerErrorsAFirstBookofANSIC,FourthEdition4AssignmentThegeneralsyntaxforanassignmentstatementis variable=operand;Theoperandtotherightoftheassignmentoperator(=)canbeaconstant,avariable,oranexpressionTheequalsigninCdoesnothavethesamemeaningasanequalsigninalgebralength=25;isread“lengthisassignedthevalue25”Subsequentassignmentstatementscanbeusedtochangethevalueassignedtoavariablelength=3.7;length=6.28;AFirstBookofANSIC,FourthEdition5Assignment(continued)TheoperandtotherightoftheequalsigninanassignmentstatementcanbeavariableoranyvalidCexpression sum=3+7; product=.05*14.6;Thevalueoftheexpressiontotherightof=iscomputedfirstandthenthecalculatedvalueisstoredinthevariabletotheleftof=Variablesusedintheexpressiontotherightofthe=mustbeinitializediftheresultistomakesenseamount+1892=1000+10*5isinvalid!AFirstBookofANSIC,FourthEdition6Assignment(continued)Ifwidthwasnotinitialized,thecomputerusesthevaluethathappenstooccupythatmemoryspacepreviously(compilerwouldprobablyissueawarning)AFirstBookofANSIC,FourthEdition7Assignment(continued)=hasthelowestprecedenceofallthebinaryandunaryarithmeticoperatorsintroducedinSection2.4Multipleassignmentsarepossibleinthesamestatement a=b=c=25;All=operatorshavethesameprecedenceOperatorhasright-to-leftassociativityc=25;b=c;a=b;AFirstBookofANSIC,FourthEdition8ImplicitTypeConversionsDatatypeconversionstakeplaceacrossassignmentoperatorsdoubleresult;result=4;//integer4isconvertedto4.0Theautomaticconversionacrossanassignmentoperatoriscalledanimplicittypeconversionintanswer;answer=2.764;//2.764isconvertedto2Heretheimplicitconversionisfromahigherprecisiontoalowerprecisiondatatype;thecompilerwillissueawarningAFirstBookofANSIC,FourthEdition9ExplicitTypeConversions(Casts)Theoperatorusedtoforcetheconversionofavaluetoanothertypeisthecastoperator (dataType)expressionwheredataType
isthedesireddatatypeoftheexpressionfollowingthecastExample:Ifsumisdeclaredasdoublesum;,(int)sumistheintegervaluedeterminedbytruncatingsum’sfractionalpartAFirstBookofANSIC,FourthEdition10AssignmentVariationssum=sum+10isnotanequation—itisanexpressionthatisevaluatedintwomajorstepsAFirstBookofANSIC,FourthEdition11AssignmentVariations(continued)AFirstBookofANSIC,FourthEdition12AssignmentVariations(continued)AFirstBookofANSIC,FourthEdition13AssignmentVariations(continued)Assignmentexpressionslikesum=sum+25canbewrittenusingthefollowingoperators:+=-=*=/=%=sum=sum+10canbewrittenassum+=10price*=rateisequivalenttoprice=price*rateprice*=rate+1isequivalenttoprice=price*(rate+1)AFirstBookofANSIC,FourthEdition14AccumulatingThefirststatementinitializessumto0ThisremovesanypreviouslystoredvalueinsumthatwouldinvalidatethefinaltotalApreviouslystorednumber,ifithasnotbeeninitializedtoaspecificandknownvalue,isfrequentlycalledagarbagevalueAFirstBookofANSIC,FourthEdition15Accumulating(continued)AFirstBookofANSIC,FourthEdition16Accumulating(continued)AFirstBookofANSIC,FourthEdition17CountingAcountingstatementisverysimilartotheaccumulatingstatement variable=variable+fixedNumber;Examples:i=i+1;andm=m+2;Incrementoperator(++):variable=variable+1canbereplacedbyvariable++or++variableAFirstBookofANSIC,FourthEdition18Counting(continued)AFirstBookofANSIC,FourthEdition19Counting(continued)AFirstBookofANSIC,FourthEdition20Counting(continued)Whenthe++operatorappearsbeforeavariable,itiscalledaprefixincrementoperator;whenitappearsafteravariable,itiscalledpostfixincrementoperatork=++n;isequivalentton=n+1;//incrementnfirstk=n;//assignn'svaluetokk=n++;isequivalenttok=n;//assignn'svaluetokn=n+1;//andthenincrementnAFirstBookofANSIC,FourthEdition21Counting(continued)Prefixdecrementoperator:theexpressionk=--nfirstdecrementsthevalueofnby1beforeassigningthevalueofntokPostfixdecrementoperator:theexpressionk=n--firstassignsthecurrentvalueofntonandthenreducesthevalueofnby1AFirstBookofANSIC,FourthEdition22Counting(continued)AFirstBookofANSIC,FourthEdition23MathematicalLibraryFunctionsAFirstBookofANSIC,FourthEdition24MathematicalLibraryFunctions(continued)Theargumenttosqrtmustbefloating-pointvalue;passinganintegervalueresultsinacompilererrorReturnvalueisdouble-precisionMustinclude#include<math.h>AFirstBookofANSIC,FourthEdition25MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition26MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition27MathematicalLibraryFunctions(continued)ArgumentneednotbeasingleconstantAFirstBookofANSIC,FourthEdition28MathematicalLibraryFunctions(continued)Thestep-by-stepevaluationoftheexpression 3.0*sqrt(5*33-13.91)/5 is(seenextslide)AFirstBookofANSIC,FourthEdition29MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition30MathematicalLibraryFunctions(continued)Determinethetimeittakesaballtohitthegroundafterithasbeendroppedfroman800-foottowertime=sqrt(2*distance/g),whereg=32.2ft/sec2AFirstBookofANSIC,FourthEdition31MathematicalLibraryFunctions(continued)AFirstBookofANSIC,FourthEdition32InteractiveInputAFirstBookofANSIC,FourthEdition33InteractiveInput(continued)Thisprogrammustberewrittentomultiplydifferentnumbersscanf()isusedtoenterdataintoaprogramwhileitisexecuting;thevalueisstoredinavariableItrequiresacontrolstringasthefirstargumentinsidethefunctionnameparenthesesAFirstBookofANSIC,FourthEdition34InteractiveInput(continued)Thecontrolstringpassedtoscanf()typicallyconsistsofconversioncontrolsequencesonlyscanf()requiresthatalistofvariableaddressesfollowthecontrolstringscanf("%d",&num1);AFirstBookofANSIC,FourthEdition35InteractiveInput(continued)AFirstBookofANSIC,FourthEdition36InteractiveInput(continued)ThisstatementproducesapromptAddressoperator(&)AFirstBookofANSIC,FourthEdition37InteractiveInput(continued)scanf()canbeusedtoentermanyvaluesscanf("%f%f",&num1,&num2);//"%f%f"isthesameAspacecanaffectwhatthevaluebeingenterediswhenscanf()isexpectingacharacterdatatypescanf("%c%c%c",&ch1,&ch2,&ch3);storesthenextthreecharacterstypedinthevariablesch1,ch2,andch3;ifyoutypexyz,thenxisstoredinch1,ablankisstoredinch2,andyisstoredinch3scanf("%c%c%c",&ch1,&ch2,&ch3);causesscanf()tolookforthreecharacters,eachcharacterseparatedbyexactlyonespaceAFirstBookofANSIC,FourthEdition38InteractiveInput(continued)Inprintingadouble-precisionnumberusingprintf(),theconversioncontrolsequenceforasingle-precisionvariable,%f,canbeusedWhenusingscanf(),ifadouble-precisionnumberistobeentered,youmustusethe%lfconversioncontrolsequencescanf()doesnottestthedatatypeofthevaluesbeingenteredInscanf("%d%f",&num1,&num2),ifuserenters22.87,22isstoredinnum1and.87innum2AFirstBookofANSIC,FourthEdition39Caution:ThePhantomNewlineCharacterAFirstBookofANSIC,FourthEdition40Caution:ThePhantomNewlineCharacter(continued)ThefollowingisasamplerunforProgram3.10:Typeinacharacter:mThekeystrokejustacceptedis109Typeinanothercharacter:Thekeystrokejustacceptedis10AFirstBookofANSIC,FourthEdition41Caution:ThePhantomNewlineCharacter(continued)AFirstBookofANSIC,FourthEdition42Caution:ThePhantomNewlineCharacter(continued)AFirstBookofANSIC,FourthEdition43AFirstLookatUser-InputValidationAFirstBookofANSIC,FourthEdition44AFirstLookatUser-InputValidation(continued)Aswritten,Program3.12isnotrobustTheproblembecomesevidentwhenauserentersanon-integervalueEnterthreeintegernumbers:1020.6820Theaverageof10,20,and-858993460is-286331143.333333Handlinginvaliddatainputiscalleduser-inputvalidationValidatingtheentereddataeitherduringorimmediatelyafterthedatahavebeenenteredProvidingtheuserwithawayofreenteringanyinvaliddataAFirstBookofANSIC,FourthEdition45FormattedOutput618124---148OutputisnotalignedAFirstBookofANSIC,FourthEdition46FormattedOutput(continued)618124---148FieldwidthspecifierAFirstBookofANSIC,FourthEdition47FormattedOutput(continued)AFirstBookofANSIC,FourthEdition48FormatModifiersLeftjustification:printf("%-10d",59);producesthedisplay59٨٨٨٨٨٨٨٨Explicitsigndisplay:printf("%+10d",59);producesthedisplay٨٨٨٨٨٨٨+59Formatmodifiersmaybecombined%-+10dwouldcauseanintegernumbertobothdisplayitssignandbeleft-justifiedinafieldwidthof10spacesTheorderoftheformatmodifiersisnotcritical%+-10disthesameAFirstBookofANSIC,FourthEdition49OtherNumberBases[Optional]Thedecimal(base10)valueof15is15.Theoctal(base8)valueof15is17.Thehexadecimal(base16)valueof15isf.AFirstBookofANSIC,FourthEdition50OtherNumberBases(continued)AFirstBookofANSIC,FourthEdition51OtherNumberBases(continued)Thedecimalvalueoftheletterais97.Theoctalvalueoftheletterais141.Thehexvalueoftheletterais61.AFirstBookofANSIC,FourthEdition52OtherNumberBases(continued)AFirstBookofANSIC,FourthEdition53SymbolicConstantsLiteraldatareferstoanydatawithinaprogramthatexplicitlyidentifiesitselfLiteralvaluesthatappearmanytimesinthesameprogramarecalledmagicnumbersCallowsyoutodefinethevalueoncebyequatingthenumbertoasymbolicname#defineSALESTAX0.05#definePI3.1416AlsocalledsymbolicconstantsandnamedconstantsAFirstBookofANSIC,FourthEdition54SymbolicConstants(continued)#signisasignaltoaCpreprocessorAFirstBookofANSIC,FourthEdition55CaseStudy:InteractiveInputAFirstBookofANSIC,FourthEdition56CaseStudy:InteractiveInput(continued)AFirstBookofANSIC,FourthEdition57CommonProgrammingErrorsForgettingtoassigninitialvaluestoallvariablesbeforethevariablesareusedinanexpressionCallingsqrt()withanintegerargumentForgettingtousetheaddressoperator,&,infrontofvariablenamesinascanf()functioncallNotincludingthecorrectcontrolsequencesinscanf()functioncallsforthedatavaluesthatmustbeenteredIncludingamessagewithinthecontrolstringpassedtoscanf()AFirstBookofANSIC,FourthEdition58CommonProgrammingErrors(continued)Terminatinga#definecommandtothepreprocessorwithasemicolonPlacinganequalsignina#definecommandwhenequatingasymbolicconstanttoavalueUsingtheincrementanddecrementoperatorswithvariablesthatappearmorethanonceinthesameexpressionBeingunwillingtotestaprogramindepthAFirstBookofANSIC,FourthEdition59CommonCompilerErrorsAFirstBookofANSIC,FourthEdition60CommonCompilerErrors(continued)AFirstBookofANSIC,FourthEdition61Summary
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年部编版初中历史市场经济体制演进试卷及答案
- 2026年自身抗体检测在系统性红斑狼疮诊断中的应用习题集及答案
- 上海市沪教版初中英语九年级下册第10单元听力练习题及答案
- 营业税改征增值税政策辅导生活服务业
- 安徽皖南八校2026-2027学年高三上学期10月考试语文试卷
- 河北环境岗2026事业编面试自测卷例题试卷含解析
- 水利岗2026面试自测卷事业编面试配套答题思路
- 77 第九章 第71课时 一元线性回归模型及其应用 - 副本
- 河北综合岗2026结构化面试模拟练习含答案
- 读书使人充实谈话使人敏捷讨论使人机智数学使人聪明
- 驾驶人员技能培训课件
- 2025年中职(动漫与游戏制作)二维动画制作试题及答案
- 亮剑的团队精神
- 儿童电除颤教学课件
- 劳动监察培训课件
- 东北林业大学《生物科学专业英语》2023-2024学年第二学期期末试卷
- 建筑物拆除工程合同模板
- DB32T 3262-2017 移动泵机组运行与维护规范
- 会计基础第四版 课件 项目四 运用借贷记账法
- 2024年全国职业院校技能大赛高职组(社区服务实务赛项)考试题库(含答案)
- 计算机网络的发展
评论
0/150
提交评论