定义一个向量类Vector,有以下成员

(1)保护字段存放向量的各个分量值,使用一维数组存放。
(2)公有属性Dim获取向量的维数。
(3)默认的公有构造函数,产生一个零向量,带参数构造函数,使用可变数组参数。
(4)公有方法Dot求两向量点乘的结果, Distance求两个向量之间的距离, Angle求两个向量之间的夹角, Normal对向量进行单位化, Length求向量的长度
(5)重载运算符“+", “.”对两个向量进行加减操作,重载运算符“=”判断两个向量是否是同向的。
(6)公有函数display以如下形式输出向量中的各个分量(1,2..,5)


using System;
using System.Collections.Generic;

namespace ConsoleApp2
{
    class Vector
    {
        private double[] list;
        private int dim;

        public int Dim { get => dim; set => dim = value; }
        protected double[] List { get => list; set => list = value; }
        public Vector()
        {
            list = new double[1];           
            dim = 1;
        }
        public Vector(int length) {         
            list = new double[length];
            for (int i = 0; i < length; i++) list[i] = 0;
            dim = length;
        }

        public void SetVectorList(int index,double value)
        {
            if (index < 0)
            {
                Console.WriteLine("This is a wrong Dim!");
                return;
            }else if (index >= dim)
            {
                //数组扩容
                Array.Resize<double>(ref list, index + 1);
                this.dim = index+1;
            }
             list[index] = value;
        }
        // 点乘
        public double Dot(Vector v)
        {
            int d = v.Dim;
            if (d == Dim)
            {
                double value=0;
                for(int i = 0; i < v.dim; i++)
                {
                    value += v.list[i] * this.list[i];
                }
                return value;
            }
            else
            {
                Console.WriteLine("They have wrong Dim!");                
            }
            return 0;
        }

        //距离
        public double Distance(Vector v)
        {
            int d = v.Dim;
            if (d == Dim)
            {
                double value = 0,tmpDouble=0;
                for (int i = 0; i < d; i++)
                {
                    tmpDouble = v.list[i] - this.list[i];
                    value += tmpDouble * tmpDouble;
                }
                return Math.Sqrt(value);
            }
            else
            {
                Console.WriteLine("They have wrong Dim!");
                return 0;
            }
        }
       
        //夹角
        public double Angle(Vector v)
        {
            double dotValue = this.Dot(v);
            double aLen = v.Length();
            double bLen = this.Length();
            return Math.Acos(dotValue/(aLen*bLen));
        }       
        //Normal
        public void Normal()
        {
            double Len = this.Length();
            if (Len > 0)
            {
                for(int i = 0; i < dim; i++)
                {
                    this.list[i] /= Len;
                }
            }
        }
        // Length
        public double Length()
        {
            if (this.Dim <= 0) 
                return 0;
            double value=0;
            for(int i = 0; i < dim; i++)
            {
                value += list[i] * list[i];
            }
            return Math.Sqrt(value);
        }

        public override string ToString()
        {
            if (dim <= 0) 
                return "()";
            String str = "(" + list[0].ToString();
            for(int i = 1; i < dim; i++)
            {
                str += ',' + list[i].ToString();
            }
            str += ')';
            return str;
        }
        public void display()
        {
            Console.WriteLine(this.ToString());
        }
        //重载运算符“+", “-”对两个向量进行加减操作,重载运算符“=”判断两个向量是否是同向的
        public static Vector operator +(Vector a, Vector b)
        {
            int d1 = a.Dim, d2=b.Dim;
            if (d1 == d2)
            {
                for (int i = 0; i < d1; i++)
                {
                    a.list[i] += b.list[i];
                }
            }
            else
            {
                Console.WriteLine("They have wrong Dim!");
            }
            return a;
        }

        public static Vector operator -(Vector a, Vector b)
        {
            int d1 = a.Dim, d2 = b.Dim;
            if (d1 == d2)
            {
                for (int i = 0; i < d1; i++)
                {
                    a.list[i] -= b.list[i];
                }
            }
            else
            {
                Console.WriteLine("They have wrong Dim!");
            }
            return a;
        }
        public static bool operator == (Vector a, Vector b)
        {
            double angle = a.Angle(b);
            return angle == 0;
        }
        public static bool operator !=(Vector a, Vector b)
        {
            double angle = a.Angle(b);
            return angle != 0;
        }
        public override bool Equals(object obj)
        {
            return this.Equals(obj);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Vector v = new Vector();
            v.SetVectorList(0, 1);
            v.SetVectorList(1, 0);
            v.SetVectorList(3, 3);
            Console.WriteLine(v);
            Vector v1 = new Vector(3);
            v1.SetVectorList(0, 1);
            v1.SetVectorList(1, 0);
            v1.SetVectorList(2, 5);
            Console.WriteLine(v1);
        }
    }
}

img

厉害了无参构造函数不给维数还要生成零向量,等个大佬