Presentation is loading. Please wait.

Presentation is loading. Please wait.

鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

Similar presentations


Presentation on theme: "鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所"— Presentation transcript:

1 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
實值變數與運算式 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

2 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

3 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

4 示範程式UsingVariable重要片段
char aChar = 'a'; Console.WriteLine(aChar); int anInt = 123; Console.WriteLine(anInt); double aDouble; aDouble = ; Console.WriteLine(aDouble); bool aBool = true; Console.WriteLine(aBool);

5 主記憶系統概念 *J. G. Brookshear, Computer Science – An Overview, 8th edition, Addison-Wesley, 2005

6 使用變數 變數宣告與地址觀念 變數命名 設值 (Assignment) 與初始化 (Initialization) 型別相容
命名規定與關鍵字 維護考量 軟體紀律 匈牙利命名法 設值 (Assignment) 與初始化 (Initialization) 型別相容

7 示範程式UsingNumeric重要片段
int x = 256; Console.WriteLine("x : " + x); byte y = 255; Console.WriteLine("y : " + y); double z = ; Console.WriteLine("z : " + z); float f = f; Console.WriteLine("f : " + f); decimal d = m; Console.WriteLine("d : " + d);

8 整數型別 sbyte: -128 ~ 127 byte: 0 ~ 255 short: -32768 ~ 32767
unshort: 0 ~ 65535 int: ~ uint: 0 ~ long: ~ ulong: 0 ~ char: U+0000 ~ U+ffff

9 浮點數型別 float: 7 位精確度, 正負1.5e-45 ~ 3.4e38, 32 位元
double: 15~16位精確度, 正負5.0e-324 ~ 1.7e308, 64 位元

10 decimal 型別 28 ~ 29 位小數, 正負1.0e-28 ~ 7.9e28, 128 位元

11 示範程式UsingChar重要片段 char c1 = 'a'; char c2 = '文'; char c3 = '\x0059';
char c4 = '\u0058'; char c5 = '\n'; char c6 = '\'';

12 字元表示 ASCII vs. Unicode 十六進位與Unicode表示法 逸散字元( Escaped character )
‘\a’: 警示(alarm) ‘\b’: 退格(backspace) ‘\’’: 單引號(apostrophe) ‘\\’: 倒斜線(backslash) ‘\t’: 水平定位(tab) ‘\n’: 換行(next line)

13 字串與字元 string s1 = “abc”; string s2 = “a”; char c = ‘a’;

14 堆疊(Stack)與堆積(Heap) Heap . Stack

15 實值型別儲存方式 堆疊(Stack) int x = 100; x 100

16 參考型別儲存方式 堆積(Heap) 堆疊(Stack) string x = “abc”; x 參考 ‘a’ ‘b’ ‘c’

17 練習 撰寫一程式,宣告與設定數個變數,並予輸出。

18 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

19 示範程式Conversion重要片段 int a = 10; double b = 0; b = a; b = 20.5;
a = (int)b; float c = 20; c = 20.5f; c = (float)20.5; char d = (char)65;

20 變數設值與型別轉換 變數設值 (Assignment) 隱含轉換 (Implicit conversion)
強制轉換 (Explicit conversion)

21 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

22 示範程式UsingMathOperator重要片段
Console.WriteLine("請輸入第一個整數值x :"); int x = int.Parse(Console.ReadLine()); Console.WriteLine("請輸入第二個整數值y :"); int y = int.Parse(Console.ReadLine()); Console.WriteLine(" x + y = {0} ", x + y); Console.WriteLine(" x - y = {0} ", x - y); Console.WriteLine(" x * y = {0} ", x * y); Console.WriteLine(" x / y = {0} ", x / y); Console.WriteLine(" x % y = {0} ", x % y);

23 設值與算術運算 運算元(Operand)與運算子(Operator) 設值運算子(Assignment) 算術運算子 括弧使用與計算順序
加、減、乘、除 模數 括弧使用與計算順序

24 型別轉換錯誤三例 例1 例2 例3 byte bValue = 254; bValue = bValue*2; byte bValue;
int aa = 0; bValue = aa + 0; 例3 float f = 0; f = ;

25 示範程式UsingMathFunctions重要片段
Console.WriteLine("Sqrt(2) = " + Math.Sqrt(2.0)); Console.WriteLine("PI = " + Math.PI); Console.WriteLine("Sin(PI/6.0) = " + Math.Sin(Math.PI / 6.0)); Console.WriteLine("Pow(2.0, 0.5) = " + Math.Pow(2.0, 0.5)); Console.WriteLine("Exp(1) = " + Math.Exp(1.0)); Console.WriteLine("ln(e) = " + Math.Log(Math.E)); Console.WriteLine("log10(100) = " + Math.Log10(100.0));

26 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

27 示範程式UsingConstant重要片段
const double G = 9.8; Console.WriteLine("常數G = {0} ", G); double t = 1.0; double y = -(1.0 / 2.0) * G * t * t; G = 9.8 / 6; // Error!

28 使用常數 常數設定 常數特性 常數與程式維護

29 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

30 示範程式UsingInDeOperator重要片段
Console.WriteLine("請輸入整數變數x初值"); int x0 = int.Parse(Console.ReadLine()); Console.WriteLine("請輸入所要加總的整數值add"); int add = int.Parse(Console.ReadLine()); int x = x0; x = x + add; x = x0; x += add; int post; post = x++; int pre; pre = ++x;

31 遞增遞減運算子 運算子 +=、-=、*=、/-、%= 運算子++、-- 前置運算子(prefix) 後置運算子(postfix)
result = ++x; 後置運算子(postfix) result = x++;

32 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

33 關連(Relation)運算子 運算子 說明 相等 不等於 大於 大於等於 小於 小於等於 == != > >= <
<= 小於等於

34 if 流程 score < 60 false true score = 60

35 示範程式UsingSimpleIf重要片段
Console.Write( "請輸入一個小於100的整數原始成績: "); int score = int.Parse(Console.ReadLine()); // 調分公式 if (score < 60) { score = 60; } Console.WriteLine("調分後成績: " + score);

36 if-else 流程 score < 60 true false result = score result = 60

37 示範程式UsingTerOp重要片段 Console.Write( "請輸入一個小於100的整數原始成績: ");
int score = int.Parse(Console.ReadLine()); int result = score < 60 ? 60 : score; // 調分公式 Console.WriteLine("調分後成績: " + result);

38 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

39 示範程式UsingLB重要片段 bool x = 7 > 3; bool y = 2 < 0;
bool xORy = x | y; bool xANDy = x & y; bool xOy = (x & y) | (x | y); bool xNy = (x & y) & (x | y);

40 布林型別 邏輯敘述, 流程控制, Debug.Assert() x > 1 true/false, 不可寫為數值如1 或 0

41 一般邏輯運算 x y x & y x | y x ^ y !y false true

42 Short-Circuit 邏輯運算 && 與 || 範例 x && y x || y (x & y) || (x | y)

43 字串物件比較 string first = “one”; string second = “One”;
string third = “one”; Console.WriteLine( first == second ); Console.WriteLine( first == third ); Console.WriteLine( first != second ); Console.WriteLine( first != third );

44 綱要 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式 運算優先順序

45 運算子優先順序 算術運算優先順序 關連運算子 邏輯運算子 !, &, ^, |, &&, || 三元運算子
一元遞增遞減運算子 正負號 四則運算與模數計算 關連運算子 邏輯運算子 !, &, ^, |, &&, || 三元運算子 設定 =,*=,/=,%=,+=,-=,&=,^=,|= 使用括弧改變順序

46 練習 撰寫程式混合運用學過的運算式, 添加註解說明程式目的,作者,時間,及關鍵算式


Download ppt "鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所"

Similar presentations


Ads by Google