C++课件(精华版)_第1页
C++课件(精华版)_第2页
C++课件(精华版)_第3页
C++课件(精华版)_第4页
C++课件(精华版)_第5页
已阅读5页,还剩49页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

2、C+programming,AsimpleC+programTypes,variables,expressions,Howtowriteaprogram,FindthewholestepsintherealmodelUsesomegraphornaturelanguagetodescribeRealizewithcomputerlanguage,workflow,PseudoCode,Afirstidea:intmain()variableswhile(condition)analyzetheexpressionevaluatetheexpressionprinttheresult,4,mainFunction,intmain(void)return0;,intx=5;inty=8;intz=x+y;coutx“+”y“=“zendl;,5,/2.1.cpp,#includeusingnamespacestd;/*AsimpleprogramfordemonstratingthebasicsofaC+project.*ItdoesagoodjobofdemonstratingC+fundamentals,buta*terriblejobwiththepoetry.*/intmain()coutDontyoujustfeellikealouse;coutendl;coutTolearnthatyournewtheoremwasprovedbyGauss?;coutendl;return0;,6,cout,cout,7,C+Tokens,AtokenisthesmallestelementofaC+programthatismeaningfultothecompiler.Kindsoftokens:identifiers,keywords,literals,operators,punctuators,andotherseparators.Tokensareusuallyseparatedbywhitespace.Whitespacecanbeoneormoreblanks,horizontalorverticaltabs,newlines,formfeedsorcomments.,8,C+Keywords,autoconstdoublefloatintshortstructunsignedunsignedbreakbreakcontinueelseforlongswitchvoidcasesizeoftypedefchardoifreturnstaticunionwhile,ETC.,9,Commenting,/*nameofprogram*informationofauthor*functionofprogram*/asampleintmain()/*thisisinthecommentthisisalsointhecomment*/.,10,Constants,1,2,31.2,4.50“name”,“your_phonenumber”ture,false0 x121,A,$,xhh,ddd#definePI3.141592#definePRICE100constintpi=3.141592;,11,/2.2.cpp,#includeusingnamespacestd;intmain()intx;inty;x=3;y=4;coutx+yendl;return0;,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,VariablesNames,Choosemeaningfulnamesconfusemtbf,TLA,myw,nbvShortnamescanbemeaningfulxisalocalvariableiisaloopindexDontuselongnamesOk:partial_sum,element_count,staple_partitionToolong:the_number_of_elementsremaining_free_slots_in_the_symbol_table,15,NotVariablesNames,AnameinaC+programStartswithaletter,containsletters,digits,andunderscores(only)x,number_of_elements,Fourier_transform,z2Notnames:12x,time$to$market,mainlineNotstartnameswithunderscores:_fooNotusekeywordsintifwhile,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:,|,Constantvariables,constinti=5;i=6;/error,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,Assignmentandincrement,inta=7;a=9;a=a+a;a+=2;+a;,20,7,9,18,20,21,a:,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,/2.3.cppAprogramtoillustrateintegeroverflow,#includeusingnamespacestd;/*Aprogramtoillustratewhathappenswhenlargeintegers*aremultipliedtogether.*/intmain()intmillion=1000000;inttrillion=million*million;coutAccordingtothiscomputer,millionsquaredistrillion.endl;,22,Atype-safetyviolation(“implicitnarrowing”),intmain()inta=20000;charc=a;intb=c;if(a!=b)coutoops!:a!=bn;elsecoutWow!Wehavelargecharactersn;,23,C+characterset,0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$#()%:;.?*+/charlet=97;CyclethroughtheASCIItablewithmath!charbee=a+1;,26,Operators,+,+-,-*/%,27,ArithmeticAssignmentOperators,28,Thinkabout:inta=12;a+=a-=a*a;couta;cout;cinb;couta%b=a%bendl;return0;,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,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,Thinkabout:theoutput?,intx,y;x=10;y=x+;coutyendl;inta,b;a=10;b=+a;coutby)a=!k,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);,/boolleapyear(inty),/*anyyeardivisibleby4exceptcentenaryyearsnotdivisibleby400*/boolleapyear(inty)/anyyeardivisibleby4exceptcentenaryyearsnotdivisibleby400if(y%4)returnfalse;if(y%100=0,Conditions2,if(condition)/dothiselse/dothat,if(x0)cout-x;elsecoutb)couta;elsecout=a,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;,if(condition)/dothiselseif(condition)/dothis,conditions4,Switches,switch(expression)casei:/dothisbreak;casej:/dothatbreak;default:/dootherthing,Switchexample,#includeusingnamespacestd;intmain()intx=6;switch(x)case1:coutxis1n;break;case2:case3:coutxis2or3;break;default:coutxisnot1,2,or3;return0;,/weekday,voidweekday(intd)switch(d)case7:coutSunday;b

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论