20
1 運運運 運運運 (Operators) (Operators) 鄭鄭鄭 鄭鄭鄭 鄭鄭鄭鄭鄭鄭 鄭鄭鄭鄭鄭鄭 鄭鄭鄭鄭鄭鄭 鄭鄭鄭鄭鄭鄭 / / 鄭鄭鄭鄭鄭鄭鄭 鄭鄭鄭鄭鄭鄭鄭 / / 鄭鄭鄭鄭鄭鄭鄭 鄭鄭鄭 鄭鄭鄭鄭鄭鄭鄭 鄭鄭鄭

運算子 (Operators)

  • Upload
    artie

  • View
    34

  • Download
    4

Embed Size (px)

DESCRIPTION

運算子 (Operators). 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所. 程式 UsingMathOperator (1/2). using System; namespace UsingMathOperator { class Program { static void Main(string[] args) { int x; int y; Console.WriteLine(" 請輸入第一個整數值 x :"); - PowerPoint PPT Presentation

Citation preview

Page 1: 運算子  (Operators)

1

運算子 運算子 (Operators)(Operators)

鄭士康鄭士康國立台灣大學國立台灣大學

電機工程學系電機工程學系 // 電信工程研究所電信工程研究所 //資訊網路與多媒體研究所資訊網路與多媒體研究所

Page 2: 運算子  (Operators)

2

程式 程式 UsingMathOperator (1/UsingMathOperator (1/2)2)

using System;using System;namespace UsingMathOperatornamespace UsingMathOperator{{ class Programclass Program {{ static void Main(string[] args)static void Main(string[] args) {{ int x;int x; int y;int y; Console.WriteLine("Console.WriteLine("請輸入第一個整數值請輸入第一個整數值 x :");x :"); x = int.Parse(Console.ReadLine());x = int.Parse(Console.ReadLine()); Console.WriteLine("Console.WriteLine("請輸入第二個整數值請輸入第二個整數值 y :");y :"); y = int.Parse(Console.ReadLine());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);

Page 3: 運算子  (Operators)

3

程式 程式 UsingMathOperator (2/UsingMathOperator (2/2)2)

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); }} }}}}

Page 4: 運算子  (Operators)

4

設值與算術運算設值與算術運算• 運算元運算元 (Operand)(Operand) 與運算子與運算子 (Operator)(Operator)• 設值運算子設值運算子 (Assignment)(Assignment)• 算術運算子算術運算子

– 加、減、乘、除加、減、乘、除– 模數模數

• 括弧使用與計算順序括弧使用與計算順序

Page 5: 運算子  (Operators)

5

程式 程式 UsingInDeOperator (1/UsingInDeOperator (1/3)3)

using System;using System;

namespace UsingInDeOperatornamespace UsingInDeOperator{{ class Programclass Program {{ static void Main(string[] args)static void Main(string[] args) {{ int x0;int x0; int x;int x; int add;int add; Console.WriteLine("Console.WriteLine("請輸入整數變數請輸入整數變數 xx初值初值 ");"); x0 = int.Parse(Console.ReadLine());x0 = int.Parse(Console.ReadLine()); Console.WriteLine("Console.WriteLine("請輸入所要加總的整數值請輸入所要加總的整數值 add");add"); add = int.Parse(Console.ReadLine());add = int.Parse(Console.ReadLine());

Page 6: 運算子  (Operators)

6

程式 程式 UsingInDeOperator (2/UsingInDeOperator (2/3)3)

x = x0;x = x0; x = x + add;x = x + add; Console.WriteLine(Console.WriteLine( ""使用運算式使用運算式 \"x = x + add\" \"x = x + add\" 運算結果等於運算結果等於 {0} ", x);{0} ", x); x = x0;x = x0; x += add;x += add; Console.WriteLine(Console.WriteLine( ""使用運算式使用運算式 \"x += add\" \"x += add\" 運算結果等於運算結果等於 {0} ", x);{0} ", x); int pre;int pre; int post;int post; x = x0;x = x0; post = x++;post = x++; x = x0;x = x0; pre = ++x;pre = ++x;

Page 7: 運算子  (Operators)

7

程式 程式 UsingInDeOperator (3/UsingInDeOperator (3/3)3)

Console.WriteLineConsole.WriteLine(( ""使用運算式使用運算式 \"post = x++\"post = x++後後 \" post\" post等於等於 {0} ", {0} ",

post);post); Console.WriteLine(Console.WriteLine( ""使用運算式使用運算式 \"post = x++\"post = x++後後 \" x\" x等於等於 {0}", x);{0}", x); Console.WriteLine(Console.WriteLine( ""使用運算式使用運算式 \"pre = ++x\" pre\"pre = ++x\" pre等於等於 {0} ", pre);{0} ", pre); Console.WriteLine(Console.WriteLine( ""使用運算式使用運算式 \"pre = ++x\"pre = ++x後後 \" x\" x等於等於 {0}", x);{0}", x); Console.ReadLine();Console.ReadLine(); }} }}}}

Page 8: 運算子  (Operators)

8

遞增遞減運算子遞增遞減運算子• 運算子 運算子 +=+= 、、 -=-= 、、 *=*= 、、 /-/- 、、 %=%=• 運算子運算子 ++++ 、、 ----• 前置運算子前置運算子 (prefix)(prefix)

result = ++x;result = ++x;

• 後置運算子後置運算子 (postfix)(postfix)result = x++;result = x++;

Page 9: 運算子  (Operators)

9

型別轉換錯誤三例型別轉換錯誤三例• 例例 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;

Page 10: 運算子  (Operators)

10

程式程式 UsingLB (1/2)UsingLB (1/2)using System;using System;

namespace UsingLBnamespace UsingLB{{ class Programclass Program {{ static void Main(string[] args)static void Main(string[] args) {{ bool x = 7 > 3;bool x = 7 > 3; bool y = 2 < 0; bool y = 2 < 0; Console.WriteLine("x = " + x);Console.WriteLine("x = " + x); Console.WriteLine("y = " + y);Console.WriteLine("y = " + y); bool xORy = x | y;bool xORy = x | y; Console.WriteLine("x | y :" + xORy);Console.WriteLine("x | y :" + xORy); bool xANDy = x & y;bool xANDy = x & y; Console.WriteLine("x & y :" + xANDy);Console.WriteLine("x & y :" + xANDy);

Page 11: 運算子  (Operators)

11

程式程式 UsingLB (2/2)UsingLB (2/2) bool xOy = (x & y) | (x | y);bool xOy = (x & y) | (x | y); Console.WriteLine("(x & y) | (x | y) :" + Console.WriteLine("(x & y) | (x | y) :" +

xOy);xOy); bool xNy = (x & y) & (x | y);bool xNy = (x & y) & (x | y); Console.WriteLine("(x & y) & (x | y) :" + Console.WriteLine("(x & y) & (x | y) :" +

xNy);xNy); Console.ReadLine();Console.ReadLine(); }} }}}}

Page 12: 運算子  (Operators)

12

關連關連 (Relation)(Relation) 運算子與布林運算子與布林變數變數

運算子運算子 說明說明 運算子運算子 說明說明==== 相等相等 !=!= 不等於不等於>> 大於大於 >=>= 大於等於大於等於<< 小於小於 <=<= 小於等於小於等於

Page 13: 運算子  (Operators)

13

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

Page 14: 運算子  (Operators)

14

一般邏輯運算一般邏輯運算

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

Page 15: 運算子  (Operators)

15

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)

Page 16: 運算子  (Operators)

16

邏輯逐位元運算子與位移運算子邏輯逐位元運算子與位移運算子• 十進位與二進位十進位與二進位• 逐位元 逐位元 &,|, ^, ~&,|, ^, ~• 位移運算子 位移運算子 >>, <<>>, <<

Page 17: 運算子  (Operators)

17

程式 程式 UsingTerOpUsingTerOpusing System;using System;

namespace UsingTerOpnamespace UsingTerOp{{ /* /* * * 用調分公式說明三元運算子的使用用調分公式說明三元運算子的使用 * skj 3/4/2007* skj 3/4/2007 */*/

class Programclass Program {{ static void Main(string[] args)static void Main(string[] args) {{ int grade;int grade; int result;int result;

Page 18: 運算子  (Operators)

18

程式 程式 UsingTerOpUsingTerOp Console.WriteLine("Console.WriteLine("請輸入一個小於的整數原始成績請輸入一個小於的整數原始成績 ::

");"); grade = int.Parse(Console.ReadLine());grade = int.Parse(Console.ReadLine()); result = grade < 60 ? 60 : grade; // result = grade < 60 ? 60 : grade; // 調分公式調分公式 Console.WriteLine("Console.WriteLine("調分後成績調分後成績 : " + result);: " + result); Console.ReadLine();Console.ReadLine(); }} }}}}

Page 19: 運算子  (Operators)

19

運算子優先順序運算子優先順序• 算術運算優先順序算術運算優先順序

– 一元遞增遞減運算子一元遞增遞減運算子– 正負號正負號– 四則運算與模數計算四則運算與模數計算

• 關連運算子關連運算子• 邏輯運算子 邏輯運算子 !, &, ^, |, &&, ||!, &, ^, |, &&, ||• 三元運算子三元運算子• 設定 設定 =,*=,/=,%=,+=,-=,&=,^=,|==,*=,/=,%=,+=,-=,&=,^=,|=• 使用括弧改變順序使用括弧改變順序

Page 20: 運算子  (Operators)

20

練習練習• 撰寫程式混合運用本章學過的運算子撰寫程式混合運用本章學過的運算子 , , 添添

加註解說明程式目的,作者,時間,及關加註解說明程式目的,作者,時間,及關鍵算式鍵算式