c#程序实验

文件数据的排序和删除

这周用c#写一周的实验,计算器和超市选址都写的挺顺利的,第二个实验遇到了一点点坎坷,主要是文件的操作不熟悉

1.文件的删除

listView1.Items.RemoveAt(this.listView1.SelectedItems[0].Index);

刚开始啥都不知道,这是我们实验指导书上的,他只能在显示删除,文件里并没有删除

图片描述

图片描述

图片描述

所以就想在文件里怎么删除,问了问黄庭祥,又在网上搜了半天

发现只能是重新按行写入文件,遇到想要删除的那行,跳过,不写入,实现文件中的删除

        private void delete(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count != 0)
            {
                int x;

                // 获取删除的行号
                x = this.listView1.SelectedItems[0].Index;

                // 在显示中删除
                listView1.Items.RemoveAt(this.listView1.SelectedItems[0].Index);


                int counter = 0;
                String line;
                List<String> lines = new List<String>();

                // 按行读取文件  
                System.IO.StreamReader file =
                    new System.IO.StreamReader(@"e:\data.txt");

                // 跳过删除的行,重新写入文件
                while ((line = file.ReadLine()) != null)
                {
                    if (counter != x)
                    {
                        lines.Add(line);
                    }
                    counter++;
                }

                file.Close();
                File.WriteAllLines(@"e:\data.txt", lines, UTF8Encoding.Default);
            }

        }

图片描述

图片描述

此时文件中的数据也就被删除了

2.数据的排序

数据的排序是我这次实验中耗时最长的,中间遇到了许多问题就不多讲了

实现方法:

按行读取文件,以空格分为n个string数组,取其中的总分存入两个数组当中。

图片描述

匹配获得每一行的排名,如果改行有排名,将其排名改为新的排名,若没有则加上排名,写入文件

        private void order(object sender, EventArgs e)
        {
            string line;
            int line_num = 0;           // 记录文件行数
            double[] order_temp = new double[10];
            double[] order = new double[10];

            // 给数组赋初值
            for (int j = 0; j < 10; j++)
            {
                order_temp[j] = 0;
                order[j] = 0;
            }

            System.IO.StreamReader file1 =
                new System.IO.StreamReader(@"e:\data.txt");
            while ((line = file1.ReadLine()) != null)
            {

                string[] array = Regex.Split(line, " ");
                order_temp[line_num] = Convert.ToDouble(array[5]);
                order[line_num] = Convert.ToDouble(array[5]);
                line_num++;

            }

            file1.Close();
            double t;
            bool tag = true;

            // 排序
            for (int i = line_num - 1; i >= 1 && tag; i--)
            {
                tag = false;
                for (int j = 0; j < i; j++) // for循环进行冒泡排序
                {
                    if (order_temp[j] < order_temp[j + 1])
                    {
                        t = order_temp[j];
                        order_temp[j] = order_temp[j + 1];
                        order_temp[j + 1] = t;
                        tag = true;
                    }
                }
            }

            // 将文件数据暂时存放与lines中
            List<String> lines = new List<String>();
            string line1;
            // Read the file and display it line by line.  
            System.IO.StreamReader file =
                new System.IO.StreamReader(@"e:\data.txt");
            int k = 0, status = 0;

            while ((line1 = file.ReadLine()) != null)
            {
                for (int i = 0; i < line_num; i++)
                {
                    if (order[k] == order_temp[i])
                    {
                        string line2;
                        line2 = line1;

                        // 若有排名,则将其排名修改为之后的排名
                        string[] array = Regex.Split(line2, " ");
                        if (array.Length < 8)
                        {
                            string[] records = Regex.Split(line2, "\r\n");


                            line2 = records[0] + " " + (i + 1).ToString();
                            lines.Add(line2);
                        }

                        // 无排名,则在其后面加入排名
                        else
                        {
                            string line3 = "";
                            for (int m = 0; m < 7; m++)
                            {
                                line3 = line3 + array[m] + " ";
                            }
                            array[7] = (i + 1).ToString();
                            line3 = line3 + array[7];
                            lines.Add(line3);
                        }

                        k++;
                        i = 0;

                    }

                    if (status != k)
                    {
                        status = k;
                        break;
                    }
                }
            }

            file.Close();
            File.WriteAllLines(@"e:\data.txt", lines, UTF8Encoding.Default);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值