資源描述:
《算式由一個或多個運算元(operand)構成并傳回值(結果)》由會員上傳分享,免費在線閱讀,更多相關內容在行業(yè)資料-天天文庫。
1、Expressions(算式)Expressions(算式)算式由一個或多個運算元(operand)構成並傳回值(結果)算式的形式可能有……a+b;3.14159;var;a>b;ArithematicOperators(算術運算子)addition+subtraction?multiplication*division/modulus%ArithematicOperators(算術運算子)SyntaxoperandoperatoroperandExample7+1534—18992*31345/6.0286%3*Modulus12/314/312%314%3Themodulus
2、operatoryieldstheremainderofintegerdivision.**43121204314122C++ModulusExample18%4=213%4is117%3=235%47is3524%6=024%4is04%18=40%7is012%2.5error6.0%6error***intmain(){cout<<“18%4=“<<18%4;}OperatorsPrecedence(優(yōu)先權)-EvaluationTreesEvaluationTreesii10/2*310%3-4/2iii5.0*2.0/4.0*2.0rrr5*2/(4.0*2.0)irrO
3、peratorsOverloadOperatoroverloadUsingthesamesymbolformorethanoneoperation整數(shù)形態(tài)/整數(shù)形態(tài)9/5operatorperforms整數(shù)版本除法double浮點形態(tài)/double浮點形態(tài)9.0/5.0operatorperformsdouble版本除法Example相等,關係,邏輯運算子!邏輯運算NOT<小於<=小於或等於>大於>=大於或等於==相等!=不相等&&邏輯運算AND
4、
5、邏輯運算ORProgramERLDemo.cppintbb=4;if(bb/4
6、
7、bb/0)cout<<"Condictionok";條件式
8、運算:專門用在條件判斷while(x>10&&y<5)cout<<"Condictionok";Assignment運算子LValue=RValue;a+=b;等效於a=a+b;a-=b;等效於a=a-b;a*=b;等效於a=a*b;a/=b;等效於a=a/b;a%=b;等效於a=a%b;Thisoperatorassignsfromrighttoleft.validinvalidx=55=xAssignmentStatementSyntax:variable=expression;#includeusingnamespacestd;voidmain(){inta;str
9、ingstrName,strMajor,Student;a=14;strName=“井民全”;strMajor=“資訊科學系”;Student=strMajor+““+strName;}assignmentAssignmentStatementSyntax:variable=expression;宣告時順便指定初值#includeusingnamespacestd;voidmain(){intmyage=33;intwidth=10,length;doublePI=3.14,Score=73.5;charans,key=‘Q’;char*name=“卡拉楊”;strin
10、gname=“周杰倫”;}沒指定初值內容為亂七八糟的數(shù)C-style字串C++字串物件遞增,遞減運算子a++;等效於a=a+1;a--;等效於a=a-1;Exampleintindex=3;inta[5]={11,22,33,44,55};//宣告陣列cout<11、析A=B+B;B++;ConditionalOperator(條件運算子)B敘述C敘述條件?:SyntaxStarttruefalse條件B敘述C敘述語意Seeprogram:CoDemo.cppa>=b?"a大於或等於b":"a小於b"請利用條件運算子與巨集寫一個絕對值的函式.條件運算子範例#includevoidmain(){inta,b;cout<<"現(xiàn)在要比較a,b的關係,請輸入整數(shù)a的內容:";ci