(1)保护字段存放向量的各个分量值,使用一维数组存放。
(2)公有属性Dim获取向量的维数。
(3)默认的公有构造函数,产生一个零向量,带参数构造函数,使用可变数组参数。
(4)公有方法Dot求两向量点乘的结果, Distance求两个向量之间的距离, Angle求两个向量之间的夹角, Normal对向量进行单位化, Length求向量的长度
(5)重载运算符“+", “.”对两个向量进行加减操作,重载运算符“=”判断两个向量是否是同向的。
(6)公有函数display以如下形式输出向量中的各个分量(1,2..,5)
定义一个向量类Vector,有以下成员
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录
悬赏问题
- ¥15 乌班图ip地址配置及远程SSH
- ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
- ¥15 PSPICE制作一个加法器
- ¥15 javaweb项目无法正常跳转
- ¥15 VMBox虚拟机无法访问
- ¥15 skd显示找不到头文件
- ¥15 机器视觉中图片中长度与真实长度的关系
- ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
- ¥15 java 的protected权限 ,问题在注释里
- ¥15 这个是哪里有问题啊?