需要C#中的刮擦卡代码生成器的解决方案吗?

需要C#中的刮擦卡代码生成器的解决方案吗?,c#,.net,visual-studio-2010,visual-studio,data-structures,C#,.net,Visual Studio 2010,Visual Studio,Data Structures,我正在用c#Vs 2010制作一个SCRATCH卡代码生成器程序。。 这将生成可能的组合数和唯一数 表示数字不重复 例如: 5!=120组合或可能的数字 2!=2组合或可能的数字 我们可以同时给出整数值和字符串值,并生成其可能的组合 程序正在成功运行 您可以在主方法字符串c=“abc”中设置新值 例如:(您可以在此处更改值) 字符串c=“abc”; 字符串c=“1232abc” 整个代码都在控制台c中:- static void Main(字符串[]args) { 排列p=新排列(); //在这

我正在用c#Vs 2010制作一个SCRATCH卡代码生成器程序。。 这将生成可能的组合数和唯一数 表示数字不重复

例如:

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

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

我们可以同时给出整数值和字符串值,并生成其可能的组合 程序正在成功运行

您可以在主方法字符串c=“abc”中设置新值

例如:(您可以在此处更改值) 字符串c=“abc”; 字符串c=“1232abc”

整个代码都在控制台c中:-

static void Main(字符串[]args)
{
排列p=新排列();
//在这里,您可以更改字符串中的值
字符串c=“abc”;
char[]c2=c.ToCharArray();
p、 设置器(c2);
Console.ReadKey();
}
类排列
{
整数计数=0;
专用无效交换(参考字符a、参考字符b)
{
字符x;
如果(a==b)返回;
x=a;
a=b;
b=x;
}
公共无效设置器(字符[]列表)
{
int x=list.Length-1;
go(列表,0,x);
}
私有void go(字符[]列表,整数k,整数m)
{
int i;
如果(k==m)
{
控制台。写入(列表);
计数++;
Console.WriteLine(“+计数);
}
其他的
对于(i=k;i 0)
{
x++;
对于(int j=0;jValue= Value+@“

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

用编写良好的代码:

  • 你不需要经过
    ref
  • 您不应该输出类的函数,特别是为了在不同类型的事物之间重用它
  • 对于
  • 循环,应该使用
    foreach
    而不是
    
    
  • 变量和函数应该有描述性的名称来表示它们是什么,除非从上下文中看很明显
  • char[]
    在大多数情况下应该是
    string
    (这种情况可能是合法使用)

  • 对于实际的排列逻辑,请参阅的答案-唯一的变化是,您将在
    char
    而不是
    string
    上进行排列。或者,请查看从那里链接的内容。

    为什么要重写它?这几乎可以肯定是问题所在。我立即注意到,在表单应用程序中,每次舔你正在生成一个新的
    置换实例。
    。是的,Matt Burland。.我们正在调用我们的类方法,以便生成数字。.Bobson我们正在使用ref,这不是一个很好的使用方法??你能解释为什么这可能有助于我了解更多。@mustafahalai-进一步思考后,我认为这可能是一个可以接受的u但是它使用的代码级别比C语言低得多,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]);
            }
        }
    
    // 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;
        }