1 / 20

運算子 (Operators)

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

artie
Download Presentation

運算子 (Operators)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 運算子 (Operators) 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

  2. 程式 UsingMathOperator (1/2) using System; namespace UsingMathOperator { class Program { static void Main(string[] args) { int x; int y; Console.WriteLine("請輸入第一個整數值x :"); x = int.Parse(Console.ReadLine()); Console.WriteLine("請輸入第二個整數值y :"); y = int.Parse(Console.ReadLine()); Console.WriteLine(" x + y = {0} ", x + y); Console.WriteLine(" x - y = {0} ", x - y);

  3. 程式 UsingMathOperator (2/2) Console.WriteLine(" x * y = {0} ", x * y); Console.WriteLine(" x / y = {0} ", x / y); Console.WriteLine(" x % y = {0} ", x % y); } } }

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

  5. 程式 UsingInDeOperator (1/3) using System; namespace UsingInDeOperator { class Program { static void Main(string[] args) { int x0; int x; int add; Console.WriteLine("請輸入整數變數x初值"); x0 = int.Parse(Console.ReadLine()); Console.WriteLine("請輸入所要加總的整數值add"); add = int.Parse(Console.ReadLine());

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

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

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

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

  10. 程式UsingLB (1/2) using System; namespace UsingLB { class Program { static void Main(string[] args) { bool x = 7 > 3; bool y = 2 < 0; Console.WriteLine("x = " + x); Console.WriteLine("y = " + y); bool xORy = x | y; Console.WriteLine("x | y :" + xORy); bool xANDy = x & y; Console.WriteLine("x & y :" + xANDy);

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

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

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

  14. 一般邏輯運算

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

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

  17. 程式 UsingTerOp using System; namespace UsingTerOp { /* * 用調分公式說明三元運算子的使用 * skj 3/4/2007 */ class Program { static void Main(string[] args) { int grade; int result;

  18. 程式 UsingTerOp Console.WriteLine("請輸入一個小於的整數原始成績:"); grade = int.Parse(Console.ReadLine()); result = grade < 60 ? 60 : grade; // 調分公式 Console.WriteLine("調分後成績: " + result); Console.ReadLine(); } } }

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

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

More Related