Presentation is loading. Please wait.

Presentation is loading. Please wait.

鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

Similar presentations


Presentation on theme: "鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所"— Presentation transcript:

1 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
陣列與簡單檔案讀寫 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

2 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

3 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

4 ArrayAccess.Program. Program.Main() 片段(1/2)
const int N = 6; int[] intArray = new int[N]; for (int i = 0; i < N; i++) { intArray[i] = i * 10; } Console.WriteLine( "以下列出陣列物件intArray的所有元素值"); "intArray[{0}] : {1} ", i, intArray[i]);

5 ArrayAccess.Program. Program.Main() 片段(2/2)
string[] strArray = {"這", "是", "陣列", "測試","!!"}; Console.WriteLine( "以下列出陣列物件strArray的所有元素值"); for (int i = 0; i < strArray.Length; i++) { "strArray[{0}] : {1} ", i, strArray[i]); } "陣列strArray的長度為{0},開始索引值為{1}, 結束索引值為{2}\n", strArray.Length, strArray.GetLowerBound(0), strArray.GetUpperBound(0));

6 一維陣列 一維陣列與陣列索引 int[] intArray = new int [6]; intArray[0] intArray[3]

7 一維陣列物件特性 陣列長度 Length 陣列開始索引值 GetLowerBound(0) 陣列結束索引值 GetUpperBound(0)

8 練習 寫一程式,用迴圈輸入3位同學成績到一維陣列,再用迴圈把陣列長度及所有成績顯示出來. 修改上述程式, 使同學人數可以在程式執行時決定.

9 DiceSimulation3.Program. Program.Main() 片段 (1/2)
int[] count = new int[6]; // 累計點數出現次數 for (int i = 0; i < 6; ++i) { count[i] = 0; } const int N = 12000; // 總擲骰次數 int seed = 123; Dice dice = new Dice(seed);

10 DiceSimulation3.Program. Program.Main() 片段 (2/2)
for (int i = 0; i < N; ++i) { dice.Toss(); faceValue = dice.FaceValue; ++count[faceValue - 1]; } // 印出結果 for (int i = 0; i < 6; ++i) Console.WriteLine(" {0} appears {1} times ", i + 1, count[i]);

11 練習 擲兩個骰子12000次,統計各點數出現次數

12 繪製長條圖

13 DrawingHistogram.Program片段
class Program { static void Main(string[] args) int[] data = { 60, 75, 82, 93, 72 }; Histogram diagram = new Histogram( data ); diagram.Draw(); }

14 DrawingHistogram.Histogram 片段(1/2)
private int[] data; private const int UNIT = 5; public Histogram(int[] data) { this.data = data; } public void Draw() for (int n = 0; n < data.Length; ++n) DrawABar(n);

15 DrawingHistogram.Histogram 片段(2/2)
private void DrawABar( int n ) { Console.Write(n+"\t"+data[n]+"\t"); for (int m = 0; m < data[n]/UNIT; ++m) Console.Write("*"); } Console.WriteLine();

16 練習 擲骰子12000次,統計各點數出現次數,繪出對應長條圖 擲兩個骰子12000次,統計各點數出現次數,繪出對應長條圖
注意調整每個星號代表的間距

17 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

18 二十一點遊戲模擬v0.1流程 產生固定牌疊 電腦(莊家)向玩家(一人)及本身派發一張明牌 電腦向玩家及本身派發一張明牌
莊家詢問玩家是否加牌, 直至玩家不加牌或報到 莊家如不足 17點便需加牌直至超過或等於 17點 對未有爆煲或報到的玩家, 比點數大小, 大者勝, 如莊家爆煲, 玩家勝

19 二十一點遊戲模擬原型之測試規畫: 場景1 玩家 莊家 ♠A ♥J ♦10 勝

20 二十一點遊戲模擬例牌檢驗虛擬碼 find total points; if total points == 21, BLACK_JACK;
else if total points > 21, BURST and return; else PASS; if PASS and with Ace { sum = total points + 10; // at most one // Ace can add 10 if sum == 21, BLACK_JACK ; if sum <= 21, total points = sum; if sum > 21, keep original total points; }

21 二十一點遊戲模擬總點數計算虛擬碼 sum = 0; for i=0 to number of cards - 1 {
sum += points(rank of hand[i]); }

22 二十一點遊戲模擬牌點計算虛擬碼 points = rank; if rank > 10, points = 10;

23 二十一點遊戲模擬尋找是否有A虛擬碼 isWithAce = false; for i=0 to number of cards – 1 {
if card[i] is Ace isWithAce = true; break; }

24 類別圖 24

25 Scenario 1: Sequence Diagram

26 BlackJack_0_0_3.Program片段
class Program { static void Main(string[] args) Debug.Assert(BlackJackTest.Card_OK()); Debug.Assert(BlackJackTest.Deck_OK()); Debug.Assert( BlackJackTest.Scenario_1_OK()); }

27 BlackJack_0_0_3.BlackJackTest 片段(1/4)
public static bool Deck_OK() { Card[] cards = { new Card(Suit.SPADE, 1), new Card(Suit.HEART, 11), new Card(Suit.DIAMOND, 10) }; Deck deck = new Deck(cards); Card card; card = deck.DealACard(); bool success1 = (card.suit == Suit.SPADE && card.rank == 1);

28 BlackJack_0_0_3.BlackJackTest 片段(2/4)
bool success2 = (card.suit == Suit.HEART && card.rank == 11); card = deck.DealACard(); bool success3 = (card.suit == Suit.DIAMOND && card.rank == 10); return success1 && success2 && success3; }

29 BlackJack_0_0_3.BlackJackTest 片段(3/4)
public static bool Scenario_1_OK() { Card[] cards = { new Card(Suit.SPADE, 1), new Card(Suit.HEART, 11), new Card(Suit.DIAMOND, 10) }; Deck deck = new Deck(cards); HumanPlayer player = new HumanPlayer(); ComputerPlayer computer = new ComputerPlayer(); player.SaveACard(deck.DealACard());

30 BlackJack_0_0_3.BlackJackTest 片段(4/4)
computer.SaveACard(deck.DealACard()); player.SaveACard(deck.DealACard()); return (player.GetStatus()==Status.BLACK_JACK && computer.GetStatus()==Status.PASS); }

31 BlackJack_0_0_3.Deck 片段(1/2)
private Card[] cards; private int top; public Deck(Card[] cards) { int nCards = cards.Length; this.cards = new Card[nCards]; int i; for (i = 0; i < nCards; ++i) this.cards[i] = cards[i]; } top = 0;

32 BlackJack_0_0_3.Deck 片段(2/2)
public Card DealACard() { return cards[top++]; }

33 BlackJack_0_0_3.HumanPlayer 片段(1/8)
public enum Status { PASS = 0, BLACK_JACK = 1, BURST = 2 }

34 BlackJack_0_0_3.HumanPlayer 片段(2/8)
// a player can have at most 11 cards // {A, A, A, A, 2, 2, 2, 2, 3, 3, 3} // for not burst or BlackJack private Card[] hand = new Card[11]; private int nCards; private Status status; private int totalPoints;

35 BlackJack_0_0_3.HumanPlayer 片段(3/8)
public HumanPlayer() { nCards = 0; } public void SaveACard(Card card) hand[nCards++] = card; SetStatus();

36 BlackJack_0_0_3.HumanPlayer 片段(4/8)
private void SetStatus() { int[] point = new int[nCards]; int i; int sum = 0; for (i = 0; i < nCards; ++i) point[i] = Points(hand[i].rank); sum += point[i]; } status = JudgeStatus(sum); totalPoints = sum;

37 BlackJack_0_0_3.HumanPlayer 片段(5/8)
if (status != Status.PASS) return; bool isWithAce = false; // check if with Aces for (i = 0; i < nCards; ++i) { if (point[i] == 1) isWithAce = true; break; }

38 BlackJack_0_0_3.HumanPlayer 片段(6/8)
if (isWithAce) { sum += 10; if (sum == 21) status = Status.BLACK_JACK; } if (sum <= 21) totalPoints = sum;

39 BlackJack_0_0_3.HumanPlayer 片段(7/8)
private int Points(int rank) { int points = rank; if (rank > 10) points = 10; return points; } private Status JudgeStatus(int sum) Status status; if (sum == 21) status = Status.BLACK_JACK;

40 BlackJack_0_0_3.HumanPlayer 片段(8/8)
else if (sum > 21) { status = Status.BURST; } else status = Status.PASS; return status;

41 練習(1/2) 在BlackJack_0_0_3.BlackJackTest中添加Scenario_2_OK(),並予測試

42 練習(2/2): 場景2 玩家 莊家 ♣3 ♥J ♠10 ♦ A勝

43 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

44 二十一點遊戲模擬v0.1流程虛擬碼 產生牌疊 電腦(莊家)向玩家(一人)及本身派發一張明牌 電腦向玩家及本身派發一張明牌,並檢驗例牌
while(玩家PASS狀態且牌疊有牌且玩家加牌 ) { 向玩家派發一張明牌 } 檢驗例牌 while(莊家PASS狀態且牌疊有牌且電腦加牌 ) 向電腦派發一張明牌 判斷勝負

45 牌疊準備觀念 1 2 3 4 5 6 7 8 9 10 11 12 ♣A ♣2 ♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K 13 14 15 16 17 18 19 20 21 22 23 24 25 ♦A ♦2 ♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K 26 27 28 29 30 31 32 33 34 35 36 37 38 ♥A ♥2 ♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥10 ♥J ♥Q ♥K 39 40 41 42 43 44 45 46 47 48 49 50 51 ♠A ♠2 ♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K

46 牌疊準備演算法 pos = 亂數 % 52; while( !used[pos] ) ++pos; pos = pos % 52; }
設定bool陣列used內容均為false for i = 0 to 51 { pos = 亂數 % 52; while( !used[pos] ) ++pos; pos = pos % 52; } card[i]的花色為pos/13; card[i]的點數為pos%4+1; used[i] = true;

47 類別圖

48 BlackJack_0_0_4.Program. Program.Main() 片段(1/2)
Debug.Assert(BlackJackTest.Card_OK(), "Card test failed"); Debug.Assert(BlackJackTest.Deck_OK(), "Deck test failed"); Debug.Assert(BlackJackTest.DeckRandom_OK(), "Deck random mode test failed"); Debug.Assert(BlackJackTest.Scenario_1_OK(), "Scenario 1 test failed"); Debug.Assert(BlackJackTest.Scenario_2_OK(), "Scenario 2 test failed");

49 BlackJack0_0_4.Program. Program.Main() (2/2)
Debug.Assert(BlackJackTest.Scenario_3_OK(), "Scenario 3 test failed"); Debug.Assert(BlackJackTest.Scenario_4_OK(), "Scenario 4 test failed"); Debug.Assert(BlackJackTest.Scenario_5_OK(), "Scenario 5 test failed"); Debug.Assert(BlackJackTest.Scenario_6_OK(), "Scenario 6 test failed"); Debug.Assert(BlackJackTest.Game_OK(), "Game test failed"); Game game = new Game(); game.Run();

50 BlackJack_0_0_4.BlackJackTest 片段(1/7)
public static bool DeckRandom_OK() { Deck deck = new Deck(123); Card[] cards = new Card[52]; int i; for (i = 0; i < 52; ++i) cards[i] = deck.DealACard(); } int[] nSuit = new int [4]; int s;

51 BlackJack_0_0_4.BlackJackTest 片段(2/7)
for (s = 0; s < 4; ++s) { nSuit[s] = 0; } int[] nRank = new int[13]; int r; for (r = 0; r < 13; ++r) nRank[r] = 0;

52 BlackJack_0_0_4.BlackJackTest 片段(3/7)
for (i = 0; i < 52; ++i) { switch (cards[i].suit) //累計各花色張數 case Suit.CLUB: nSuit[0]++; break; case Suit.DIAMOND: nSuit[1]++; case Suit.HEART: nSuit[2]++;

53 BlackJack_0_0_4.BlackJackTest 片段(4/7)
case Suit.SPADE: nSuit[3]++; break; default: } nRank[cards[i].rank-1]++; //累計各牌點張數 bool suit_OK = true;

54 BlackJack_0_0_4.BlackJackTest 片段(5/7)
for (s = 0; s < 4; s++) { if (nSuit[s] != 13) suit_OK = false; break; } bool rank_OK = true; // 各花色牌均為13張

55 BlackJack_0_0_4.BlackJackTest 片段(6/7)
for (r = 0; r < 13; r++) { if (nRank[r] != 4) rank_OK = false; // 某點數牌不為4張 break; } return suit_OK && rank_OK;

56 BlackJack_0_0_4.BlackJackTest 片段(7/7)
public static bool Game_OK() { Console.WriteLine("Game 測試"); Game game = new Game(); game.Run(123); return true; }

57 BlackJack_0_0_4.HumanPlayer 片段(1/2)
public bool WantOneMoreCard() { Console.Write("要再一張牌嗎? (y/n) "); string answer = Console.ReadLine(); return (answer == "Y" || answer == "y"); } public void Dump() int i; Console.Write("玩家牌: ");

58 BlackJack_0_0_4.HumanPlayer 片段(2/2)
for (i = 0; i < nCards; ++i) { hand[i].Dump(); Console.Write("\t"); if ((i + 1) % 5 == 0) Console.WriteLine(); } Console.WriteLine("玩家總點數: " + totalPoints);

59 BlackJack_0_0_4.ComputerPlayer 片段
public bool WantOneMoreCard() { return (totalPoints < 17); }

60 BlackJack_0_0_4.Deck片段 (1/5)
private Card[] cards = new Card[52]; private int top = 0; public Deck() { Random rand = new Random(); PrepareDeck(rand); } public Deck(int seed) { Random rand = new Random(seed);

61 BlackJack_0_0_4.Deck片段 (2/5)
private void PrepareDeck(Random rand) { int i; bool[] used = new bool[52]; for (i = 0; i < 52; ++i) used[i] = false; } int pos; // position in the table given // in reference

62 BlackJack_0_0_4.Deck片段 (3/5)
int s; for (i = 0; i < 52; ++i) { pos = rand.Next() % 52; // search for un-used position while (used[pos]) ++pos; pos = pos % 52; } s = pos / 13; switch (s)

63 BlackJack_0_0_4.Deck片段 (4/5)
case 0: cards[i].suit = Suit.CLUB; break; case 1: cards[i].suit = Suit.DIAMOND; case 2: cards[i].suit = Suit.HEART; case 3: cards[i].suit = Suit.SPADE;

64 BlackJack_0_0_4.Deck片段 (5/5)
default: break; } cards[i].rank = pos % ; used[pos] = true;

65 BlackJack_0_0_4.Card 片段 (1/2)
public void Dump() { string[] ranks = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" }; switch (suit) case Suit.CLUB: Console.Write("c" + ranks[rank - 1]); break;

66 BlackJack_0_0_4.Card 片段 (2/2)
case Suit.DIAMOND: Console.Write("d" + ranks[rank - 1]); break; case Suit.HEART: Console.Write("h" + ranks[rank - 1]); case Suit.SPADE: Console.Write("s" + ranks[rank - 1]); default: }

67 BlackJack_0_0_4.Game 片段 (1/7)
Deck deck; HumanPlayer player = new HumanPlayer(); ComputerPlayer computer = new ComputerPlayer(); public void Run() { deck = new Deck(); Play(); } public void Run(int seed) deck = new Deck(seed);

68 BlackJack_0_0_4.Game 片段 (2/7)
private void Play() { player.SaveACard(deck.DealACard()); player.Dump(); computer.SaveACard(deck.DealACard()); computer.Dump(); if (IsBlackJackOrBurst()) return;

69 BlackJack_0_0_4.Game 片段 (3/7)
if (IsBlackJackOrBurst()) return; while (player.GetStatus() == Status.PASS && player.WantOneMoreCard() && deck.HasMoreCard()) { player.SaveACard(deck.DealACard()); player.Dump(); }

70 BlackJack_0_0_4.Game 片段 (4/7)
while ( computer.GetStatus() == Status.PASS && computer.WantOneMoreCard() && deck.HasMoreCard()) { computer.SaveACard(deck.DealACard()); computer.Dump(); } if (IsBlackJackOrBurst()) return;

71 BlackJack_0_0_4.Game 片段 (5/7)
Console.WriteLine("比大小分勝負"); player.Dump(); computer.Dump(); if (IsBlackJackOrBurst()) return; if (computer.GetTotalPoints() >= player.GetTotalPoints()) { Console.WriteLine("電腦勝"); } else Console.WriteLine("玩家勝"); }

72 BlackJack_0_0_4.Game 片段 (6/7)
private bool IsBlackJackOrBurst() { bool isBlackJack = false; if(player.GetStatus()==Status.BLACK_JACK) isBlackJack = true; Console.WriteLine( "玩家 BlackJack!!!"); } if(computer.GetStatus()==Status.BLACK_JACK) Console.WriteLine( "電腦 BlackJack!!!"); bool isBurst = false;

73 BlackJack_0_0_4.Game 片段 (7/7)
if(player.GetStatus()==Status.BURST) { isBurst = true; Console.WriteLine( "玩家爆!!!"); } if(computer.GetStatus()==Status.BURST) Console.WriteLine("電腦爆!!!"); return (isBlackJack || isBurst);

74 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

75 重構(Refactoring) 不好的味道(Bad program flavor) 重構 重複出現的程式碼 不妥的名稱 擷取方法 重新命名
建立新類別 使用繼承

76 重構練習 選取類別HumanPlayer中的函式名稱SaveACard
重構>重新命名>輸入新名稱ReceiveACard>確定 預覽變更>套用 相同步驟改回原名 重新建置偵錯

77 重構練習 嘗試取消類別Game中的函式IsBlackJackOrBurst,修改對應程式碼 選取改過的程式碼
修改產生的函式 重新建置偵錯

78 BlackJack_0_0_4重構: BlackJack_0_0_5
HumanPlayer與ComputerPlayer中有關判斷status的函式SetStaus、 JudgeStatus、Points完全相同 建立新類別StatusChecker,函式SetStaus, JudgeStatus, Points改成此一類別的靜態函式 SetStatus改名DetermineStatusAndTotalPoints,並修改參數 將列舉型別Status的宣告移到StatusChecker.cs

79 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

80 Array2D.Program. Program.Main() 片段
const int N_ROW = 9; const int N_COL = 9; int[,] mTable = new int[N_ROW, N_COL]; Console.WriteLine( "mTable 是{0} X {1} 的{2} 維陣列\n", N_ROW, N_COL, mTable.Rank); for (int i = 0; i < N_ROW; i++) { for (int j = 0; j < N_COL; j++) mTable[i, j] = (i+1)*(j+1); }

81 矩形陣列 int[ , ] mTable = new int [9, 9]; mTable[0,0] mTable[0,5]

82 處理列 j=0 j i=0 i data[i,j] for(i=0; i<data.GetUpperBound(0); ++i) {
for(j=0; j<data.GetUpperBound(1); ++j) { . . . }

83 處理行 j j=0 i=0 i data[i,j] for(j=0; j<data.GetUpperBound(1); ++j) {
for(i=0; i<data.GetUpperBound(0); ++i) { . . . }

84 練習 寫一程式,設值及印出3階魔方陣(河圖洛書)

85 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

86 加入文字檔 專案/加入新項目/文字檔/名稱/加入 鍵入資料 檔案/另存***為. . . 檢查儲存位置

87 串流(Stream)的觀念 StreamReader 輸入檔 Heap 記憶區 StreamWriter 輸出檔

88 FileDemo.Program (1/6) /* * 示範文字檔案之讀寫 * skj 4/20/2007 *
* 假設欲讀入之資料檔格式如下: * 第一列為一個整數代表以下每列有多少個整數數據 * 欲寫出之資料檔格式與欲讀入之資料檔格式相似 * 測試: * 欲讀入之資料檔檔名為Test.dat * 欲寫出之資料檔檔名為Test.output

89 FileDemo.Program (2/6) * 欲讀入之資料檔內容為(數據之間以一個空白隔開) * 3 * 1 4 2 * 2 3 5
* 7 6 9 * * * * 寫出之資料檔內容相同, 但數據之間以TAB('\t')分開, 即 * * * * *

90 FileDemo.Program (3/6) * 注意如果執行偵錯版本, 所用資料檔要放在專案的bin\Debug * 檔案夾,
* 如果不偵錯, 須放在bin\Release檔案夾, 否則檔案名要用完整 * 檔案路徑 */ using System; using System.IO; namespace FileDemo { class Program static void Main(string[] args)

91 FileDemo.Program (4/6) Console.Write("輸入欲讀入之資料檔名: ");
string fileName = Console.ReadLine(); StreamReader input = new StreamReader(fileName); Console.Write("輸入欲寫出之資料檔名: "); fileName = Console.ReadLine(); StreamWriter output = new StreamWriter(fileName); string line; string[] head = new string[2]; // 讀入並寫出第一列 line = input.ReadLine(); int nDataPerLine = int.Parse(line); output.WriteLine(nDataPerLine);

92 FileDemo.Program (5/6) // 讀入並寫出後續數據 string[] dataString = new
string[nDataPerLine]; int data; while (!input.EndOfStream) { line = input.ReadLine(); dataString = line.Split(' '); for ( int i = 0; i < nDataPerLine; ++i) data = int.Parse(dataString[i]); Console.Write(data + "\t"); output.Write(data + "\t"); }

93 FileDemo.Program (6/6) Console.WriteLine(); output.WriteLine(); }
output.Close(); input.Close();

94 練習 產生一文字資料檔,在其中用星號*和空白製作圖案 撰寫程式讀取此一資料檔,並在螢幕印出其中圖案

95 範例問題 假設有一N_ROW列、N_COL欄的整數表格,寫一程式計算每列總和、每欄總和、及全部總和。以如下2列3欄表格為例(Case 1):
其每列總和為 9、18,每欄總和為8、11、8,全部總和為27。利用一個二維陣列儲存表格資料,兩個一維陣列分別儲存每列總和及每欄總和。 3 4 2 5 7 6

96 資料檔格式 2, 3 3, 4, 2 5, 7, 6 i.e., <nRow>, <nCol>
<data[0,0]>, <data[0,1]>, . . ., <data[0,nCol]> . . . <data[nRow,0]>, <data[nRow,1]>, . . ., <data[nRow, nCol]>

97 TableDemo.Program片段 using System; using System.Diagnostics;
namespace TableDemo { class Program static void Main(string[] args) Debug.Assert(TableTest.Case1_OK(), "Table test: Case 1 failed"); }

98 TableDemo.TableTest 片段
public static bool Case1_OK() { Table table = new Table("Test1.dat"); int[] rowSum = table.RowSum(); Debug.Assert(rowSum[0] == 9); Debug.Assert(rowSum[1] == 18); int[] colSum = table.ColSum(); Debug.Assert(colSum[0] == 8); Debug.Assert(colSum[1] == 11); Debug.Assert(colSum[2] == 8); Debug.Assert(table.TotalSum() == 27); return true; }

99 TableDemo.Table 片段(1/6) private int nRow; private int nCol;
private int[,] data; public Table(string fileName) { StreamReader input = new StreamReader(fileName); string line; string[] head = new string[2]; line = input.ReadLine(); head = line.Split(','); nRow = int.Parse(head[0]); nCol = int.Parse(head[1]); data = new int[nRow, nCol];

100 TableDemo.Table 片段(2/6) int i; int j; for (i = 0; i < nRow; ++i) {
string[] dataString = new string[nCol]; int i; int j; for (i = 0; i < nRow; ++i) { line = input.ReadLine(); dataString = line.Split(','); for (j = 0; j < nCol; ++j) data[i,j] = int.Parse(dataString[j]); }

101 TableDemo.Table 片段(3/6) public int[] RowSum() {
int[] rowSum = new int[nRow]; for (int i = 0; i < nRow; ++i) rowSum[i] = 0; for (int j = 0; j < nCol; ++j) rowSum[i] += data[i, j]; } return rowSum;

102 TableDemo.Table 片段(4/6) public int[] ColSum() {
int[] colSum = new int[nCol]; for (int j = 0; j < nCol; ++j) colSum[j] = 0; for (int i = 0; i < nRow; ++i) colSum[j] += data[i, j]; } return colSum;

103 TableDemo.Table 片段(5/6) public int TotalSum() { int totalSum = 0;
for (int i = 0; i < nRow; ++i) for (int j = 0; j < nCol; ++j) totalSum += data[i, j]; } return totalSum;

104 練習 在類別Table中增加建構式public Table(int[,] data) ,以便由一個矩形陣列建立表格
在類別TableTest中增加public static bool Case2_OK() ,以測試上述建構式 修改主程式,進行測試

105 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

106 程式 Array2DA /* * 以下三角矩陣 * * / x \ * | x x | * | x x x | * | x x x x |
* 示範不規則二維陣列之使用 * 9/29/2008 */

107 Array2DA.Program 片段 const int N_ROW = 5;
char[][] lowerTriangularMatrix = new char[N_ROW][]; for (int i = 0; i < N_ROW; i++) { lowerTriangularMatrix[i] = new char[i + 1]; } for (int j = 0; j < lowerTriangularMatrix[i].Length; j++) lowerTriangularMatrix[i][j] = ‘x';

108 不規則二維陣列 int[][] a = new int [5]; a[0][0] a[1][1] a[0]= new int[1];

109 練習 宣告並由使用者輸入再印出一個不規則二維整數陣列, 此一陣列共五列, 每列的長度分別為1, 2, 3, 2, 1。

110 綱要 一維陣列 二十一點模擬程式0.0.3版 二十一點模擬程式0.0.4版 重構 矩形二維陣列 簡單檔案讀寫 不規則二維陣列
*foreach 敘述

111 UsingForEach.Program 片段
string[,] strArray = { {"Demonstrating", "the", "use", "of", "foreach"}, {"\nGood", "for", "high", "dimensional", "array"} }; foreach (string str in strArray) { Console.Write(str+" "); } Console.WriteLine();


Download ppt "鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所"

Similar presentations


Ads by Google