【发布时间】:2013-04-12 16:50:08
【问题描述】:
我正在 c# Vs 2010 中制作 SCRATCH 卡代码生成器程序。 这也会生成可能的组合数和唯一数 表示数字不重复
例如:
5! = 120 个组合或可能的数字
2! = 2 个组合或可能的数字
我们可以同时给出整数值和字符串值,并生成可能的组合 程序运行成功..
您可以在 MAIN METHOD 字符串 c = "abc"; 中设置新值;
例如:(您可以在此处更改值) 字符串 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 窗体 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;
}
为什么它没有像在 CONSOLE PROGRAM 中那样生成唯一的数字??? 问题是什么????
【问题讨论】:
-
你为什么要重写它?这几乎肯定是问题所在。
-
我立即注意到的一件事是,在您的表单应用程序中,每次单击都会生成一个新的
Permutation实例。 -
是的,Matt Burland..我们正在调用我们的类方法,以便生成数字..
标签: c# .net visual-studio-2010 visual-studio data-structures