




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
AFirstBookofANSIC
FourthEditionChapter3ProcessingandInteractiveInputAFirstBookofANSIC,FourthEdition2ObjectivesAssignmentMathematicalLibraryFunctionsInteractiveInputFormattedOutputAFirstBookofANSIC,FourthEdition3Objectives(continued)SymbolicConstantsCaseStudy:InteractiveInputCommonProgrammingandCompilerErrorsAFirstBookofANSIC,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,FourthEdition61SummaryArithmeticcalculationscanbeperformedusingassignmentstatementsormathematicalfunctionsTh
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- N型成型擠壓機安裝施工組織設(shè)計方案
- 鋁熔鑄工藝知識培訓(xùn)課件
- 燃煤鍋爐范文報告
- 浙江國企招聘2025臺州市華東水產(chǎn)品交易有限公司招聘1人筆試參考題庫附帶答案詳解
- 汽修廠總經(jīng)理報告范文
- 二零二五年度倉儲物流租賃意向協(xié)議書
- 2025年度租賃房屋押金退還協(xié)議書
- 2025年度金融科技公司競業(yè)禁止合作合同
- 二零二五年度荒山承包轉(zhuǎn)讓與林業(yè)生態(tài)保護與恢復(fù)合同
- 二零二五年度全新租賃房屋合同住宅物業(yè)服務(wù)合同
- 離婚協(xié)議書電子版下載
- 中醫(yī)藥膳學(xué)124張課件
- 汽車法規(guī)第一章
- Kappa測試數(shù)據(jù)分析報告
- 礦山地質(zhì)與安全
- 視知覺原理課件
- 建筑工程計量與計價高職PPT完整全套教學(xué)課件
- 網(wǎng)店運營PPT完整全套教學(xué)課件
- 用戶操作手冊-Tagetik合并財務(wù)報表系統(tǒng)實施項目
- 畜牧場經(jīng)營管理
- 【課件】算法及其特征 課件教科版(2019)高中信息技術(shù)必修1
評論
0/150
提交評論