2011-09-01 55 views
0

我想增加傳遞數,但目前爲止輸出的內容顯示的格式不正確。例如,我進入5個號碼等5 4 3 2 1,格式將是:增量傳遞數

通行證1:1 4 3 2 5
通行證2:1 4 3 2 5
通行證3:1 2 3 4 5
通行證4:1 2 3 4 5
通行證5:1 2 3 4 5

但我的當前輸出是:

通行證1:2:3:4:5:1 4 3 2 5
通過1:2:3:4:5:1 4 3 2 5
通過1:2:3:4:5:1 2 3 4 5
通行證1:2:3:4:5:1 2 3 4 5
通行證1:2:3:4:5:1 2 3 4 5

我卡與for循環語句。任何想法我將如何做這種輸出。我正在做一個快速排序。

int[] nums = new int[100]; 
    int SizeNum; 
    bool isNum = false; 

    private void ExeButton_Click(object sender, EventArgs e) 
    { 
      string SizeString = SizeTextBox.Text; 
      isNum = Int32.TryParse(SizeString, out SizeNum); 
      string[] numsInString = EntNum.Text.Split(' '); //split values in textbox 
      for (int j = 0; j < numsInString.Length; j++) 
      { 
       nums[j] = int.Parse(numsInString[j]); 
      } 
      if (SizeNum == numsInString.Length) 
      { 

       sortArray(); 
      } 
     } 
    } 

    public void q_sort(int left, int right) 
    { 
     int pivot, l_hold, r_hold; 

     l_hold = left; 
     r_hold = right; 
     pivot = nums[left]; 

     while (left < right) 
     { 
      while ((nums[right] >= pivot) && (left < right)) 
      { 
       right--; 
      } 

      if (left != right) 
      { 
       nums[left] = nums[right]; 
       left++; 
      } 

      while ((nums[left] <= pivot) && (left < right)) 
      { 
       left++; 
      } 
      if (left != right) 
      { 
       nums[right] = nums[left]; 
       right--; 
      } 
     } 
     nums[left] = pivot; 
     pivot = left; 
     left = l_hold; 
     right = r_hold; 
     Display(); 
     if (left < pivot) 
     { 
      q_sort(left, pivot - 1); 
     } 
     if (right > pivot) 
     { 
      q_sort(pivot + 1, right); 
     } 
    } 

    public void sortArray() 
    { 
     q_sort(0, SizeNum - 1); 
    } 

    public void Display() 
    { 
     int i; 
     int x; 
     String numbers = ""; 
     ResultText.AppendText("Pass "); 
     for (x = 1; x < SizeNum; x++) 
     { 

      ResultText.AppendText(" " + x + ": "); 

     } 
     for (i = 0; i < SizeNum; i++) 
     { 
      numbers += nums[i].ToString() + " , "; 
     } 
     ResultText.AppendText(numbers + "\n"); 
    } 
+0

你提的問題是非常清楚 - 「通行證數量」的含義並不明顯。 –

回答

1

最簡單的方法是通過你的快速排序功能一起傳遞調試變量來保持當前狀態的跟蹤,如:

public void q_sort(int left, int right, int currentPass) 
{ 
    /* ... */ 

    Display(currentPass); 

    /* ... */ 

    q_sort(left, pivot - 1, currentPass + 1); 
    q_sort(pivot + 1, right, currentPass + 1); 

    /* ... */ 
} 

public void Display(int currentPass) 
{ 
    ResultText.AppendText("Pass " + currentPass); 

    // output the array contents as you currently do 
} 
+0

但currentPass不會爲父母更新,因此不會一直增加? –

0

您SizeNum始終是5所以在顯示的循環:

for (x = 1; x < SizeNum; x++) 
    { 

     ResultText.AppendText(" " + x + ": "); 

    } 

將始終打印1-5。您需要以某種方式讓Display知道它被調用了多少次才能獲得正確的標籤。

0

在類的頂部添加

int Pass=0; 

,然後在顯示常規更換

for (x = 1; x < SizeNum; x++) 
     { 

      ResultText.AppendText(" " + x + ": "); 

     } 

ResultText.AppendFormat("{0}: ",Pass++); 

當你的代碼看起來像它可以運行一個以上一旦你可能想在SortArray()中重設Pass = 1,

0

讓我們來看看這第一個for循環:

for (x = 1; x < SizeNum; x++) 
{ 
    ResultText.AppendText(" " + x + ": "); 
} 

請注意,您要統計從1SizeNum。此循環會將每個數字附加到您的結果中,這就是爲什麼您在每行上都獲得Pass 1: 2: 3: 4: 5:的原因。如果你輸入更多的數字,你會看到輸出反映了這一點。例如,如果您輸入一個包含10個數字的數組,您將在數組後面看到Pass 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:

爲了讓你在找什麼,你需要在當前迭代傳遞給你的函數,如一些答案已經指出,像這樣:

private void Display(int pass) 
    // initialise your other variables here 
    ResultText.AppendText("Pass " + pass + ": "); 

    // output your array as normal here