Reputation: 1
i am making a Program of SCRATCH card code generator in c# Vs 2010.. Which Generates possible number of Combinations and unique number as well means number is not repeated
for example:
5! = 120 combination or possible numbers
2! = 2 combination or possible numbers
we can give integer value and string value at same time and it generates its possible combinations the program is successfully running..
You can set new value in MAIN METHOD string c = "abc";
for example: (You can change value here) string c = "abc"; string c = "1232abc";
THE Whole code is here in Console 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]);
}
}
The Problem is we are not able to convert in WINDOWS FORM C# when we convert it does not generate Unique numbers which was doing in CONSOLE Program we have try to do it in WINDOWS FORM what is the PROBLEM we cannot detect it
The Code for Windows form c# is (which we have tried):
// 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;
}
Why it is not generating UNIQUE numbers as it is happening in CONSOLE PROGRAM??? What is the problem????
Upvotes: 0
Views: 1434
Reputation: 13706
There's a number of issues here, none of which are the explicit problem you're having, but all of which make it that much harder to see. This looks much more like C++ code than C# code.
In well-written code:
ref
.foreach
instead of for
loops. char[]
should be string
under most circumstances (this case may be a legit use)For the actual permutation logic, see the answers to this question - the only change is that you'd be doing it on char
instead of string
. Alternatively, see here which is linked from there.
Upvotes: 1