m0_62718592 2021-11-20 09:58 采纳率: 100%
浏览 209
已结题

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

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

  • 写回答

2条回答 默认 最新

  • 广大菜鸟 2021-11-20 15:11
    关注
    
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月29日
  • 已采纳回答 11月21日
  • 创建了问题 11月20日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改