Hello, I have problem that I need to solve.
I have 3 string "a", "b" and "c".
I need all varabiles to be added in listbox.
Sample of list:
aaa
aab
aac
abc
abb
acc
acb
baa
bba
bca
caa
cba
cca
cac
bac
etc.
Can someone show me some code for this problem ?
Loading
FroglegPosted Feb 23, 2011, 3:00 PM
private void button3_Click(object sender, EventArgs e)
{
listBox1.Items.Add("aaa");
listBox1.Items.Add("ccc");
listBox1.Items.Add("bbb");
char[] abc = new char[3]{'a','b','c'};
setper(abc);
char[] aab = new char[3] { 'a', 'a', 'b' };// you will need all char combinations eg aac,bba etc
setper(aab);
listBox1.Sorted = true;
//label1.Text = Convert.ToString(listBox1.Items.Count);
}
private void swap(ref char a, ref char b)
{
if (a == b)return;
a ^= b;
b ^= a;
a ^= b;
}
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)
{
if (listBox1.Items.Contains(list[0].ToString() + list[1].ToString() + list[2].ToString()) == false)
{
listBox1.Items.Add(list[0].ToString() + list[1].ToString() + list[2].ToString());
}
}
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]);
}
}
Janis PeksaPosted Feb 28, 2011, 9:47 AM
Janis PeksaPosted Feb 24, 2011, 4:54 AM
FroglegPosted Feb 24, 2011, 4:22 AM
Janis PeksaPosted Feb 24, 2011, 3:37 AM
Ops, I realy didn't try a code. [Edited]
Sam HobbsPosted Feb 23, 2011, 9:41 PM
char[] ca = { 'a', 'b', 'c' };
String Permutation;
foreach (char c1 in ca)
foreach (char c2 in ca)
foreach (char c3 in ca)
Permutation = String.Format("{0} {1} {2}", c1, c2, c3);
Suthish NairPosted Feb 23, 2011, 3:18 PM
After Frogleg given the solution, your query changed to something else symbols.
So why not asked in first thread itself.
Thats why i said, your question is not clear.
Janis PeksaPosted Feb 23, 2011, 2:49 PM
What do you not understand ?
I need in all combinations 'a', 'b' and 'c'
to be added in listbox.
Suthish NairPosted Feb 23, 2011, 2:41 PM
Janis PeksaPosted Feb 23, 2011, 2:36 AM
Janis PeksaPosted Feb 22, 2011, 2:38 AM
If I will have 5 or 10 symbols ?
Need somekind of generator or command to do this.
FroglegPosted Feb 21, 2011, 4:46 PM
listBox1.Items.Add("aaa");
listBox1.Items.Add("aab");
listBox1.Items.Add("aac");
listBox1.Items.Add("aba");
listBox1.Items.Add("abb");
listBox1.Items.Add("abc");
listBox1.Items.Add("aca");
listBox1.Items.Add("acb");
listBox1.Items.Add("acc");
listBox1.Items.Add("baa");
listBox1.Items.Add("bab");
listBox1.Items.Add("bac");
listBox1.Items.Add("bba");
listBox1.Items.Add("bbb");
listBox1.Items.Add("bbc");
listBox1.Items.Add("bca");
listBox1.Items.Add("bcb");
listBox1.Items.Add("caa");
listBox1.Items.Add("cab");
listBox1.Items.Add("cac");
listBox1.Items.Add("cba");
listBox1.Items.Add("cbb");
listBox1.Items.Add("cca");
listBox1.Items.Add("ccc");
listBox1.Sorted = true;
is less code
Janis PeksaPosted Feb 21, 2011, 8:52 AM
FroglegPosted Feb 21, 2011, 8:51 AM