實值變數與運算式
description
Transcript of 實值變數與運算式
1
實值變數與運算式實值變數與運算式
鄭士康鄭士康國立台灣大學國立台灣大學
電機工程學系電機工程學系 // 電信工程研究所電信工程研究所 //資訊網路與多媒體研究所資訊網路與多媒體研究所
2
示範程式示範程式 UsingVariableUsingVariable 重要重要片段片段
char aChar = 'a';char aChar = 'a';Console.WriteLine(aChar);Console.WriteLine(aChar);int anInt = 123;int anInt = 123;Console.WriteLine(anInt);Console.WriteLine(anInt);double aDouble;double aDouble;aDouble = 123.456;aDouble = 123.456;Console.WriteLine(aDouble);Console.WriteLine(aDouble);bool aTrueValue = truebool aTrueValue = true;; Console.WriteLine(aTrueValue);Console.WriteLine(aTrueValue);
3
主記憶系統概念主記憶系統概念
*J. G. Brookshear, Computer Science – An Overview, 8th edition, *J. G. Brookshear, Computer Science – An Overview, 8th edition, Addison-Wesley, 2005Addison-Wesley, 2005
4
使用變數使用變數• 變數宣告與地址觀念變數宣告與地址觀念• 變數命名變數命名
– 命名規定與關鍵字命名規定與關鍵字– 維護考量維護考量– 軟體紀律軟體紀律– 匈牙利命名法匈牙利命名法
• 設值 設值 (Assignment) (Assignment) 與初始化 與初始化 (Initialization)(Initialization)
• 型別相容型別相容
5
示範程式示範程式 UsingNumericUsingNumeric 重重要片段要片段
int x = 256;int x = 256;Console.WriteLine("x : " + x);Console.WriteLine("x : " + x);byte y = 255;byte y = 255;Console.WriteLine("y : " + y);Console.WriteLine("y : " + y);double z = 123.45;double z = 123.45;Console.WriteLine("z : " + z);Console.WriteLine("z : " + z);float f = 123.45f;float f = 123.45f;Console.WriteLine("f : " + f);Console.WriteLine("f : " + f);decimal d = 123.45m;decimal d = 123.45m;Console.WriteLine("d : " + d);Console.WriteLine("d : " + d);
6
整數型別整數型別• sbyte: -128 ~ 127sbyte: -128 ~ 127• byte: 0 ~ 255byte: 0 ~ 255• short: -32768 ~ 32767short: -32768 ~ 32767• unshort: 0 ~ 65535unshort: 0 ~ 65535• int: -2147483648 ~ 2147483647int: -2147483648 ~ 2147483647• uint: 0 ~ 4294967295uint: 0 ~ 4294967295• long: -9223372036854775808 ~ long: -9223372036854775808 ~ 92233720368547758079223372036854775807
• ulong: 0 ~ 18446744073709551615ulong: 0 ~ 18446744073709551615• char: U+0000 ~ U+ffffchar: U+0000 ~ U+ffff
7
浮點數型別浮點數型別• floatfloat: 7 : 7 位精確度位精確度 , , 正負正負 1.5e-45 ~ 1.5e-45 ~ 3.4e383.4e38, , 32 32 位元位元
• doubledouble: 15~16: 15~16 位精確度位精確度 , , 正負正負 5.0e-5.0e-324 ~ 1.7e308324 ~ 1.7e308, 64 , 64 位元位元
0
8
decimal decimal 型別型別• 28 ~ 29 28 ~ 29 位小數位小數 , , 正負正負 1.0e-28 ~ 1.0e-28 ~ 7.9e287.9e28,, 128 128 位元位元
9
示範程式示範程式 UsingCharUsingChar 重要片重要片段段
char c1 = 'a';char c1 = 'a';
char c2 = 'char c2 = '文文 ';';
char c3 = '\x0059';char c3 = '\x0059';
char c4 = '\u0058';char c4 = '\u0058';
char c5 = '\n';char c5 = '\n';
char c6 = '\'';char c6 = '\'';
10
字元表示字元表示• ASCII vs. Unicode ASCII vs. Unicode • 十六進位與十六進位與 UnicodeUnicode 表示法表示法• 逸散字元逸散字元 ( Escaped character )( Escaped character )
– ‘‘\a’\a’:: 警示警示 (alarm)(alarm)– ‘‘\b’\b’:: 退格退格 (backspace)(backspace)– ‘‘\’’\’’: : 單引號單引號 (apostrophe)(apostrophe)– ‘‘\\’: \\’: 倒斜線倒斜線 (backslash)(backslash)– ‘‘\t’\t’:: 水平定位水平定位 (tab)(tab)– ‘‘\n’\n’:: 換行換行 (next line)(next line)
11
字串與字元字串與字元string s1 = “abc”;string s1 = “abc”;
string s2 = “a”;string s2 = “a”;
char c = ‘a’;char c = ‘a’;
12
堆疊堆疊 (Stack)(Stack) 與堆積與堆積 (Heap)(Heap)
Stack
Heap
.
.
.
13
實值型別儲存方式實值型別儲存方式
堆疊 (Stack)
int x = 100;
100x
14
參考型別儲存方式參考型別儲存方式堆疊 (Stack)
string x = “abc”;
x 參考 ‘a’
‘b’
‘c’
堆積 (Heap)
15
練習練習• 撰寫一程式,宣告與設定數個變數,並予撰寫一程式,宣告與設定數個變數,並予
輸出。輸出。
16
示範程式示範程式 ConversionConversion 重要片重要片段段
int a = 10;int a = 10;double b = 0;double b = 0;b = a;b = a;b = 20.5;b = 20.5;a = (int)b;a = (int)b;float c = 20;float c = 20;c = 20.5f;c = 20.5f;c = (float)20.5;c = (float)20.5;char d = (char)65;char d = (char)65;
17
變數設值與型別轉換變數設值與型別轉換• 變數設值 變數設值 (Assignment)(Assignment)• 隱含轉換 隱含轉換 (Implicit conversion)(Implicit conversion)• 強制轉換 強制轉換 (Explicit conversion)(Explicit conversion)
18
型別轉換錯誤三例型別轉換錯誤三例• 例例 11
byte bValue = 254;byte bValue = 254;bValue = bValue*2;bValue = bValue*2;
• 例例 22byte bValue;byte bValue;int aa = 0;int aa = 0;bValue = aa + 0;bValue = aa + 0;
• 例例 33float f = 0;float f = 0;f = 0.1 + 0.1;f = 0.1 + 0.1;
19
示範程式示範程式 UsingMathOperatorUsingMathOperator 重重要片段要片段
Console.WriteLine("Console.WriteLine("請輸入第一個整數值請輸入第一個整數值 x :");x :");int x = int.Parse(Console.ReadLine());int x = int.Parse(Console.ReadLine());Console.WriteLine("Console.WriteLine("請輸入第二個整數值請輸入第二個整數值 y :");y :");int y = int.Parse(Console.ReadLine());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);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)(Operand) 與運算子與運算子 (Operator)(Operator)• 設值運算子設值運算子 (Assignment)(Assignment)• 算術運算子算術運算子
– 加、減、乘、除加、減、乘、除– 模數模數
• 括弧使用與計算順序括弧使用與計算順序
21
示範程式示範程式 UsingMathFunctionsUsingMathFunctions 重重要片段要片段
Console.WriteLine("Sqrt(2) = " + Console.WriteLine("Sqrt(2) = " + Math.Sqrt(2.0));Math.Sqrt(2.0));
Console.WriteLine("PI = " + Math.PI);Console.WriteLine("PI = " + Math.PI);Console.WriteLine("Sin(PI/6.0) = " + Console.WriteLine("Sin(PI/6.0) = " + Math.Sin(Math.PI / 6.0));Math.Sin(Math.PI / 6.0));
Console.WriteLine("Pow(2.0, 0.5) = " + Console.WriteLine("Pow(2.0, 0.5) = " + Math.Pow(2.0, 0.5));Math.Pow(2.0, 0.5));
Console.WriteLine("Exp(1) = " + Console.WriteLine("Exp(1) = " + Math.Exp(1.0));Math.Exp(1.0));
Console.WriteLine("ln(e) = " + Console.WriteLine("ln(e) = " + Math.Log(Math.E));Math.Log(Math.E));
Console.WriteLine("log10(100) = " + Console.WriteLine("log10(100) = " + Math.Log10(100.0));Math.Log10(100.0));
22
練習練習• 撰寫一程式,宣告變數,以算式與數學函撰寫一程式,宣告變數,以算式與數學函
式設值,並予輸出。式設值,並予輸出。
23
示範程式示範程式 UsingConstantUsingConstant 重要片重要片段段
int anInt = 123;int anInt = 123;
const int A_CONST = 456;const int A_CONST = 456;
anInt = 321;anInt = 321;
24
使用常數使用常數• 常數設定常數設定• 常數特性常數特性• 常數與程式維護常數與程式維護
25
示範程式示範程式 UsingInDeOperatorUsingInDeOperator 重要重要片段片段
Console.WriteLine("Console.WriteLine("請輸入整數變數請輸入整數變數 xx初值初值 ");");int x0 = int.Parse(Console.ReadLine());int x0 = int.Parse(Console.ReadLine());Console.WriteLine("Console.WriteLine("請輸入所要加總的整數值請輸入所要加總的整數值 add");add");int add = int.Parse(Console.ReadLine());int add = int.Parse(Console.ReadLine());int x = x0;int x = x0;x = x + add;x = x + add;x = x0;x = x0;x += add;x += add;int post;int post;x = x0;x = x0;post = x++;post = x++;int pre;int pre;x = x0;x = x0;pre = ++x;pre = ++x;
26
遞增遞減運算子遞增遞減運算子• 運算子 運算子 +=+=、、 -=-= 、、 **==、、 /-/-、、 %=%=• 運算子運算子 ++++、、 ----• 前置運算子前置運算子 (prefix)(prefix)
result = ++x;result = ++x;
• 後置運算子後置運算子 (postfix)(postfix)result = x++;result = x++;
練習練習• 利用偵錯器觀察遞增遞減算式的作用利用偵錯器觀察遞增遞減算式的作用
27
28
示範程式示範程式 UsingLBUsingLB 重要片段重要片段
bool x = 7 > 3;bool x = 7 > 3;
bool y = 2 < 0;bool y = 2 < 0;
bool xORy = x | y;bool xORy = x | y;
bool xANDy = x & y;bool xANDy = x & y;
bool xOy = (x & y) | (x | y);bool xOy = (x & y) | (x | y);
bool xNy = (x & y) & (x | y);bool xNy = (x & y) & (x | y);
29
布林型別與算式布林型別與算式• 邏輯敘述邏輯敘述 , , 流程控制流程控制 , , Debug.Assert()Debug.Assert()
– x > 1x > 1
• true/false, true/false, 不可寫為不可寫為數值如數值如 11 或 或 00• 關連算式關連算式• 布林算式布林算式
30
關連關連 (Relation)(Relation) 運算子與布林運算子與布林變數變數
運算子運算子 說明說明 運算子運算子 說明說明==== 相等相等 !=!= 不等於不等於>> 大於大於 >=>= 大於等於大於等於<< 小於小於 <=<= 小於等於小於等於
31
一般邏輯運算一般邏輯運算
xx yy x & yx & y x | yx | y x ^ yx ^ y !y!y
falsefalse falsefalse falsefalse falsefalse falsefalse truetrue
truetrue falsefalse falsefalse truetrue truetrue truetrue
falsefalse truetrue falsefalse truetrue truetrue falsefalse
truetrue truetrue truetrue truetrue falsefalse falsefalse
32
Short-Circuit Short-Circuit 邏輯運算邏輯運算• && && 與 與 ||||• 範例範例
– x && yx && y– x || yx || y– (x & y) || (x | y)(x & y) || (x | y)– (x & y) && (x | y)(x & y) && (x | y)
33
字串物件比較字串物件比較string first = “one”;string first = “one”;
string second = “One”;string second = “One”;
string third = “one”;string third = “one”;
Console.WriteLine( first == second );Console.WriteLine( first == second );
Console.WriteLine( first == third );Console.WriteLine( first == third );
Console.WriteLine( first != second );Console.WriteLine( first != second );
Console.WriteLine( first != third );Console.WriteLine( first != third );
34
if if 流程流程
grade < 60
true
grade = 60
false
35
示範程式示範程式 UsingSimpleIfUsingSimpleIf 重要片重要片段段
Console.Write(Console.Write(""請輸入一個小於請輸入一個小於 100100的整數原始成績的整數原始成績 : ");: ");
int grade = int.Parse(Console.ReadLine());int grade = int.Parse(Console.ReadLine());// // 調分公式調分公式if (grade < 60)if (grade < 60){{ grade = 60;grade = 60;} } Console.WriteLine("Console.WriteLine("調分後成績調分後成績 : " + grade);: " + grade);
36
if-else if-else 流程流程
grade < 60
result = grade
truefalse
result = 60
37
示範程式示範程式 UsingTerOpUsingTerOp 重要片段重要片段
Console.Write(Console.Write(
""請輸入一個小於請輸入一個小於 100100的整數原始成績的整數原始成績 : : ");");
int grade = int grade = int.Parse(Console.ReadLine());int.Parse(Console.ReadLine());
int result = grade < 60 ? 60 : int result = grade < 60 ? 60 : grade; // grade; // 調分公式調分公式
Console.WriteLine("Console.WriteLine("調分後成績調分後成績 : " + : " + result);result);
38
運算子優先順序運算子優先順序• 算術運算優先順序算術運算優先順序
– 一元遞增遞減運算子一元遞增遞減運算子– 正負號正負號– 四則運算與模數計算四則運算與模數計算
• 關連運算子關連運算子• 邏輯運算子 邏輯運算子 !, &, ^, |, &&, ||!, &, ^, |, &&, ||• 三元運算子三元運算子• 設定 設定 =,*=,/=,%=,+=,-=,&=,^=,|==,*=,/=,%=,+=,-=,&=,^=,|=• 使用括弧改變順序使用括弧改變順序
39
練習練習• 撰寫程式混合運用學過的運算式與撰寫程式混合運用學過的運算式與 ifif 或三或三
元算符敘述,添加註解說明程式目的、作元算符敘述,添加註解說明程式目的、作者、時間、關鍵算式者、時間、關鍵算式
40
示範程式示範程式 UsingEnumUsingEnum 重要片段重要片段(1/2)(1/2)
enum WeekDayenum WeekDay{{ SUN = 1,SUN = 1, MON = 2,MON = 2, TUE = 3,TUE = 3, WED = 4,WED = 4, THU = 5,THU = 5, FRI = 6,FRI = 6, SAT = 7SAT = 7}}
41
示範程式示範程式 UsingEnumUsingEnum 重要片段重要片段(2/2)(2/2)
WeekDay day = WeekDay.TUE;WeekDay day = WeekDay.TUE;switch (day)switch (day){{ case WeekDay.SUN:case WeekDay.SUN: Console.WriteLine("Console.WriteLine("星期日為一週的第星期日為一週的第 {0}{0}天天 !!", !!",
(int)WeekDay.SUN);(int)WeekDay.SUN); break;break; case WeekDay.TUE:case WeekDay.TUE: Console.WriteLine("Console.WriteLine("星期二為一週的第星期二為一週的第 {0}{0}天天 !!", !!", (int)WeekDay.TUE);(int)WeekDay.TUE); break;break; default:default: Console.WriteLine("Console.WriteLine("一週的第一週的第 {0}{0}天天 !!",!!", (int)day);(int)day); break;break;}}
42
列舉型別 列舉型別 (Enumeration)(Enumeration)
• 組織整數常數與程式維護組織整數常數與程式維護• 省略數值指定省略數值指定• 應用列舉型別應用列舉型別
43
練習練習• 宣告並測試列舉型別宣告並測試列舉型別 SeasonSeason ,其中包括,其中包括
常數常數SpringSpring、、 SummerSummer、、 FallFall、、 WinterWinter