2013-04-12 38 views
0

我想提出的刮獎卡代碼生成的程序在C#中的VS 2010 .. 產生組合的可能數量和唯一編號以及 意味着數量不重複需要C#中的刮刮卡代碼生成器的解決方案嗎?

例如:

5! = 120組合或可能的數字

2! = 2的組合或可能的數字

我們可以給整數值,並在同一時間字符串值,並且它產生其可能的組合 程序成功運行..

可以在主要方法串c設置新值= 「ABC」;

例如:(您可以在此更改值) string c =「abc」; string c =「1232abc」;

整個代碼是在這裏控制檯C#: -

static void Main(string[] args) 
{ 
    Permute p = new Permute(); 
    // here u can change value in string 
    string c = "abc"; 
    char[] c2 = c.ToCharArray(); 
    p.setper(c2); 
    Console.ReadKey(); 
} 

class Permute 
{ 
    int count = 0 ; 
    private void swap (ref char a, ref char b) 
    { 
     char x; 
     if(a==b)return; 
     x = a; 
     a = b; 
     b = x; 
    } 

    public void setper(char[] list) 
    { 
     int x=list.Length-1; 
     go(list,0,x); 
    } 

    private void go (char[] list, int k, int m) 
    { 
     int i; 
     if (k == m) 
     { 
      Console.Write (list); 
      count++; 
      Console.WriteLine (" "+ count ); 
     } 
     else 
     for (i = k; i <= m; i++) 
     { 
      swap (ref list[k],ref list[i]); 
      go (list, k+1, m); 
      swap (ref list[k],ref list[i]); 
     } 
    } 

問題是,我們不能在Windows窗體C# 轉換時,我們把它不會產生這是在控制檯程序中做的唯一編號 我們已經嘗試做它在Windows窗體有什麼問題,我們不能檢測到它

的代碼,Windows窗體C#是(我們都試過):

// this is our Button 1 we have changed name to BtGenerate 
// we have used LISTBOX to show the generated values 
// or u can say to show numbers in Listbox 
private void BtGenerate_Click(object sender, EventArgs e) 
{ 
    string abc = textBox1.Text; 
    char[] c = abc.ToCharArray(); 
    Permutation p = new Permutation(); 
    string value = p.setper(c); 

    // here is our listbox 
    listBox1.Items.Add(value); 
} 

// we have used the PERMUTATION Class here 
// which is generating numbers 
// this class is changed compared to console Permutation class 
class Permutation 
{ 
    public string Value; 

    private void swap(ref char a, ref char b) 
    { 
     if (a == b) return; 
     a ^= b; 
     b ^= a; 
     a ^= b; 
    } 

    public string setper(char[] list) 
    {    
     int x = list.Length - 1; 
     string ValueInString = go(list, 0, x); 
     return ValueInString;      
    } 

    int x = 0; 
    private string go(char[] list, int k, int m) 
    { 
     int i; 

     if (k == m) 
     {    
      if(x == 0) 
      { 
       x++; 
       for (int j = 0; j < list.Length; j++) 
       {     
        Value = Value + list[j].ToString(); 
       } 
       //this code is used which gives gap like arrow to seperate number 
       //because we are not able to generate each number to new line 

       Value = Value + @"<--" + x + " "; 
      } 
      if (x > 0) 
      { 
       x++; 

       for (int j = 0; j < list.Length; j++) 
       { 
        Value = Value + list[j].ToString(); 
       } 

       Value = Value + @"<--"+ x + " "; 
      }    
     } 
     else 
      for (i = k; i <= m; i++) 
      { 
       swap(ref list[k], ref list[i]); 
       go(list, k + 1, m); 
       swap(ref list[k], ref list[i]); 
      } 
     return Value; 
    } 

爲什麼它不生成唯一的數字,因爲它發生在控制檯程序中??? 是什麼問題?

+1

你爲什麼重寫它?這幾乎是肯定的問題。 – Bobson

+1

我立即注意到的一件事是,在你的表單應用程序中,每次你點擊你正在生成一個'Permutation'的新實例。 –

+0

是Matt Burland ..我們正在調用我們的類方法,該數字可能會生成.. – mh51

回答

1

這裏有很多問題,其中沒有一個是你遇到的顯而易見的問題,但所有這些都使得它更難以看清。這看起來很像很多比C#代碼更像C++代碼。

在精心編寫的代碼:

  1. 你不應該在任何時候需要通過ref通過。
  2. 您不應該從類的函數中輸出,特別是爲了您可以在不同類型的東西之間重用它。
  3. 您應該使用foreach而不是for循環。
  4. 變量和函數應該有描述性的名稱來表明它們是什麼,除非它從上下文中顯而易見。
  5. char[]應該是在大多數情況下string(這種情況可能是合法的使用)

對於實際的排列邏輯,看到答案this question - 唯一的變化是,你會做在char而不是string。或者,請參閱從那裏鏈接的here

+0

Bobson我們正在使用ref,這不是一個好方法來使用它?你可以解釋爲什麼這可能會幫助我瞭解更多.. – mh51

+0

@mustafahalai - 經過進一步思考,我認爲這可能是一個可以接受的用法。但它使用的代碼要低得多,而不是C#通常的代碼,你直接交換數組中的兩個元素的值,而不是操作'List '。 – Bobson