C#访问指定的子数组

时间:2017-04-30 12:38:18

标签: c# arrays

我的数据文件包含一个双数字数组(这些数字是道路中心线的偏移量及其以米为单位的点高度),每组都有一条中心线每20米增加一次:

10250.000

-100.00 660.698 -050.00 665.789 -025.00 667.269 0.00 664.101 025.00 666.225 050.00 668.987 100.00 664.361

10270.000

-100.00 667.772 -050.00 663.907 -025.00 668.065 0.00 668.101 025.00 667.225 050.00 665.899 100.00 663.365

10290.000

-100.00 663.698 -050.00 665.989 -025.00 676.999 0.00 665.105 025.00 676.225 050.00 677.797 100.00 665.371 。 。

10330.000

-100.00 665.594 -050.00 662.985 -025.00 667.762 0.00 667.106 025.00 667.823 050.00 668.087 100.00 669.357

我想编写一个C#程序,它可以读取和访问指定的中心行号及其子阵列偏移量&现场高度。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            new CenterLine(FILENAME);
        }
    }
    public class CenterLine
    {
        public static List<CenterLine> centerLines = new List<CenterLine>();

        public double centerLine { get; set; }
        public List<double> heights { get; set; }

        enum State
        {
            GET_CENTER_LINE,
            GET_HEIGHTS
        }
        public CenterLine() { }
        public CenterLine(string filename)
        {
            StreamReader reader = new StreamReader(filename);

            string inputLine = "";

            State state = State.GET_CENTER_LINE;

            CenterLine newCenterLine = null;
            while ((inputLine = reader.ReadLine()) != null)
            {
                inputLine = inputLine.Trim();
                if (inputLine.Length > 0)
                {
                    switch (state)
                    {
                        case State.GET_CENTER_LINE :
                            newCenterLine = new CenterLine();
                            centerLines.Add(newCenterLine);
                            newCenterLine.centerLine = double.Parse(inputLine);

                            state = State.GET_HEIGHTS;
                            break;

                        case State.GET_HEIGHTS :
                            newCenterLine.heights = inputLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                .Select(x => double.Parse(x)).ToList();
                            state = State.GET_CENTER_LINE;
                            break;
                    }
                }
            }
        }
    }
}