简体   繁体   English

如何制作 WinForms 冒泡排序程序?

[英]How do I make a WinForms bubblesort program?

We just started using c# and the whole class knows nothing.我们刚开始使用c#,全班同学什么都不知道。 Yet, the teacher told us to use WinForms and c# to make a program that used bubblesort.然而,老师告诉我们用WinForms和c#来做一个使用冒泡排序的程序。 I looked around the web and only found validation for the text boxes (numbers only).我环顾网络,只找到了文本框的验证(仅限数字)。

I'm new here but I would like to ask for help if possible.我是新来的,但如果可能的话,我想寻求帮助。 We have to deliver this work today and we've got nothing so far.我们今天必须交付这项工作,到目前为止我们什么都没有。

This is the code I have.这是我的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace maedofeixeira
{
public partial class Form1 : Form
{
    int[] elementos = new int[5];
    public Form1()
    {
        InitializeComponent();
    }

    private void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
        {
            MessageBox.Show("SO NUMEROS!!!CRL.");
            textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
        }
    }

    private void TextBox2_TextChanged(object sender, EventArgs e)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(textBox2.Text, "[^0-9]"))
        {
            MessageBox.Show("SO NUMEROS!!!CRL.");
            textBox2.Text = textBox2.Text.Remove(textBox2.Text.Length - 1);
        }
    }

    private void TextBox3_TextChanged(object sender, EventArgs e)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(textBox3.Text, "[^0-9]"))
        {
            MessageBox.Show("SO NUMEROS!!!CRL.");
            textBox3.Text = textBox3.Text.Remove(textBox3.Text.Length - 1);
        }
    }

    private void TextBox4_TextChanged(object sender, EventArgs e)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(textBox4.Text, "[^0-9]"))
        {
            MessageBox.Show("SO NUMEROS!!!CRL.");
            textBox4.Text = textBox4.Text.Remove(textBox4.Text.Length - 1);
        }
    }

    private void TextBox5_TextChanged(object sender, EventArgs e)
    {
        if (System.Text.RegularExpressions.Regex.IsMatch(textBox5.Text, "[^0-9]"))
        {
            MessageBox.Show("SO NUMEROS!!!CRL.");
            textBox5.Text = textBox5.Text.Remove(textBox5.Text.Length - 1);
        }
    }

    private void Bubblesort()
    {
        int refos = 0;
        for (int i=0;i<elementos.Length-1;i++)
        {
            for (int j = 0; j < elementos.Length - (i + 1); j++)
            {
                if (elementos[j] > elementos[j + 1])
                {
                    refos = elementos[j];
                    elementos[j] = elementos[j + 1];
                    elementos[j + 1] = refos;
                }
            }
        }
    }

    private void Button1_Click(object sender, EventArgs e)
    {
        Bubblesort();
        for(int i=0;i<elementos.Length;i++)
        {
            lbOrdenada.Items.Add(elementos[i]);
        }
    }
}
}

Screenshot:截屏: 截屏

Problem so far: When we hit the "Ordenar" button, it shows 5 zeros.到目前为止的问题:当我们点击“Ordenar”按钮时,它显示 5 个零。

You need to put the values from the text boxes into the elementos array.您需要将文本框中的值放入 elementos 数组中。 Because of the general messiness of the code, this can't be done elegantly, but something like this should do it:由于代码的一般混乱,这不能优雅地完成,但应该这样做:

private void Button1_Click(object sender, EventArgs e)
{
    elementos[0] = Convert.ToInt32(TextBox1.Text);
    elementos[1] = Convert.ToInt32(TextBox2.Text);
    elementos[2] = Convert.ToInt32(TextBox3.Text);
    elementos[3] = Convert.ToInt32(TextBox4.Text);
    elementos[4] = Convert.ToInt32(TextBox5.Text);
    Bubblesort();
    for(int i=0;i<elementos.Length;i++)
    {
        lbOrdenada.Items.Add(elementos[i]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM