




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AFirstBookofANSIC
FourthEditionChapter3ProcessingandInteractiveInput1AFirstBookofANSIC,FourthEditionObjectivesAssignmentMathematicalLibraryFunctionsInteractiveInputFormattedOutput2AFirstBookofANSIC,FourthEditionObjectives(continued)SymbolicConstantsCaseStudy:InteractiveInputCommonProgrammingandCompilerErrors3AFirstBookofANSIC,FourthEditionAssignmentThegeneralsyntaxforanassignmentstatementis variable=operand;Theoperandtotherightoftheassignmentoperator(=)canbeaconstant,avariable,oranexpressionTheequalsigninCdoesnothavethesamemeaningasanequalsigninalgebralength=25;isread“lengthisassignedthevalue25”Subsequentassignmentstatementscanbeusedtochangethevalueassignedtoavariablelength=3.7;length=6.28;4AFirstBookofANSIC,FourthEditionAssignment(continued)TheoperandtotherightoftheequalsigninanassignmentstatementcanbeavariableoranyvalidCexpression sum=3+7; product=.05*14.6;Thevalueoftheexpressiontotherightof=iscomputedfirstandthenthecalculatedvalueisstoredinthevariabletotheleftof=Variablesusedintheexpressiontotherightofthe=mustbeinitializediftheresultistomakesenseamount+1892=1000+10*5isinvalid!5AFirstBookofANSIC,FourthEditionAssignment(continued)Ifwidthwasnotinitialized,thecomputerusesthevaluethathappenstooccupythatmemoryspacepreviously(compilerwouldprobablyissueawarning)6AFirstBookofANSIC,FourthEditionAssignment(continued)=hasthelowestprecedenceofallthebinaryandunaryarithmeticoperatorsintroducedinSection2.4Multipleassignmentsarepossibleinthesamestatement a=b=c=25;All=operatorshavethesameprecedenceOperatorhasright-to-leftassociativityc=25;b=c;a=b;7AFirstBookofANSIC,FourthEditionImplicitTypeConversionsDatatypeconversionstakeplaceacrossassignmentoperatorsdoubleresult;result=4;//integer4isconvertedto4.0Theautomaticconversionacrossanassignmentoperatoriscalledanimplicittypeconversionintanswer;answer=2.764;//2.764isconvertedto2Heretheimplicitconversionisfromahigherprecisiontoalowerprecisiondatatype;thecompilerwillissueawarning8AFirstBookofANSIC,FourthEditionExplicitTypeConversions(Casts)Theoperatorusedtoforcetheconversionofavaluetoanothertypeisthecastoperator (dataType)expressionwheredataType
isthedesireddatatypeoftheexpressionfollowingthecastExample:Ifsumisdeclaredasdoublesum;,(int)sumistheintegervaluedeterminedbytruncatingsum’sfractionalpart9AFirstBookofANSIC,FourthEditionAssignmentVariationssum=sum+10isnotanequation—itisanexpressionthatisevaluatedintwomajorsteps10AFirstBookofANSIC,FourthEditionAssignmentVariations(continued)11AFirstBookofANSIC,FourthEditionAssignmentVariations(continued)12AFirstBookofANSIC,FourthEditionAssignmentVariations(continued)Assignmentexpressionslikesum=sum+25canbewrittenusingthefollowingoperators:+=-=*=/=%=sum=sum+10canbewrittenassum+=10price*=rateisequivalenttoprice=price*rateprice*=rate+1isequivalenttoprice=price*(rate+1)13AFirstBookofANSIC,FourthEditionAccumulatingThefirststatementinitializessumto0ThisremovesanypreviouslystoredvalueinsumthatwouldinvalidatethefinaltotalApreviouslystorednumber,ifithasnotbeeninitializedtoaspecificandknownvalue,isfrequentlycalledagarbagevalue14AFirstBookofANSIC,FourthEditionAccumulating(continued)15AFirstBookofANSIC,FourthEditionAccumulating(continued)16AFirstBookofANSIC,FourthEditionCountingAcountingstatementisverysimilartotheaccumulatingstatement variable=variable+fixedNumber;Examples:i=i+1;andm=m+2;Incrementoperator(++):variable=variable+1canbereplacedbyvariable++or++variable17AFirstBookofANSIC,FourthEditionCounting(continued)18AFirstBookofANSIC,FourthEditionCounting(continued)19AFirstBookofANSIC,FourthEditionCounting(continued)Whenthe++operatorappearsbeforeavariable,itiscalledaprefixincrementoperator;whenitappearsafteravariable,itiscalledpostfixincrementoperatork=++n;isequivalentton=n+1;//incrementnfirstk=n;//assignn'svaluetokk=n++;isequivalenttok=n;//assignn'svaluetokn=n+1;//andthenincrementn20AFirstBookofANSIC,FourthEditionCounting(continued)Prefixdecrementoperator:theexpressionk=--nfirstdecrementsthevalueofnby1beforeassigningthevalueofntokPostfixdecrementoperator:theexpressionk=n--firstassignsthecurrentvalueofntonandthenreducesthevalueofnby121AFirstBookofANSIC,FourthEditionCounting(continued)22AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions23AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Theargumenttosqrtmustbefloating-pointvalue;passinganintegervalueresultsinacompilererrorReturnvalueisdouble-precisionMustinclude#include<math.h>24AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)25AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)26AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Argumentneednotbeasingleconstant27AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Thestep-by-stepevaluationoftheexpression 3.0*sqrt(5*33-13.91)/5 is(seenextslide)28AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)29AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)Determinethetimeittakesaballtohitthegroundafterithasbeendroppedfroman800-foottowertime=sqrt(2*distance/g),whereg=32.2ft/sec230AFirstBookofANSIC,FourthEditionMathematicalLibraryFunctions(continued)31AFirstBookofANSIC,FourthEditionInteractiveInput32AFirstBookofANSIC,FourthEditionInteractiveInput(continued)Thisprogrammustberewrittentomultiplydifferentnumbersscanf()isusedtoenterdataintoaprogramwhileitisexecuting;thevalueisstoredinavariableItrequiresacontrolstringasthefirstargumentinsidethefunctionnameparentheses33AFirstBookofANSIC,FourthEditionInteractiveInput(continued)Thecontrolstringpassedtoscanf()typicallyconsistsofconversioncontrolsequencesonlyscanf()requiresthatalistofvariableaddressesfollowthecontrolstringscanf("%d",&num1);34AFirstBookofANSIC,FourthEditionInteractiveInput(continued)35AFirstBookofANSIC,FourthEditionInteractiveInput(continued)ThisstatementproducesapromptAddressoperator(&)36AFirstBookofANSIC,FourthEditionInteractiveInput(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,eachcharacterseparatedbyexactlyonespace37AFirstBookofANSIC,FourthEditionInteractiveInput(continued)Inprintingadouble-precisionnumberusingprintf(),theconversioncontrolsequenceforasingle-precisionvariable,%f,canbeusedWhenusingscanf(),ifadouble-precisionnumberistobeentered,youmustusethe%lfconversioncontrolsequencescanf()doesnottestthedatatypeofthevaluesbeingenteredInscanf("%d%f",&num1,&num2),ifuserenters22.87,22isstoredinnum1and.87innum238AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter39AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter(continued)ThefollowingisasamplerunforProgram3.10:Typeinacharacter:mThekeystrokejustacceptedis109Typeinanothercharacter:Thekeystrokejustacceptedis1040AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter(continued)41AFirstBookofANSIC,FourthEditionCaution:ThePhantomNewlineCharacter(continued)42AFirstBookofANSIC,FourthEditionAFirstLookatUser-InputValidation43AFirstBookofANSIC,FourthEditionAFirstLookatUser-InputValidation(continued)Aswritten,Program3.12isnotrobustTheproblembecomesevidentwhenauserentersanon-integervalueEnterthreeintegernumbers:1020.6820Theaverageof10,20,and-858993460is-286331143.333333Handlinginvaliddatainputiscalleduser-inputvalidationValidatingtheentereddataeitherduringorimmediatelyafterthedatahavebeenenteredProvidingtheuserwithawayofreenteringanyinvaliddata44AFirstBookofANSIC,FourthEditionFormattedOutput618124---148Outputisnotaligned45AFirstBookofANSIC,FourthEditionFormattedOutput(continued)618124---148Fieldwidthspecifier46AFirstBookofANSIC,FourthEditionFormattedOutput(continued)47AFirstBookofANSIC,FourthEditionFormatModifiersLeftjustification:printf("%-10d",59);producesthedisplay59٨٨٨٨٨٨٨٨Explicitsigndisplay:printf("%+10d",59);producesthedisplay٨٨٨٨٨٨٨+59Formatmodifiersmaybecombined%-+10dwouldcauseanintegernumbertobothdisplayitssignandbeleft-justifiedinafieldwidthof10spacesTheorderoftheformatmodifiersisnotcritical%+-10disthesame48AFirstBookofANSIC,FourthEditionOtherNumberBases[Optional]Thedecimal(base10)valueof15is15.Theoctal(base8)valueof15is17.Thehexadecimal(base16)valueof15isf.49AFirstBookofANSIC,FourthEditionOtherNumberBases(continued)50AFirstBookofANSIC,FourthEditionOtherNumberBases(continued)Thedecimalvalueoftheletterais97.Theoctalvalueoftheletterais141.Thehexvalueoftheletterais61.51AFirstBookofANSIC,FourthEditionOtherNumberBases(continued)52AFirstBookofANSIC,FourthEditionSymbolicConstantsLiteraldatareferstoanydatawithinaprogramthatexplicitlyidentifiesitselfLiteralvaluesthatappearmanytimesinthesameprogramarecalledmagicnumbersCallowsyoutodefinethevalueoncebyequatingthenumbertoasymbolicname#defineSALESTAX0.05#definePI3.1416Alsocalledsymbolicconstantsandnamedconstants53AFirstBookofANSIC,FourthEditionSymbolicConstants(continued)#signisasignaltoaCpreprocessor54AFirstBookofANSIC,FourthEditionCaseStudy:InteractiveInput55AFirstBookofANSIC,FourthEditionCaseStudy:InteractiveInput(continued)56AFirstBookofANSIC,FourthEditionCommonProgrammingErrorsForgettingtoassigninitialvaluestoallvariablesbeforethevariablesareusedinanexpressionCallingsqrt()withanintegerargumentForgettingtousetheaddressoperator,&,infrontofvariablenamesinascanf()functioncallNotincludingthecorrectcontrolsequencesinscanf()functioncallsforthedatavaluesthatmustbeenteredIncludingamessagewithinthecontrolstringpassedtoscanf()57AFirstBookofANSIC,FourthEditionCommonProgrammingErrors(continued)Terminatinga#definecommandtothepreprocessorwithasemicolonPlacinganequalsignina#definecommandwhenequatingasymbolicconstanttoavalueUsingtheincrementanddecrementoperatorswithvariablesthatappearmorethanonceinthesameexpressionBeingunwillingtotestaprogramindepth58AFirstBookofANSIC,FourthEditionCommonCompilerErrors59AFirstBookofANSIC,FourthEditionCommonCompilerErrors(continued)60AFirstBookofANSIC,FourthEditionSummary
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 买卖小区车位合同样本
- 六年级上册心理健康教学计划
- 配电室日常运行管理制度和维护方案
- 小学二年级数学上册《7的乘法口诀》教学设计
- 东莞2008租赁合同样本
- 框架玻璃幕墙拆除方案
- 人工造林用工合同样本
- 房屋转租合同协议书范本模板【版】
- 消防工程质量控制措施
- 网络维护协议书
- 企业数据资产入表流程研究
- 校外培训机构预收费资金托管协议书范本
- Unit 2 Know your body(说课稿) -2024-2025学年外研版(三起)(2024)英语三年级下册
- 托管老师培训
- 《口语交际:走进他们的童年岁月》(说课稿)2023-2024学年统编版语文五年级下册
- 6.3.1+平面向量基本定理(教学课件)-高一数学(人教A版2019)
- 回填块石施工方案
- GB/T 45156-2024安全与韧性应急管理社区灾害预警体系实施通用指南
- 2025年中国面包行业市场集中度、企业竞争格局分析报告-智研咨询发布
- 酒店的突发事件及案例
- 2025年中国冶金地质总局招聘笔试参考题库含答案解析
评论
0/150
提交评论