實值變數與運算式 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所 綱要 1. 2. 3. 4. 5. 6. 7. 8. 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子 bool型別及邏輯運算式 運算優先順序 綱要 1. 2. 3. 4. 5. 6. 7. 8. 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子 bool型別及邏輯運算式 運算優先順序 示範程式UsingVariable重要片段 char aChar = 'a'; Console.WriteLine(aChar); int anInt = 123; Console.WriteLine(anInt); double aDouble; aDouble = 123.456; Console.WriteLine(aDouble); bool aBool = true; Console.WriteLine(aBool);

Download Report

Transcript 實值變數與運算式 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所 綱要 1. 2. 3. 4. 5. 6. 7. 8. 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子 bool型別及邏輯運算式 運算優先順序 綱要 1. 2. 3. 4. 5. 6. 7. 8. 變數宣告與設值 數值型別轉換 基本算術運算式與數學函數 常數宣告 遞增與遞減算子 關連算子, 簡單if敘述, 三元運算子 bool型別及邏輯運算式 運算優先順序 示範程式UsingVariable重要片段 char aChar = 'a'; Console.WriteLine(aChar); int anInt = 123; Console.WriteLine(anInt); double aDouble; aDouble = 123.456; Console.WriteLine(aDouble); bool aBool = true; Console.WriteLine(aBool);

實值變數與運算式
鄭士康
國立台灣大學
電機工程學系/電信工程研究所/
資訊網路與多媒體研究所
1
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
2
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
3
示範程式UsingVariable重要片段
char aChar = 'a';
Console.WriteLine(aChar);
int anInt = 123;
Console.WriteLine(anInt);
double aDouble;
aDouble = 123.456;
Console.WriteLine(aDouble);
bool aBool = true;
Console.WriteLine(aBool);
4
主記憶系統概念
*J. G. Brookshear, Computer Science – An Overview, 8th edition,
Addison-Wesley, 2005
5
使用變數
• 變數宣告與地址觀念
• 變數命名
–
–
–
–
命名規定與關鍵字
維護考量
軟體紀律
匈牙利命名法
• 設值 (Assignment) 與初始化 (Initialization)
• 型別相容
6
示範程式UsingNumeric重要片段
int x = 256;
Console.WriteLine("x
byte y = 255;
Console.WriteLine("y
double z = 123.45;
Console.WriteLine("z
float f = 123.45f;
Console.WriteLine("f
decimal d = 123.45m;
Console.WriteLine("d
: " + x);
: " + y);
: " + z);
: " + f);
: " + d);
7
整數型別
• sbyte: -128 ~ 127
• byte: 0 ~ 255
• short: -32768 ~ 32767
• unshort: 0 ~ 65535
• int: -2147483648 ~ 2147483647
• uint: 0 ~ 4294967295
• long: -9223372036854775808 ~
9223372036854775807
• ulong: 0 ~ 18446744073709551615
• char: U+0000 ~ U+ffff
8
浮點數型別
• float: 7 位精確度, 正負1.5e-45 ~
3.4e38, 32 位元
• double: 15~16位精確度, 正負5.0e-324
~ 1.7e308, 64 位元
0
9
decimal 型別
• 28 ~ 29 位小數, 正負1.0e-28 ~ 7.9e28,
128 位元
10
示範程式UsingChar重要片段
char
char
char
char
char
char
c1
c2
c3
c4
c5
c6
=
=
=
=
=
=
'a';
'文';
'\x0059';
'\u0058';
'\n';
'\'';
11
字元表示
• ASCII vs. Unicode
• 十六進位與Unicode表示法
• 逸散字元( Escaped character )
– ‘\a’:
– ‘\b’:
– ‘\’’:
– ‘\\’:
– ‘\t’:
– ‘\n’:
警示(alarm)
退格(backspace)
單引號(apostrophe)
倒斜線(backslash)
水平定位(tab)
換行(next line)
12
字串與字元
string s1 = “abc”;
string s2 = “a”;
char c = ‘a’;
13
堆疊(Stack)與堆積(Heap)
Heap
.
.
.
Stack
14
實值型別儲存方式
堆疊(Stack)
int x = 100;
x
100
15
參考型別儲存方式
堆疊(Stack)
堆積(Heap)
string x = “abc”;
x
參考
‘a’
‘b’
‘c’
16
練習
• 撰寫一程式,宣告與設定數個變數,並予
輸出。
17
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
18
示範程式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;
19
變數設值與型別轉換
• 變數設值 (Assignment)
• 隱含轉換 (Implicit conversion)
• 強制轉換 (Explicit conversion)
20
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
21
示範程式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);
22
設值與算術運算
• 運算元(Operand)與運算子(Operator)
• 設值運算子(Assignment)
• 算術運算子
– 加、減、乘、除
– 模數
• 括弧使用與計算順序
23
型別轉換錯誤三例
• 例1
byte bValue = 254;
bValue = bValue*2;
• 例2
byte bValue;
int aa = 0;
bValue = aa + 0;
• 例3
float f = 0;
f = 0.1 + 0.1;
24
示範程式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));
25
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
26
示範程式UsingConstant重要片段
int anInt = 123;
const int A_CONST = 456;
anInt = 321;
27
使用常數
• 常數設定
• 常數特性
• 常數與程式維護
28
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
29
示範程式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;
x = x0;
post = x++;
int pre;
x = x0;
pre = ++x;
30
遞增遞減運算子
• 運算子 +=、-=、*=、/-、%=
• 運算子++、-• 前置運算子(prefix)
result = ++x;
• 後置運算子(postfix)
result = x++;
31
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
32
關連(Relation)運算子
運算子
說明
運算子
說明
==
相等
大於
小於
!=
不等於
大於等於
小於等於
>
<
>=
<=
33
if 流程
score < 60
false
true
score = 60
34
示範程式UsingSimpleIf重要片段
Console.Write(
"請輸入一個小於100的整數原始成績: ");
int score = int.Parse(Console.ReadLine());
// 調分公式
if (score < 60)
{
score = 60;
}
Console.WriteLine("調分後成績: " + score);
35
if-else 流程
false
result = score
score < 60
true
result = 60
36
示範程式UsingTerOp重要片段
Console.Write(
"請輸入一個小於100的整數原始成績: ");
int score = int.Parse(Console.ReadLine());
int result = score < 60 ? 60 : score;
// 調分公式
Console.WriteLine("調分後成績: " + result);
37
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
38
示範程式UsingLB重要片段
bool
bool
bool
bool
bool
bool
x = 7 > 3;
y = 2 < 0;
xORy = x | y;
xANDy = x & y;
xOy = (x & y) | (x | y);
xNy = (x & y) & (x | y);
39
布林型別
• 邏輯敘述, 流程控制, Debug.Assert()
– x > 1
• true/false, 不可寫為數值如1 或 0
40
一般邏輯運算
x
y
x & y
x | y
x ^ y
!y
false
false
false
false
false
true
true
false
false
true
true
true
false
true
false
true
true
false
true
true
true
true
false
false
41
Short-Circuit 邏輯運算
• && 與 ||
• 範例
– x &&
– x ||
– (x &
– (x &
y
y
y) || (x | y)
y) && (x | y)
42
字串物件比較
string first = “one”;
string second = “One”;
string third = “one”;
Console.WriteLine( first
Console.WriteLine( first
Console.WriteLine( first
Console.WriteLine( first
==
==
!=
!=
second );
third );
second );
third );
43
綱要
1.
2.
3.
4.
5.
6.
7.
8.
變數宣告與設值
數值型別轉換
基本算術運算式與數學函數
常數宣告
遞增與遞減算子
關連算子, 簡單if敘述, 三元運算子
bool型別及邏輯運算式
運算優先順序
44
運算子優先順序
• 算術運算優先順序
– 一元遞增遞減運算子
– 正負號
– 四則運算與模數計算
•
•
•
•
•
關連運算子
邏輯運算子 !, &, ^, |, &&, ||
三元運算子
設定 =,*=,/=,%=,+=,-=,&=,^=,|=
使用括弧改變順序
45
練習
• 撰寫程式混合運用學過的運算式, 添加註解
說明程式目的,作者,時間,及關鍵算式
46