0

我正在 c# Vs 2010 中制作一个 SCRATCH 卡代码生成器程序。它生成可能的组合数量和唯一数字以及意味着数字不重复

例如:

5!= 120 个组合或可能的数字

2!= 2 个组合或可能的数字

我们可以同时给出整数值和字符串值,它会生成程序成功运行的可能组合..

您可以在 MAIN METHOD 字符串中设置新值 c = "abc";

例如:(您可以在此处更改值) string c = "abc"; 字符串 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 FORM C# 中进行转换,当我们转换它时不会生成在 CONSOLE 程序中执行的唯一数字我们尝试在 WINDOWS FORM 中执行此操作 什么是我们无法检测到的问题

Windows form 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;
    } 

为什么它没有像在控制台程序中那样生成唯一的数字???问题是什么????

4

1 回答 1

1

这里有很多问题,没有一个是你遇到的明确问题,但所有这些都使它更难看到。这看起来更像C++ 代码而不是 C# 代码。

在编写良好的代码中:

  1. 你永远不需要经过ref
  2. 您不应该从类的函数中输出,特别是这样您就可以在不同类型的事物之间重用它。
  3. 您应该使用foreach而不是for循环。
  4. 变量和函数应该有描述性的名称来表明它们是什么,除非从上下文中很明显。
  5. char[]应该string在大多数情况下(这种情况下可能是合法使用)

对于实际的排列逻辑,请参阅此问题的答案- 唯一的变化是您将在char而不是string. 或者,请参阅从那里链接的此处

于 2013-04-12T17:24:52.020 回答