C++课件(精华版)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

2、C++programmingAsimpleC++programTypes,variables,expressionsHowtowriteaprogramFindthewholestepsintherealmodelUsesomegraphornaturelanguagetodescribeRealizewithcomputerlanguageworkflowPseudoCodeAfirstidea:intmain(){variableswhile(condition){analyzetheexpressionevaluatetheexpressionprinttheresult}}4mainFunctionintmain(void){statementsreturn0;}cout“Hello,Tom!”;intx=5;inty=8;intz=x+y;coutx“+”y“=“zendl;5//2.1.cpp#includeiostreamusingnamespacestd;/***AsimpleprogramfordemonstratingthebasicsofaC++project.**ItdoesagoodjobofdemonstratingC++fundamentals,buta*terriblejobwiththepoetry.*/intmain(){coutDon’tyoujustfeellikealouse;coutendl;coutTolearnthatyour\new\theoremwasprovedbyGauss?;coutendl;return0;}6coutexpressionscout“hello,world!”;cout5;coutx;coutx+5;coutx“+5=”x+5;”\n”,“\t”,““,endl,setw(n)setw(n):#includeiomanip.hcout7C++TokensAtokenisthesmallestelementofaC++programthatismeaningfultothecompiler.Kindsoftokens:identifiers,keywords,literals,operators,punctuators,andotherseparators.Tokensareusuallyseparatedbywhitespace.Whitespacecanbeoneormoreblanks,horizontalorverticaltabs,newlines,formfeedsorcomments.8C++Keywordsautoconstdoublefloatintshortstructunsignedunsignedbreakbreakcontinueelseforlongswitchvoidcasesizeoftypedefchardoifreturnstaticunionwhile,ETC.9Commenting/**********************************************nameofprogram*informationofauthor*functionofprogram**********************************************///asampleintmain(){///*thisisinthecommentthisisalsointhecomment*/….}10Constants1,2,31.2,4.50“name”,“your_phonenumber”ture,false0x12‘1’,’A’,’$’,’\xhh’,’\ddd’#definePI3.141592#definePRICE100constintpi=3.141592;11//2.2.cpp#includeiostreamusingnamespacestd;intmain(){intx;inty;x=3;y=4;coutx+yendl;return0;}12VariablesTypesBuilt-intypesBooleantypebool1byteCharactertypeschar1byteIntegertypesint2-4bytes(2)[-32768~32767]short(2)&long(4)[-2147483648~2147483647]Floating-pointtypesdouble8bytesfloat4bytes131byte(8bits)2bytes(16bits)4bytes(32bits)8bytes(64bits)16bytes(128bits)shortcharboolintfloatlongdoublelongdoubleVariablesLiteralsBooleanliterals:boolt;true,falseCharacterliterals:charc;'a','x','4','\n','$‘Integerliterals:intx;0,1,123,-6,0x34,0xa3Floatingpointliterals:doubled;floatf;1.2,13.345,.3,-0.54,1.2e3,.3F,.3FStringliterals:strings;asdf,Howdy,ally'all!”14VariablesNamesChoosemeaningfulnamesconfusemtbf,TLA,myw,nbvShortnamescanbemeaningfulxisalocalvariableiisaloopindexDon'tuselongnamesOk:partial_sum,element_count,staple_partitionToolong:the_number_of_elementsremaining_free_slots_in_the_symbol_table15NotVariablesNamesAnameinaC++programStartswithaletter,containsletters,digits,andunderscores(only)x,number_of_elements,Fourier_transform,z2Notnames:12x,time$to$market,mainlineNotstartnameswithunderscores:_fooNotusekeywordsintifwhile16Declarationandinitializationinta=7;intb=9;charc='a';doublex=1.2;strings1=Hello,world;strings2=1.2;179a1.213Hello,world4|1.2b:c:x:s1:s2:7a:|Constantvariablesconstinti=5;i=6;//error18Thinkabout: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.414e1219Assignmentandincrementinta=7;a=9;a=a+a;a+=2;++a;2079182021a: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#includeiostreamusingnamespacestd;/***Aprogramtoillustratewhathappenswhenlargeintegers*aremultipliedtogether.*/intmain(){intmillion=1000000;inttrillion=million*million;coutAccordingtothiscomputer,millionsquaredistrillion.endl;}22Atype-safetyviolation(“implicitnarrowing”)intmain(){inta=20000;charc=a;intb=c;if(a!=b)coutoops!:a!=b'\n';elsecoutWow!Wehavelargecharacters\n;}2320000a???cC++characterset0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_${}[]#()%:;.?*+/^&|~!=,\‘SPACE24ASCIIcode25CharstointsConvertbetweenacharanditsASCIIintintnum=‘a’;charlet=97;CyclethroughtheASCIItablewithmath!charbee=‘a’+1;26Operators+,++-,--*/%……27ArithmeticAssignmentOperatorsSymbolExampleEquivalent+=x+=1;x=x+1;*=doub*=2;doub=doub*2;-=n-=5;n=n-5;/=third/=3;third=third/3;%=odd%=2;odd=odd%2;28Thinkabout:inta=12;a+=a-=a*a;couta;29ExpressionsBooleantype:bool(trueandfalse)Equalityoperators:==(equal),!=(notequal)Logicaloperators:&&(and),||(or),!(not)Relationaloperators:(lessthan),(greaterthan),=,=Charactertype:char('a','7',and'@')Integertypes:short,int,longarithmeticoperators:+,-,*,/,%Floating-pointtypes:float,double(12.45and1.234e3)arithmeticoperators:+,-,*,///2.5.cppinputandoutput#includeiostreamusingnamespacestd;//Aprogramtoinvestigatethebehaviorofthemod(%)operation.intmain(){inta,b;coutEnterthefirstnumber--;cina;coutEnterthesecondnumber--;cinb;couta%b=a%bendl;return0;}30Thinkabout:17/3=5?5/9=0?intn;n%2+(n+1)%2=?n=2;n++;n=n+1n=2:n++;n--;++n;--n;r=2;m=--n;

1 / 54
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功