简体   繁体   中英

How do I make a WinForms bubblesort program?

We just started using c# and the whole class knows nothing. Yet, the teacher told us to use WinForms and c# to make a program that used bubblesort. 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.

You need to put the values from the text boxes into the elementos array. 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]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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