C#读取TXT文件放入数组,并统计数据,绘制折线图

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;
using System.IO;
using System.Collections;
using System.Windows.Forms.DataVisualization.Charting;

namespace ShuJuCeshi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {
        int row = 0;//行
        int col = 0;//lie
        FileStream fs;
        string path = "E:\\C##CX\\C#TT\\TEXT2.txt";
        textBox3.Text = "当前文件夹位置是:" + "\r\n" + path;
        fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(fs);
        while (!sr.EndOfStream)
        {
            string[] arr = sr.ReadLine().Split(' ');

            col = arr.Length;
            textBox5.Text = "\r\n";

            int[] a = new int[col / 2];
            double[] temp = new double[col];
            double[] ai = new double[col / 2];
            double[] aj = new double[col / 2];
            for (int x = 0; x < col; x++)
            {
                temp[x] = Convert.ToDouble(arr[x]);
                if (x % 2 == 0)
                {
                    for (int i = 0; i < col / 2; i++)
                    {
                        ai[i] = temp[x];
                        textBox1.Text = textBox1.Text + ' ' + ai[i] + "\r\n";
                        //int l =  textBox1.Text.Length; 
                    }
                }
                else
                {
                    for (int i = 0; i < col / 2; i++)
                    {
                        aj[i] = temp[x];
                        textBox4.Text = textBox4.Text + ' ' + aj[i] + "\r\n";
                    }
                }
            }
            int count = this.textBox1.Lines.GetUpperBound(0);
            for (int d = 1; d < count + 1; d++)
            {
                string rr = Convert.ToString(d);
                textBox5.Text = textBox5.Text + rr + "\r\n";
            }
        }
        row++;
        Console.WriteLine("\n");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        int count = this.textBox1.Lines.GetUpperBound(0);
        double[] temp = new double[count];
        string[] str = new string[textBox1.Lines.Length];
        string[] str1 = new string[textBox4.Lines.Length];
        for (int i = 0; i < textBox1.Lines.Length; i++)
        {
            str[i] = textBox1.Lines[i];
        }
        for (int i = 0; i < textBox4.Lines.Length; i++)
        {
            str1[i] = textBox4.Lines[i];
        }
        int Len = textBox1.Lines.Length;
        int Len1 = textBox4.Lines.Length;
        for (int pointIndex = 0; pointIndex < Len; pointIndex++)
        {
            chart1.Series["数据统计折线图"].Points.AddXY(str[pointIndex], str1[pointIndex]);
        }
    }

    private void button4_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
        textBox4.Text = "";
        textBox5.Text = "";
        textBox3.Text = "文件位置:" + "\r\n" + "未加载数据文件";
        textBox2.Text ="未加载数据文件" +"\r\n" 
            +  "测量数据点数:" +"\r\n" 
            + "测量数据与平均值最大偏离:"+ "\r\n"
            + "测量数据与平均值最小偏离:" + "\r\n"
            + "最大偏离测量节点数序号:"+ "\r\n"  
            + "最小偏离测量节点数序号:" ;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox4.Text == " ")
            MessageBox.Show("未打开任何数据处理文件,请选择打开一个数据处理文件!", "信息提示!", MessageBoxButtons.OK);
        else
        {
            int count = this.textBox1.Lines.GetUpperBound(0);
            double sum = 0;
            double aver = 0;
            double mtemp;
            double stemp;
            int mindex=0;
            int sindex=0;
            double ll;
            double l;
            double ma;
            double su;
            double[] p = new double[count];
            double[] a = new double[count];
            double[] s = new double[count];
            double[] m = new double[count];
            double[] array = new double[count];
            string[] lines = textBox4.Text.Split('\n');
            for (int h = 0; h < count; h++)
            {
                p[h] = Convert.ToDouble(lines[h]);//存放文本框中的数据
                sum = sum + p[h];
            }
            aver = sum / count;
            string straver= aver.ToString("f5");//fN 保留N位,四舍五入
            for (int h = 0; h < count; h++)
            {
                // s[h] = (p[h] - aver);
                a[h] = Math.Abs(p[h] - aver);//获取文本框数据与平均值之差的数组,主要围绕数组a进行操作
                array[h] = Math.Abs(p[h] - aver);//获取文本框数据与平均值之差的绝对值数组
            }
            ma = array.Max();
            string strma = ma.ToString("f5");
            su = array.Min();
            string strsu= su.ToString("f5");
            mtemp = a[0];
            stemp = a[0];
            for (int i = 0; i < a.Length; i++)
            {
                if (mtemp < a[i])
                {
                    mtemp = a[i];
                    mindex = i + 1;
                }
            }
            for (int j = 0; j < a.Length; j++)
            {
                if (stemp >= a[j])
                {
                    stemp = a[j];
                    sindex = j + 1;
                }
            }
            textBox2.Text = "测量数据平均值:" + straver;
            textBox2.Text = textBox2.Text + "\r\n" + "测量数据点数:" + count;
            textBox2.Text = textBox2.Text + "\r\n" + "测量数据最大偏离平均值:" + strma;
            textBox2.Text = textBox2.Text + "\r\n" + "测量数据最小偏离平均值:"  +strsu;
            textBox2.Text = textBox2.Text + "\r\n" + "最大偏离测量节点数序号:" + mindex;
            textBox2.Text = textBox2.Text + "\r\n" + "最小偏离测量节点数序号:" + sindex;
        }
    }
    //
}

}

全部评论

相关推荐

最近和朋友聊天,她说了句让我震惊的话:"我发现我连周末点外卖都开始'最优解'了,一定要赶在高峰期前下单,不然就觉得自己亏了。"这不就是典型的"班味入侵"吗?工作思维已经渗透到生活的方方面面。
小型域名服务器:啊?我一直都这样啊?我还以为是我爱贪小便宜呢?每次去实验室都得接一杯免费的开水回去,出门都得规划一下最短路径,在宿舍就吃南边的食堂,在实验室就吃北边的食堂,快递只有顺路的时候才取。
点赞 评论 收藏
分享
头像
10-22 19:18
上海大学 后端
jopajhhdjwnqk:水印都叠杀人书了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务