【问题标题】:Different Sorting methods and random不同的排序方法和随机
【发布时间】:2012-10-24 02:56:42
【问题描述】:

我正在使用randomsorting 方法。到目前为止,我有线性方法工作。我正在尝试实现一种方法,让用户决定使用哪种类型的排序,例如linearbubbleindex 等。所以我添加了带有标签textBox7Key 和@ 987654328@,用户点击它,直到找到他们想要的排序方法。当 Keytextbox7 选择了一个值时,排序数组button2 将执行该特定排序方法。如何根据Key 值执行特定的排序方法?下面是我如何设置 windows 窗体的图片

代码

namespace sortmachine
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

        }


        Stopwatch sw = new Stopwatch();


        Random r = new Random();

        private int size = 0;
        private int max = 0;
        private int ops = 0;
        private int[] ar1;
        private int[] ar2;
        private int[] ar3;

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                size = Convert.ToInt32(textBox2.Text);
                max = Convert.ToInt32(textBox3.Text);
                string s = "";

                ar1 = new int[size];
                ar2 = new int[size];
                ar3 = new int[size];

                for (int i = 0; i < size; i++)
                {
                    int n = r.Next(0, max);
                    ar1[i] = n;
                    ar2[i] = ar1[i];
                }

                for (int i = 0; i < size; i++)
                {
                    s += ar1[i].ToString() + " ";
                }
                listView1.Items.Add(s);
            }
            catch
            {
                MessageBox.Show("Please input information");
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int min = max;
            int n = 0;
            string s="";

            sw.Start();

            for (int j = 0; j < size; j++)
            {
                for (int i = 0; i < size; i++)
                {
                    if (min > ar2[i])
                    {
                        min = ar2[i];
                        n = i;
                        ops++;
                    }
                    if (min < ar2[i])
                    {
                        ops++;
                    }

                }
                ar2[n]=max;
                ar3[j] = min;
                min = max;
                s += ar3[j] + " ";
            }

            TimeSpan x = sw.Elapsed;
            textBox5.Text = x.ToString();

                listView2.Items.Add(s);
                listView2.Text = s;
                textBox6.Text = Convert.ToString(ops);
                if (checkBox1.Checked==true)
                {
                    textBox1.AppendText(s+Environment.NewLine);
                }
                for (int i = 0; i < size; i++)
                {
                    ar2[i] = ar1[i];
                }
                ops = 0;

        }

    }
}

【问题讨论】:

  • 将排序与 UI 分开。每个排序方法都应该有一个像List&lt;int&gt; BubbleSort (List&lt;int&gt; input) 这样的签名——它甚至可以被进一步提取到一个接口中,但这应该“更干净”——排序函数应该使用任何成员变量:仅输入参数和一些局部变量。 UI 只是获取要排序的内容并将其提供给 sort 函数,然后获取排序结果并将其推回 UI。各种算法可以在维基百科等上找到。
  • 另外,使用 new Random(Guid.NewGuid().GetHashCode()) 播种随机数。
  • 查理,如果您需要任何其他帮助,请告诉我。我使用winforms有点工作,总能用一些知识联系你。或者,如果您想要我的回答中的项目,那也是 :)。

标签: c# .net winforms listview


【解决方案1】:

我试试看,因为它实现了其他人的建议:

在每个 if 条件中,您可以针对每种排序类型执行自己的逻辑。如您所见,我从组合框中选择了一个排序选项,其中包含其他按钮,用于将数字添加到我的列表中进行排序,并使用一个控件来显示结果。

截图中的代码如下:

if(comboBox1.SelectedItem.ToString() == "Linear")
{
}
else if (comboBox1.SelectedItem.ToString() == "Bubble")
{            
}
else if (comboBox1.SelectedItem.ToString() == "Index")
{
}

【讨论】:

    【解决方案2】:

    我建议使用组合框或 DropDownList 来存储排序选项,并使用其 SelectedText 属性来确定在用户单击排序时使用哪种排序方法。

    只需将组合框拖到表单上即可。然后向其中添加项目,单击编辑项目并填写值。

    那么你在代码中所做的就是写类似的东西。

    string sortOption = comboBox1.SelectedText;
    

    【讨论】:

    • +1 你能告诉我用组合框或 DropDownList 来实现吗?
    【解决方案3】:

    如何使用RadioButtonList 在排序选项之间进行选择,以确定用户所需的排序而不是键值方法。

    只需将RadioButtons 放在一些GroupBox 中,一次只允许选择一个RadioButton,然后单击排序按钮,您可以检查每个RadioButtonChecked 属性以应用排序.

    if (rdoBtnQuick.Checked)
        // Apply Quick Sort
    else if (rdoBtnSelection.Checked)
        // Apply Selection Sort
    else if (rdoBtnBubble.Checked)
        // Apply Bubble Sort
    //...
    

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 2011-11-22
      • 2022-01-17
      • 1970-01-01
      • 2012-12-04
      • 2016-08-10
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多