C#程序设计经典教程(第三版)实验三

1.修改上机实验 2 的第 3 个实验任务将输人的 n个数字,通过 for 语句排序并输出注意,不允许使用 Array.Sort()方法排序。

界面布局

代码如下:

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 实训3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double[] arr = new double[5];
        int i = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            arr[i] = Convert.ToDouble(textBox1.Text);
            lblshow1.Text += arr[i]+" ";
            i++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Array.Sort(arr);
            double temp;
           for(int i = 0; i < arr.Length; i++)
            {
                for(int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[i] > arr[j])
                    {
                        temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
            lblshow2.Text = "\n排序后的序列:";
            lblshow2.Text += arr[0] +" "+ arr[1] +" "+ arr[2] +" "+ arr[3] +" "+ arr[4];
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblshow1.Text = "排序前的序列:";
        }
    }
}


2.设计一个 Windows 应用程序,实现如下功能。
(1)输入学生姓名和考试成绩并保存到结构体数组中
(2) 使用 foreach 语句求最高分并输出对应的姓名。

界面布局

代码如下

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 实训3
{
    public partial class Form2 : Form
    {
        struct Student
        {
            public String stuName;
            public Double stuGrade;
            
        }
        Student[] students = new Student[100];
        int size = 0;
        Double maxGrade = 0;
        public Form2()
        {
            InitializeComponent();

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (size >= 100)
            {
                label3.Text = "学生人员已满";
            }
            else
            {
                students[size].stuName = textBox1.Text;
                students[size].stuGrade = Convert.ToDouble(textBox2.Text);
                size++;
                label3.Text = "添加成功";
            }
            label7.Text += "姓名:" + textBox1.Text + "  分数:" + textBox2.Text + "。  ";
            if (size % 3 == 0)
            {
                label7.Text += "\n\n";
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int index = 0;
            int finalindex = 0;
            foreach (Student stu in students)
            {
                index++;
                if (stu.stuGrade > maxGrade)
                {
                    maxGrade = stu.stuGrade;
                    finalindex = index;
                }
            }
            label5.Text ="学生姓名:"+ students[finalindex - 1].stuName + "  分数:" + students[finalindex - 1].stuGrade;
        }
    }
}


3.设计一个 Windows 应用程序,输人一行字符,检索是否存在重复的二字词汇(由两个字符组成的字符),输出重复的次数,效果如图 3-19 所示。

界面布局:

代码如下:

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 实训3
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = 0;
            string[] words = new string[10];
            int[] times = new int[10];
            for(int i = 0; i < textBox1.Text.Length - 2; i++)
            {
                bool isSame = false;
                string source = textBox1.Text.Substring(i, 2);
                int j = i + 2;
                while (j < textBox1.Text.Length - 2)
                {
                    string target = textBox1.Text.Substring(j, 2);
                    if (source == target)
                    {
                        times[n]++;
                        if (Array.IndexOf(words, target) == -1)
                        {
                            isSame = true;
                            words[n] = target;
                        }
                    }
                    j++;
              
                }
                if (isSame) n++;
            }
            label2.Text = String.Format("一共有{0}个重复的词汇!\n\n其中,", n);
            for(int i = 0; i < 10; i++)
            {
                if (!String.IsNullOrEmpty(words[i]))
                {
                    label2.Text+=String.Format("'{0}'"+"重复{1}次, ",words[i],times[i]+1);
                }
            }
        }
    }
}

若转载,请保留文章出处和原作者,感谢。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值