Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 3 của 3
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    hỏi về bài toán sắp xếp

    các bác giải thích cho e các lệnh được đánh dấu cái.e mới bắt đầu học mong được các bác tận tình giúp đỡ.(ở đoạn cuối cuối bài)
    đây là một bài toán nhập 1 vài thông tin vào textbox sau đó chuyển sang hiển thị bên listbox.rồi sắp xếp các thông tin được hiển thị đó.

    Mã:
    using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data; namespace sort_0706{    /// <summary>    /// Summary description for Form1.    /// </summary>    public class Form1 : System.Windows.Forms.Form    {        private System.Windows.Forms.TextBox txt1;        private System.Windows.Forms.ListBox l1;        private System.Windows.Forms.Button btn1;        private System.Windows.Forms.Button btn2;        private System.Windows.Forms.Button btn3;        /// <summary>        /// Required designer variable.        /// </summary>        private System.ComponentModel.Container components = null;         public Form1()        {            //            // Required for Windows Form Designer support            //            InitializeComponent();             //            // TODO: Add any constructor code after InitializeComponent call            //        }         /// <summary>        /// Clean up any resources being used.        /// </summary>        protected override void Dispose( bool disposing )        {            if( disposing )            {                if (components != null)                 {                    components.Dispose();                }            }            base.Dispose( disposing );        }         #region Windows Form Designer generated code        /// <summary>        /// Required method for Designer support - do not modify        /// the contents of this method with the code editor.        /// </summary>        private void InitializeComponent()        {            this.txt1 = new System.Windows.Forms.TextBox();            this.l1 = new System.Windows.Forms.ListBox();            this.btn1 = new System.Windows.Forms.Button();            this.btn2 = new System.Windows.Forms.Button();            this.btn3 = new System.Windows.Forms.Button();            this.SuspendLayout();            //             // txt1            //             this.txt1.Location = new System.Drawing.Point(8, 16);            this.txt1.Name = "txt1";            this.txt1.TabIndex = 0;            this.txt1.Text = "";            //             // l1            //             this.l1.Location = new System.Drawing.Point(208, 0);            this.l1.Name = "l1";            this.l1.Size = new System.Drawing.Size(168, 95);            this.l1.TabIndex = 1;            //             // btn1            //             this.btn1.Location = new System.Drawing.Point(120, 16);            this.btn1.Name = "btn1";            this.btn1.TabIndex = 2;            this.btn1.Text = ">>";            this.btn1.Click += new System.EventHandler(this.btn1_Click);            //             // btn2            //             this.btn2.Location = new System.Drawing.Point(16, 56);            this.btn2.Name = "btn2";            this.btn2.TabIndex = 3;            this.btn2.Text = "Ascending";            this.btn2.Click += new System.EventHandler(this.btn2_Click);            //             // btn3            //             this.btn3.Location = new System.Drawing.Point(112, 56);            this.btn3.Name = "btn3";            this.btn3.TabIndex = 4;            this.btn3.Text = "Descending";            //             // Form1            //             this.AcceptButton = this.btn1;            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);            this.ClientSize = new System.Drawing.Size(408, 112);            this.Controls.Add(this.btn3);            this.Controls.Add(this.btn2);            this.Controls.Add(this.btn1);            this.Controls.Add(this.l1);            this.Controls.Add(this.txt1);            this.Name = "Form1";            this.Text = "Form1";            this.ResumeLayout(false);         }        #endregion         /// <summary>        /// The main entry point for the application.        /// </summary>        [STAThread]        static void Main()         {            Application.Run(new Form1());        }         private void btn1_Click(object sender, System.EventArgs e)        {            l1.Items.Add(txt1.Text);            txt1.Text="";            txt1.Focus();         }bắt đầu từ đây cho đến hết :        private void btn2_Click(object sender, System.EventArgs e)        {            int n,i;            n=l1.Items.Count;// gets the number of items in collection            string [] a= new string[n];            for (i=0;i<=n-1;i++)            {                a[i]=(string)l1.Items[i];            }            Array.Sort(a);            l1.Items.Clear();            for(i=0;i<=n-1;i++)            {                l1.Items.Add(a[i]);            }         }    }}
    Vui lòng để code vào tag code. Đọc Nội quy để biết thêm chi tiết

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã:
    private void btn2_Click(object sender, System.EventArgs e)        {            int n,i;            n=l1.Items.Count;//Lấy số phần tử trong listBox cần được sắp xếp            string [] a= new string[n];//tạo 1 mảng a             //cho duyệt trong pham vi mảng n phần tử            //lưu các giá trị của listBox vào trong mảng a vừa tạo            for (i=0;i<=n-1;i++)            {                a[i]=(string)l1.Items[i];            }             //Sắp xềp mảng a vừa tạo            //Ở đây là sắp xếp trên mảng a, không dính dáng gì tới listBox            Array.Sort(a);            //Xóa trống listBox để lát nữa lưu lại mảng đã được sắp xếp            l1.Items.Clear();                        //Thể hiện mảng đả được sorted lên listBox            for(i=0;i<=n-1;i++)            {                l1.Items.Add(a[i]);            }         }

  3. #3
    bạn lưu ý dòng này l1.Items.Add(a[i]); trước khi duyệt thì bạn phải đánh dấu chỉ số phần tử cuối cùng trươc khi sắp xếp , sau đó khi duyệt thì duyệt từ chỉ số vừa dánh dấu thì bạn sẽ được List chuỗi đã sắp xếp
    * không thì bạn có thể tạo một List khác, sau đó add a[i] vào List đó, khi dùng thì dễ sử dụng hơn
    Thân!!

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •