C++課件(精華版)ppt課件_第1頁
C++課件(精華版)ppt課件_第2頁
C++課件(精華版)ppt課件_第3頁
C++課件(精華版)ppt課件_第4頁
C++課件(精華版)ppt課件_第5頁
已閱讀5頁,還剩49頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

2、C+programming,AsimpleC+programTypes,variables,expressions,1,Howtowriteaprogram,FindthewholestepsintherealmodelUsesomegraphornaturelanguagetodescribeRealizewithcomputerlanguage,2,workflow,3,PseudoCode,Afirstidea:intmain()variableswhile(condition)analyzetheexpressionevaluatetheexpressionprinttheresult,4,4,mainFunction,intmain(void)return0;,intx=5;inty=8;intz=x+y;coutx“+”y“=“zendl;,5,5,/2.1.cpp,#includeusingnamespacestd;/*AsimpleprogramfordemonstratingthebasicsofaC+project.*ItdoesagoodjobofdemonstratingC+fundamentals,buta*terriblejobwiththepoetry.*/intmain()coutDontyoujustfeellikealouse;coutendl;coutTolearnthatyournewtheoremwasprovedbyGauss?;coutendl;return0;,6,6,cout,cout,7,7,C+Tokens,AtokenisthesmallestelementofaC+programthatismeaningfultothecompiler.Kindsoftokens:identifiers,keywords,literals,operators,punctuators,andotherseparators.Tokensareusuallyseparatedbywhitespace.Whitespacecanbeoneormoreblanks,horizontalorverticaltabs,newlines,formfeedsorcomments.,8,8,C+Keywords,autoconstdoublefloatintshortstructunsignedunsignedbreakbreakcontinueelseforlongswitchvoidcasesizeoftypedefchardoifreturnstaticunionwhile,ETC.,9,9,Commenting,/*nameofprogram*informationofauthor*functionofprogram*/asampleintmain()/*thisisinthecommentthisisalsointhecomment*/.,10,10,Constants,1,2,31.2,4.50“name”,“your_phonenumber”ture,false0 x121,A,$,xhh,ddd#definePI3.141592#definePRICE100constintpi=3.141592;,11,11,/2.2.cpp,#includeusingnamespacestd;intmain()intx;inty;x=3;y=4;coutx+yendl;return0;,12,12,VariablesTypes,Built-intypesBooleantypebool1byteCharactertypeschar1byteIntegertypesint2-4bytes(2)-3276832767short(2)true,falseCharacterliterals:charc;a,x,4,n,$Integerliterals:intx;0,1,123,-6,0 x34,0 xa3Floatingpointliterals:doubled;floatf;1.2,13.345,.3,-0.54,1.2e3,.3F,.3FStringliterals:strings;asdf,Howdy,allyall!”,14,14,VariablesNames,Choosemeaningfulnamesconfusemtbf,TLA,myw,nbvShortnamescanbemeaningfulxisalocalvariableiisaloopindexDontuselongnamesOk:partial_sum,element_count,staple_partitionToolong:the_number_of_elementsremaining_free_slots_in_the_symbol_table,15,15,NotVariablesNames,AnameinaC+programStartswithaletter,containsletters,digits,andunderscores(only)x,number_of_elements,Fourier_transform,z2Notnames:12x,time$to$market,mainlineNotstartnameswithunderscores:_fooNotusekeywordsintifwhile,16,16,Declarationandinitialization,inta=7;intb=9;charc=a;doublex=1.2;strings1=Hello,world;strings2=1.2;,17,9,a,1.2,13Hello,world,4|1.2,b:,c:,x:,s1:,s2:,7,a:,|,17,Constantvariables,constinti=5;i=6;/error,18,18,Thinkabout:,inta,b,c=2;intx,y,z,10;intm=2;intn=3;longintsum=0,add;longhello;chara=m;charb,c,d;charm=65,n=a+1;floata,b,ccc=3.1415;floatsum=0.0;doublef1,f2=1.414e12,19,19,Assignmentandincrement,inta=7;a=9;a=a+a;a+=2;+a;,20,7,9,18,20,21,a:,20,Thinkabout,inta=10,b;b=a;floatx;intk=300;x=k;floatx=3.14;intn;n=x+6;floatx=3.14;intn;n=3;coutx+n;3.0/9or(float)3/9,21,/2.3.cppAprogramtoillustrateintegeroverflow,#includeusingnamespacestd;/*Aprogramtoillustratewhathappenswhenlargeintegers*aremultipliedtogether.*/intmain()intmillion=1000000;inttrillion=million*million;coutAccordingtothiscomputer,millionsquaredistrillion.endl;,22,22,Atype-safetyviolation(“implicitnarrowing”),intmain()inta=20000;charc=a;intb=c;if(a!=b)coutoops!:a!=bn;elsecoutWow!Wehavelargecharactersn;,23,23,C+characterset,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$#()%:;.?*+/charlet=97;CyclethroughtheASCIItablewithmath!charbee=a+1;,26,26,Operators,+,+-,-*/%,27,27,ArithmeticAssignmentOperators,28,Thinkabout:inta=12;a+=a-=a*a;couta;cout;cinb;couta%b=a%bendl;return0;,30,30,Thinkabout:,17/3=5?5/9=0?intn;n%2+(n+1)%2=?n=2;n+;n=n+1n=2:n+;n-;+n;-n;r=2;m=-n;p=r+;m=10,n=100;p=(n+n,n*n,n-2);p=n+n,n*n,n-2;,31,31,Thinkabout:,inti=2;(i+)+(i+)+(i+);couti;i=2;(-i)+(-i);couti;i=2;i=(i+i+i);couti;i=2;i=(i-i);coutsecond;stringname=first+second;coutHello,namen;,33,33,Thinkabout:theoutput?,intx,y;x=10;y=x+;coutyendl;inta,b;a=10;b=+a;coutby)a=!k,37,37,38,Statements,a=b;doubled2=2.5;if(x=2)y=4;while(true)cout“hello”;for(inti=0;i8;i+)coutx;if(x0)x=-x;coutx;coutsqrt(x);,43,/boolleapyear(inty),/*anyyeardivisibleby4exceptcentenaryyearsnotdivisibleby400*/boolleapyear(inty)/anyyeardivisibleby4exceptcentenaryyearsnotdivisibleby400if(y%4)returnfalse;if(y%100=0,44,Conditions2,if(condition)/dothiselse/dothat,if(x0)cout-x;elsecoutb)couta;elsecout=a,46,Conditions3,if(condition)/dothiselseif(condition)/dothatelse/dootherthing,voidmain()inta=3,b=17,c=5;if(ab)if(bc)coutc)coutc)coutc)coutbca;elsecoutcb0y=f(x)=0 x=0-1x0)k=1;elseif(x=0)k=0;elsek=-1;coutxb?a:b;returnm;,49,if(condition)/dothiselseif(condition)/dothis,conditions4,50,Switches,switch(expression)casei:/dothisbreak;casej:/dothatbreak;default:/dootherthing,51,Switchexample,#includeusingnamespacestd;intmain()intx=6;switch(x)case1:coutxis1n;break;case2:case3:coutxis2or3;break;default:coutxisnot1,2,or3;return0;,52,/weekday,voidweekday(intd)switch(d)case7:coutSunday;br

溫馨提示

  • 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)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論