算式由一个或多个运算元(operand)构成并传回值(结果)

上传人:j****9 文档编号:54393523 上传时间:2018-09-12 格式:PPT 页数:27 大小:170.50KB
返回 下载 相关 举报
算式由一个或多个运算元(operand)构成并传回值(结果)_第1页
第1页 / 共27页
算式由一个或多个运算元(operand)构成并传回值(结果)_第2页
第2页 / 共27页
算式由一个或多个运算元(operand)构成并传回值(结果)_第3页
第3页 / 共27页
算式由一个或多个运算元(operand)构成并传回值(结果)_第4页
第4页 / 共27页
算式由一个或多个运算元(operand)构成并传回值(结果)_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《算式由一个或多个运算元(operand)构成并传回值(结果)》由会员分享,可在线阅读,更多相关《算式由一个或多个运算元(operand)构成并传回值(结果)(27页珍藏版)》请在金锄头文库上搜索。

1、Expressions (算式),Expressions (算式),算式由一個或多個運算元(operand)構成並傳回值(結果) 算式的形式可能有a+b; 3.14159; var; ab;,Arithematic Operators (算術運算子),addition +subtraction multiplication *division /modulus %,Arithematic Operators (算術運算子),Syntax operand operator operand,Example 7 + 15 34 189 92 * 31 345 / 6.02 86 % 3,*,Modu

2、lus,The modulus operator yields the remainder of integer division.,* *,C+ Modulus Example,18 % 4 = 2 13 % 4 is 1 17 % 3 = 2 35 % 47 is 35 24 % 6 = 0 24 % 4 is 0 4 % 18 = 4 0 % 7 is 0 12 % 2.5 error 6.0 % 6 error,* * *,int main() cout “18%4 = “ 18%4; ,Operators Precedence (優先權)- Evaluation Trees,Eval

3、uation Trees,Operators Overload,Operator overload Using the same symbol for more than one operation,Example,相等,關係,邏輯運算子,ProgramERLDemo.cpp,int bb=4; if( bb/4 | bb/0)cout 10 ,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;,This operat

4、or assigns from right to left. valid invalid x = 5 5 = x,Assignment Statement,Syntax:variable = expression;,#include using namespace std; void main() int a; string strName,strMajor,Student;a = 14; strName = “井民全”; strMajor = “資訊科學系”; Student = strMajor + “ “+ strName; ,assignment,Assignment Statemen

5、t,Syntax:variable = expression;,宣告時順便指定初值#include using namespace std; void main() int myage = 33; int width = 10, length; double PI = 3.14, Score = 73.5; char ans, key = Q; char* name=“卡拉楊”; string name=“周杰倫”;,C-style 字串,C+ 字串物件,遞增,遞減運算子,a+; 等效於 a=a+1; a-; 等效於 a=a-1;,Example int index=3; int a5=11,

6、22,33,44,55; / 宣告陣列 couta-index; /會印出33 cout=b ? “a大於或等於b“:“a小於b “,請利用條件運算子與巨集寫一個絕對值的函式.,條件運算子範例,#include void main() int a,b;couta;coutb;cout=b ? “a大於或等於b“ : “a小於b “ )endl; ,new算式,執行時期配置記憶體的行為,稱為動態記憶體配置(dynamic memory allocation) 基本敘述型式為: = new 以整數來說,可能會是下列的敘述: int *p=new int; /等效於 int p= / 配置一個有10

7、個元素的陣列,並把起位址指定給p.,若配置失敗, 則 p 的內容= 0,配置記憶體空間 (補充),配置大小為5個整數的連續區塊 int* pA= new int5; 配置一堆大小相同的區塊 int *pA0=new int5; int *pA1=new int5; int *pA9=new int5; 配置指標陣列集中管理 int *pA10; / 宣告一個專門存指標的陣列 pA0=new int5; pA1=new int5;,for(int y=0; yRow; y+)pAy= new int5;,配置A10Col大小的陣列,int* A10;,A0,A1,A2,A9,. . .,Row(

8、10),for(int y=0;yRow;y+)Ay=new intCol,希望任意大小陣列,A0,A1,A2,A9,. . .,Row(10),int* A10;,Initial Case 1: int * A; / 2. 儲存指標變數位址的變數 A=new int*10; / 1. 配置10指標變數,Prog: Alloc2D (建立任意大小的陣列),Initial Case 2: int * A=new int*10; / 1. 配置10指標變數,delete 算式,delete p; /釋放記憶體.對於p僅配置一個object而言. delete p; /對p配置了兩個以上的objec

9、t而言.範例程式: AllocDemo.cpp,請依上例建立3D matrix 並且亦要有free memory的功能,Implicit Type Conversions (隱式型別轉換),Exampleint ival=3.541+3;char c=a;long b=10;int a=12;long d=a+b+c;,自動使用較大的型別,隱式型別轉換:以“最大的”型別為基準.,Explicit Conversions (顯式型別轉換),static_cast / 一般的隱式轉型 (通常用來處理警告問題) const_cast const變數 / const 變數轉換非const reinte

10、rpret_cast 一般變數 / 強迫轉型 dynamic_cast 型別2_變數,將一個較大型別的算術型態指派給較小型別, 幾乎都會 引起精確度遺失的警告. 所以這時就需要強制轉型了,表 示我知道(並且不在乎) 可能的精確度遺失.,double d=97.0;char ch=static_cast(d); / char ch=(char) d;Complex *Obj;char *pc=reinterpret_cast(Obj);,Reference: C+ Primer中文版 pp 182,char *pc=(char*) Obj;,Explicit Conversions (顯式型別轉換),Superb pm= dynamic_cast (pg) 若 pg可以安全的轉型為Superb* type則ok 否則= null (page 14-53),Superb,衍生型態,位元操作運算子,unsigned char a=0xf0,b=0x55;a,11110000,01010101,01010000,& (AND),11110000 AND 01010101 =,範例程式:BitDemo.cpp,位元操作運算子,End,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号