1/02/2014

Bài Giải Về Winfrom Thuật Toán Sắp

 
Winfrom c#

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

namespace winformsapxep
{
    public partial class Form1 : Form
    {
        int n = 0, i, tmp;
        int[] a = new int[100];
        string s, s1;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtnhap.Enabled = true;
            txtxuat.Enabled = false;
            btxoa.Enabled = false;
            btthuchien.Enabled = false;

        }

        private void btnhap_Click(object sender, EventArgs e)
        {
            
            btthuchien.Enabled = true;
            btxoa.Enabled = true;
            s = txtnhap.Text;
            i = s.LastIndexOf(" ");
            while (i != -1)
            {
                s1 = s.Substring(i);
                s = s.Substring(0, i);
                a[n] = Convert.ToInt32(s1);
                n++;
                i = s.LastIndexOf(" ");

            }
            a[n] = Convert.ToInt32(s);
            s = " ";
            for (i = n; i >= 0; i--)
            {
                s = s + " " + a[i].ToString();
                txtxuat.Text = s.Trim();
            }

        }

        private void btthuchien_Click(object sender, EventArgs e)
        {
            if (rdtang.Checked == true)
            {
                for ( i = 0; i < n; i++)
              
                    for (int j =i +1; j <= n; j++)
                    
                        if (a[i] > a[j])
                        {
                            tmp = a[i];
                            a[i] = a[j];
                            a[j] = tmp;
                        }
                        s = " ";
                        for (i = 0; i <= n; i++)
                       
                            s = s + " " + a[i].ToString();
                            txtxuat.Text = s.Trim();

                        }
                        if (rdgiam.Checked == true)
                        {
                            for (i = 0; i < n; i++)

                                for (int j = i + 1; j <= n; j++)

                                    if (a[i] < a[j])
                                    {
                                        tmp = a[i];
                                        a[i] = a[j];
                                        a[j] = tmp;
                                    }
                            s = " ";
                            for (i = 0; i <= n; i++)

                                s = s + " " + a[i].ToString();
                            txtxuat.Text = s.Trim();
                        }

                    }

        private void btxoa_Click(object sender, EventArgs e)
        {
            txtnhap.Text = "";
            txtxuat.Text = "";
        }
                }
            }
  
Video Hướng Dẫn

No comments:

Post a Comment