C#简单的向量用法实例教程_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

C#简单的向量用法实例教程

发布时间:2026-01-17  |  点击率:

本文以实例讲述了C#简单的向量用法,主要包括重载运算符>:以向量长度判断是否为真、重载运算符!=、<、<=等,具体实现代码如下:

using System;
class Vector
{
 private double XVector;
 private double YVector;
 //构造函数
 public Vector(double x, double y )
 {
 XVector = x;
 YVector = y;
 }
 //获取向量的长度
 public double GetLength( )
 {
 double Length = Math.Sqrt( XVector*XVector + YVector*YVector );
 return Length;
 }
 //重载运算符==
 public static bool operator == ( Vector a, Vector b )
 {
 return ( (a.XVector == b.XVector) && (a.YVector == b.YVector) );
 }
 //重载运算符!=
 public static bool operator != ( Vector a, Vector b )
 {
 return !( a == b );
 }
 //重载运算符>:以向量长度判断是否为真
 public static bool operator > ( Vector a, Vector b )
 {
 return a.GetLength( ) > b.GetLength( );
 }
 //重载运算符<
 public static bool operator < ( Vector a, Vector b )
 {
 return a.GetLength( ) < b.GetLength( );
 }
 //重载运算符>=
 public static bool operator >= ( Vector a, Vector b )
 {
 return ( a == b ) || ( a > b );
 }
 //重载运算符<=
 public static bool operator <= ( Vector a, Vector b )
 {
 return ( a == b ) || ( a < b );
 }
}
class Test
{
 static public void Main( )
 {
 Vector vector1 = new Vector( 3, 4 );
 Vector vector2 = new Vector( 0, 5 );
 Vector vector3 = new Vector( 2, 2 );
 Console.WriteLine("向量1为( 3, 4 ) \t 向量2为( 0, 5 ) \t 向量3为( 2, 2 )");
 Console.WriteLine("向量1 == 向量2 为:{0}", vector1 == vector2 );
 Console.WriteLine("向量1 != 向量2 为:{0}", vector1 != vector2 );
 Console.WriteLine("向量1 > 向量3 为:{0}", vector1 > vector3 );
 Console.WriteLine("向量2 < 向量3 为:{0}", vector2 < vector3 );
 Console.WriteLine("向量1 >= 向量2 为:{0}", vector1 != vector2 );
 Console.WriteLine("向量1 <= 向量2 为:{0}", vector1 != vector2 );
 }
}

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598