Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

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

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

5 示範程式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);

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

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

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

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

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

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

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

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

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

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

16 示範程式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;

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

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

19 示範程式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);

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

21 示範程式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));

22 練習 撰寫一程式,宣告變數,以算式與數學函式設值,並予輸出。

23 示範程式UsingConstant重要片段
int anInt = 123; const int A_CONST = 456; anInt = 321;

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

25 示範程式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;

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

27 練習 利用偵錯器觀察遞增遞減算式的作用

28 示範程式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);

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

30 關連(Relation)運算子與布林變數
說明 == 相等 != 不等於 > 大於 >= 大於等於 < 小於 <= 小於等於

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

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

33 字串物件比較 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 );

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

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

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

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

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

39 練習 撰寫程式混合運用學過的運算式與if或三元算符敘述,添加註解說明程式目的、作者、時間、關鍵算式

40 示範程式UsingEnum重要片段(1/2)
enum WeekDay { SUN = 1, MON = 2, TUE = 3, WED = 4, THU = 5, FRI = 6, SAT = 7 }

41 示範程式UsingEnum重要片段(2/2)
WeekDay day = WeekDay.TUE; switch (day) { case WeekDay.SUN: Console.WriteLine("星期日為一週的第{0}天!!", (int)WeekDay.SUN); break; case WeekDay.TUE: Console.WriteLine("星期二為一週的第{0}天!!", (int)WeekDay.TUE); default: Console.WriteLine("一週的第{0}天!!", (int)day); }

42 列舉型別 (Enumeration) 組織整數常數與程式維護 省略數值指定 應用列舉型別

43 練習 宣告並測試列舉型別Season,其中包括常數Spring、Summer、Fall、Winter


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

Similar presentations


Ads by Google