2、C++programmingAsimpleC++programTypes,variables,expressionsHowtowriteaprogramFindthewholestepsintherealmodelUsesomegraphornaturelanguagetodescribeRealizewithcomputerlanguageworkflowPseudoCodeAfirstidea: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++TokensAtokenisthesmallestelementofaC++programthatismeaningfultothecompiler.Kindsoftokens:identifiers,keywords,literals,operators,punctuators,andotherseparators.Tokensareusuallyseparatedbywhitespace.Whitespacecanbeoneormoreblanks,horizontalorverticaltabs,newlines,formfeedsorcomments.8C++Keywordsautoconstdoublefloatintshortstructunsignedunsignedbreakbreakcontinueelseforlongswitchvoidcasesizeoftypedefchardoifreturnstaticunionwhile,ETC.9Commenting/**********************************************nameofprogram*informationofauthor*functionofprogram**********************************************///asampleintmain(){///*thisisinthecommentthisisalsointhecomment*/….}10Constants1,2,31.2,4.50“name”,“your_phonenumber”ture,false0x12‘1’,’A’,’$’,’\xhh’,’\ddd’#definePI3.141592#definePRICE100constintpi=3.141592;11//2.2.cpp#includeiostreamusingnamespacestd;intmain(){intx;inty;x=3;y=4;coutx+yendl;return0;}12VariablesTypesBuilt-intypesBooleantypebool1byteCharactertypeschar1byteIntegertypesint2-4bytes(2)[-32768~32767]short(2)&long(4)[-2147483648~2147483647]Floating-pointtypesdouble8bytesfloat4bytes131byte(8bits)2bytes(16bits)4bytes(32bits)8bytes(64bits)16bytes(128bits)shortcharboolintfloatlongdoublelongdoubleVariablesLiteralsBooleanliterals:boolt;true,falseCharacterliterals:charc;'a','x','4','\n','$‘Integerliterals:intx;0,1,123,-6,0x34,0xa3Floatingpointliterals:doubled;floatf;1.2,13.345,.3,-0.54,1.2e3,.3F,.3FStringliterals:strings;asdf,Howdy,ally'all!”14VariablesNamesChoosemeaningfulnamesconfusemtbf,TLA,myw,nbvShortnamescanbemeaningfulxisalocalvariableiisaloopindexDon'tuselongnamesOk:partial_sum,element_count,staple_partitionToolong:the_number_of_elementsremaining_free_slots_in_the_symbol_table15NotVariablesNamesAnameinaC++programStartswithaletter,containsletters,digits,andunderscores(only)x,number_of_elements,Fourier_transform,z2Notnames:12x,time$to$market,mainlineNotstartnameswithunderscores:_fooNotusekeywordsintifwhile16Declarationandinitializationinta=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:Thinkaboutinta=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++characterset0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_${}[]#()%:;.?*+/^&|~!=,\‘SPACE24ASCIIcode25CharstointsConvertbetweenacharanditsASCIIintintnum=‘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;29ExpressionsBooleantype:bool(trueandfalse)Equalityoperators:==(equal),!=(notequal)Logicaloperators:&&(and),||(or),!(not)Relationaloperators:(lessthan),(greaterthan),=,=Charactertype:char('a','7',and'@')Integertypes:short,int,longarithmeticoperators:+,-,*,/,%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+1n=2:n++;n--;++n;--n;r=2;m=--n;