比如我这个程序,需要用户输入两次,第一次输入的格式为“1.5,5”(中间为英文半角逗号),我要将其转换成两个double类型的数,即1.5和5;第二次输入的格式为“9.0,6 2,5.9 -1,-7.8”(坐标之间有英文半角空格),要转换为6个double类型的数,即9.0、6、2、5.9、-1、-7.8。
    我要写一个类(类名为Input),将这两种转换情况都包含在内。请问该怎么写?是不是应该使用Split方法?Split方法在这里该如何使用? (我是一个初学者,最好多给一些例子)
还有,我这个程序还有那些不足之处也请各位大大指点。(程序功能:根据输入的一个点坐标和一个三角形的坐标判断该点与三角形的位置关系,平面)
    PS:飘过有分~谢谢。我写的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Position position = new Position(0,0,0,1,-1,-1,1,-1);
            /*
            Console.Write("请输入点的坐标,中间用空格分隔:");
            Console.ReadLine();
            // */
            position.GetPosition();
            string result;
            position.OutPosition(out result);
            Console.WriteLine("点与三角形的位置关系为:{0}",result);
        }
    }
    class Input
    {
        private string[] str;
        private int n;
        private char[] ch;
        private double[] ex;
        public Input(int n)
        {
            this.str = new string[n];
            this.n = n;
            this.ch = new char[1] { ' ' };
            this.ex = new double[n];
        }
        public void Exchange(string strSour)
        {
            this.str = strSour.Split(this.ch,this.n-1);
            for (int i = 0; i < this.ex.Length; i++)
            {
                try
                {
                    this.ex[i] = Convert.ToDouble(this.str[i]);
                }
                catch (Exception)
                {
                    Console.WriteLine("强制转换异常!");
                    return;
                }
            }
        }
        public void OutExchange(out double[] ex)
        {
            ex = this.ex;
        }
    }   // 输入转换(未完成)
    class Position
    {
        #region 字段
        private Point point;
        private Triangle triangle;
        private string position = "点与该三角形关系未知";
        #endregion
        public Position(params double[] x)
        {
            for (int i = 0; i < x.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        this.point.x = x[i];
                        break;
                    case 1:
                        this.point.y = x[i];
                        break;
                    case 2:
                        this.triangle.a.x = x[i];
                        break;
                    case 3:
                        this.triangle.a.y = x[i];
                        break;
                    case 4:
                        this.triangle.b.x = x[i];
                        break;
                    case 5:
                        this.triangle.b.y = x[i];
                        break;
                    case 6:
                        this.triangle.c.x = x[i];
                        break;
                    case 7:
                        this.triangle.c.y = x[i];
                        break;
                    default:
                        return;
                }
            }
        }    // 初始化
        public void OutPosition(out string position)
        {
            position = this.position;
        }  // 输出位置关系
        public double Area(Point a, Point b, Point c) // 求三角形面积
        {
            return Abs( (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y) );
        }
        public double Abs(double x)
        {
            if (x < 0)
            {
                return -x;
            }
            else
            {
                return x;
            }
        }   // 求绝对值
        public bool IsOneZero(double[] x)
        {
            foreach (double item in x)
            {
                if (item == 0.0)
                {
                    return true;
                }
            }
            return false;
        }    // 数组中是否有0
        public void GetPosition()
        {
            double areaTriangle = Area(triangle.a, triangle.b, triangle.c);
            if (areaTriangle == 0)
            {
                this.position = "三角形不存在";
                return;
            }            double[] area = new double[3];
            area[0] = Area(point, triangle.b, triangle.c);
            area[1] = Area(triangle.a, point, triangle.c);
            area[2] = Area(triangle.a, triangle.b, point);            double areaSum = 0.0;
            foreach (double a in area)
            {
                areaSum += a;
            }            if (areaTriangle != areaSum)
            {
                this.position = "点在三角形外";
                return;
            }
            else if (IsOneZero(area) == true)
            {
                this.position = "点在三角形边上";
                return;
            }
            else
            {
                this.position = "点在三角形内部";
                return;
            }
        }
    }  // 位置关系
    public struct Point
    {
        public double x;    // 横坐标
        public double y;    // 纵坐标
    }  // 平面点坐标
    public struct Triangle
    {
        public Point a;
        public Point b;
        public Point c;
    } // 平面三角形三个顶点坐标
}

解决方案 »

  1.   

    代码太长,没看string test = "9.0,6 2,5.9 -1,-7.8";
    string[] po = test.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
    List<double> list = new List<double>();
    foreach (string s in po)
    {
        list.Add(double.Parse(s));
    }
      

  2.   

                Console.Write("请输入点的坐标,中间用空格分隔:");            string input = Console.ReadLine(); 
                string[] tmpArr = input .Split(',');
                foreach (string s in tmpArr )
                    //replace this line with your initialization code here :)
                    Console.WriteLine(double.Parse(s));
      

  3.   

    没有注释,看不太懂……
    test
      

  4.   

          
    //用户输入字符串, 同时保存在"input"里(如"1.5,1.8")
    string input = Console.ReadLine(); 
    //如果分隔符为",",Split返回由“,”分开的字符串数组( => "1.5" , "1.8"))
    string[] tmpArr = input .Split(','); 
    //把得到数组的每个元素的每个元素都转化成double ( => 1.5 , 1.8))
    foreach (string s in tmpArr ) 
    Console.WriteLine(double.Parse(s)); //做你的事情
      

  5.   

    楼上的答案挺好,谢谢。
    不过还有一个问题呀,第二次输入的格式为“9.0,6 2,5.9 -1,-7.8”(坐标之间有英文半角空格),要转换为6个double类型的数,即9.0、6、2、5.9、-1、-7.8。 
    要求这两次拆分使用类似函数的重载做到,也就是给一个方法一个字符串,让它自动识别时哪种情况,然后自动进行拆分。
      

  6.   

    不过还有一个问题呀,第二次输入的格式为“9.0,6 2,5.9 -1,-7.8”(坐标之间有英文半角空格),要转换为6个double类型的数,即9.0、6、2、5.9、-1、-7.8。 
    要求这两次拆分使用类似函数的重载做到,也就是给一个方法一个字符串,让它自动识别时哪种情况,然后自动进行拆分。不用重载,而且就算的重载也需要参数不同,而你只能有一种参数(String s)。
    你可以这样,把s.Trim().IndexOf(“ ”)>0 作为判断条件,即如果为true,表示输入的是多个坐标,如果为false,表示输入的是一个座标,然后做相应的处理即可。
      

  7.   


    1楼的代码很好啊,你自己用cmd执行下看看。public static void getstr()
            {
                string test = "9.0,6 2,5.9 -1,-7.8";
                string[] po = test.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);            
                foreach (string s in po)
                {                
                    Console.WriteLine(s);
                }
            }
      

  8.   


    根本不用重载,因为函数大输入是一样的,都是string,没必要重载.