设计一个 Windows 应用程序,随机生成0~100 之间的 10个数字,并通过委托实现升序或降序排列。

  1. 切换到源代码编辑视图,在窗体类中定义委托、排序的方法,代码如下:

  int[] a = new int[10];

        delegate bool Compare(int x, int y);  //声明委托类型

        void SortArray(Compare compare)   //委托形参

        {

            for (int i = 1; i < a.Length; i++)

                for (int j = 0; j < a.Length-1; j++)

                    if (compare(a[j], a[j+1]))   //使用委托调用方法,以比较大小

                    {

                        int t = a[j];a[j] = a[j+1];a[j+1] = t;

                    }

        }

        bool Ascending(int x, int y)        //比较x是否大于y

        {

            return x > y;

        }

        bool Desecding(int x, int y)       //比较x是否小于y

        {

            return x < y;

        }

        void display()            //输出数组

        {

            txtTarget.Text = "";

            foreach (int i in a)

            {

                txtTarget.Text += i + "\r\n";

            }

        }

  1. 编写按钮的 Click 事件方法,代码如下:

private void btnCreateArray_Click(object sender, EventArgs e)

        {

            txtSource.Text = "";

            txtTarget.Text = "";

            Random r = new Random();

            for (int i = 0; i < a.Length; i++)

            {

                a[i] = r.Next(100);  //取0~100间的随机数

                txtSource.Text += a[i] + "\r\n";

            }

        }

        private void btnAscSort_Click(object sender, EventArgs e)

        {

            SortArray(new Compare(Ascending)); 实参是新创建的委托对象

            display();

        }

        private void btnDescSort_Click(object sender, EventArgs e)

        {

            SortArray(new Compare(Desecding)); //实参是新创建的委托对象

            display();

      具体代码如下:

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 form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int[] a = new int[10];
        delegate bool Compare(int x, int y);  //声明委托类型
        void SortArray(Compare compare)   //委托形参
        {
            for (int i = 1; i < a.Length; i++)
                for (int j = 0; j < a.Length - 1; j++)
                    if (compare(a[j], a[j + 1]))   //使用委托调用方法,以比较大小
                    {
                        int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
                    }
        }
        bool Ascending(int x, int y)        //比较x是否大于y
        {
            return x > y;
        }
        bool Desecding(int x, int y)       //比较x是否小于y
        {
            return x < y;
        }
        void display()            //输出数组
        {
            txtTarget.Text = "";
            foreach (int i in a)
            {
                txtTarget.Text += i + "\r\n";
            }
        }

        private void btnCreateArray_Click(object sender, EventArgs e)
        {
            txtSource.Text = "";
            txtTarget.Text = "";
            Random r = new Random();
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = r.Next(100);  //取0~100间的随机数
                txtSource.Text += a[i] + "\r\n";

            }
        }

        private void btnAscSort_Click(object sender, EventArgs e)
        {
            SortArray(new Compare(Ascending)); 实参是新创建的委托对象
            display();

        }

        private void btnDescSort_Click(object sender, EventArgs e)
        {
            SortArray(new Compare(Desecding)); //实参是新创建的委托对象
            display();

        }
    }
}

运行效果图如下:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空心木偶☜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值