Download presentation
Presentation is loading. Please wait.
1
鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
C#核心命名空間 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
2
Math 類別 公用常數 公用函式 E PI 三角函數: Sin、Cos、Tan、Asin、Acos、Atan、Atan2
指數與對數:Log、Log10、Exp、Pow 代數:Abs、Ceiling、Floor、Round、Max、Min、Sign、Sqrt 雙曲函數:Sinh、Cosh、Tanh
3
Random 類別 公用建構式 公用函式 Random() Random(int seed) virtual int Next()
virtual double NextDouble() virtual int Next(int maxValue) virtual int Next(int minValue, int maxValue)
4
UsingRandom.Program (1/2)
using System; namespace UsingRandom { /* * 模擬擲骰子以示範Random類別之使用 * skj 5/21/2007 */ class Program static void Main(string[] args) Random dice = new Random(777); const int MAX_DICE_VALUE = 6; int[] accumulation = new int[MAX_DICE_VALUE];
5
UsingRandom.Program (2/2)
for (int i = 0; i < MAX_DICE_VALUE; ++i) { accumulation[i] = 0; } const int N = 12000; int faceValue; for (int i = 0; i < N; ++i) { faceValue = dice.Next(1, MAX_DICE_VALUE+1); accumulation[faceValue-1]++; Console.WriteLine( "點數" + (i+1) + " 出現" + accumulation[i] + " 次");
6
練習 將0~1分為10個區間,檢查Random.NextDouble產生的亂數是否均勻分佈於這10個區間。
7
實值資料型別對應結構 byte : Byte int : Int32 long : Int64 float : Single
double : Double bool : Boolean char : Char decimal : Decimal
8
數值結構欄位及函式 欄位 MaxValue MinValue 函式 Parse()
9
浮點數結構欄位與函式 const double NaN const double NegativeInfinity
const double PositiveInfinity bool IsInfinity(double d) bool IsNaN(double d) bool IsNegativeInginity(double d) bool IsPositiveInfinity(double d)
10
CheckingInfinityAndNaN.Program (1/2)
using System; namespace CheckingInfinityAndNaN { /* * 示範檢驗Infinity及NaN的方法 * skj, 5/21/2007 */ class Program static void Main(string[] args) Console.WriteLine("1.0/0.0為無限大? " Double.IsInfinity(1.0/0.0)); Console.WriteLine("1.0/0.0非一數值? " + Double.IsNaN(1.0/0.0));
11
CheckingInfinityAndNaN.Program (2/2)
Console.WriteLine("0.0/0.0為無限大? " + Double.IsInfinity(0.0/0.0)); Console.WriteLine("0.0/0.0非一數值? " + Double.IsNaN(0.0/0.0)); }
12
字元結構函式 bool IsDigit(char c) bool IsLetter(char c)
bool IsLetterOrDigit(char c) bool IsPunctuation(char c) bool IsUpper(char c) bool IsLower(char c) char ToLower(char c) char ToUpper(char c)
13
練習 檢驗你的電腦中int、long、float、double等型別的數值範圍
14
Array 類別 提供建立、管理、搜尋和排序陣列的方法 做為所有陣列的基底類別
15
UsingArray.Program (1/3)
using System; namespace UsingArray { /* 示範System.Array類別之函式的使用 * skj, 5/22/2007 */ class Program static void Main(string[] args) const int N = 6; string[] strArray = new string[N]; strArray[0] = "this"; strArray[1] = "is";
16
UsingArray.Program (2/3)
strArray[2] = "a"; strArray[3] = "test"; strArray[4] = "array"; strArray[5] = "!!"; PrintArray(strArray, "原始陣列"); Array.Reverse(strArray); PrintArray(strArray, "反轉後陣列"); Array.Sort(strArray); PrintArray(strArray, "排序後陣列"); string[] anotherArray = new string[N]; Array.Copy(strArray, anotherArray, N); PrintArray(strArray, "複製陣列"); }
17
UsingArray.Program (3/3)
static void PrintArray(string[] strArray, string arrayName) { Console.WriteLine("以下為{0} 內之元素", arrayName); foreach (string str in strArray) Console.Write(str + "\t"); } Console.WriteLine();
18
PassingArrayCopy.Program (1/4)
using System; namespace PassingArrayCopy { /* * 示範Array.Copy的使用時機 * skj 5/22/2007 */ class Program static void Main(string[] args) Test test = new Test(); int[] a = test.GetArray(); PrintArray(a); test.ModifyArray();
19
PassingArrayCopy.Program (2/4)
PrintArray(a); test.ResetArray(); a = test.GetArrayCopy(); test.ModifyArray(); } static void PrintArray(int[] a) { foreach (int ai in a) Console.Write(ai + "\t"); Console.WriteLine();
20
PassingArrayCopy.Program (3/4)
public class Test { private int[] a; public Test(){ a = new int[3]; ResetArray(); } public int[] GetArray(){ return a; public void ModifyArray(){ a[0]++; a[1]++; a[2]++;
21
PassingArrayCopy.Program (4/4)
public void ResetArray() { a[0] = 1; a[1] = 2; a[2] = 3; } public int[] GetArrayCopy() int[] b = new int[3]; Array.Copy(a, b, 3); return b;
22
複製建構式 (1/3) class Person { private string name; private int age;
// Copy constructor. public Person(Person previousPerson) name = previousPerson.name; age = previousPerson.age; } // Instance constructor. public Person(string name, int age) { this.name = name; this.age = age;
23
複製建構式 (2/3) // Get accessor. public string Details { get
return name + " is " + age.ToString(); }
24
複製建構式 (3/3) class TestPerson { static void Main()
// Create a new person object. Person person1 = new Person("George", 40); // Create another new object, copying person1. Person person2 = new Person(person1); System.Console.WriteLine(person2.Details); }
25
CopyConstructor.Program (1/3)
using System; namespace CopyConstructor { /* * 示範複製建構式之使用 * skj 5/24/2007 */ class Program static void Main(string[] args) double a = 5.0; Square sq0 = new Square(a); Square sq1 = sq0; Console.WriteLine("正方形sq1邊長為" + sq1.accessA);
26
CopyConstructor.Program (2/3)
sq0.accessA = a + 1.0; Console.WriteLine("正方形sq1邊長為" sq1.accessA); Square sq2 = new Square(sq0); Console.WriteLine("正方形sq2邊長為" + sq2.accessA); }
27
CopyConstructor.Program (3/3)
public class Square { private double a; public Square(double a) { this.a = a; } public Square(Square sq){ a = sq.a; public double accessA get { return a; } set { a = value; }
28
練習 改寫Test類別,增加複製建構式,並予測試
Similar presentations