注册 登录
编程论坛 C# 论坛

求大神帮忙指导一下这段代码,感激不尽。

cjn05290026 发布于 2017-03-14 10:22, 1814 次点击
想要达到的结果是将文本文件中的坐标读到数组中,然后根据坐标计算两点间的距离,菜鸟一个,求帮助。。。
namespace wenbenwenjian
{
    public struct Point
    {
        public double X;
        public double Y;
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Point[] pts = new Point[10];
            List<Point> points = new List<Point>();
            Point pt1 = new Point();

            StreamReader sr = new StreamReader("11改.txt");
            string content = sr.ReadToEnd();
            string[] str = content.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine("第 {0} 行: {1}", i + 1, str[i]);
                Console.ReadLine();
            }

            pt1.X = Convert.ToDouble(str[1]);
            pt1.Y = Convert.ToDouble(str[2]);
            points.Add(pt1);

            double dist = (points[1].Y - points[0].Y) * (points[1].Y - points[0].Y) + (points[1].X - points[0].X) * (points[1].X - points[0].X);
            double RESULT = Math.Sqrt(Math.Abs(dist));
            Console.WriteLine(RESULT);
            Console.ReadKey();
        }
    }
}
1 回复
#2
yhlvht2017-03-14 21:42
不太清楚你txt里面是怎么排列的
但是points只add了一个点进去,还差一个点没有放进去
1