我在如何发布我的代码时犯了一个错误,这表明我很懒,我只想为我完成所有事情,我想了解下面代码中的运算符重载以及这段代码在主程序中的作用
static public explicit operator Int32(Vector v)
{
return v.Length;
}
public override string ToString()
{
String res = "<";
for (int i = 0; i < elements.Length; i++)
res += " " + elements[i];
res += " >";
return res;
}
public override bool Equals(Object v)
{
return (this == (Vector)v);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
该代码通过重写向量的加法、减法、乘法和相等/不等运算符来实现一类向量。考虑到其中一个操作数的操作——实数或整数。在操作中维度向量重合的情况下抛出异常。
namespace ConsoleApplication3
{
public class Vector
{
private Double[] elements;
public Vector()
{
this.elements = null;
}
public Vector(Int32 size)
{
if (size < 0) throw new Exception("Invalid vetors size");
elements = new Double[size];
}
public Vector(params Double[] elements)
{
this.elements = elements;
}
public Vector(Vector v)
{
this.elements = v.elements;
}
public int Length
{
get
{
if (elements == null) return 0;
return elements.Length;
}
}
public Double this[int index]
{
get
{
return elements[index];
}
set
{
elements[index] = value;
}
}
static public Vector operator+(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
throw new Exception("size of vectors are different");
Vector res = new Vector(v1.Length);
for (int i = 0; i < res.Length; i++)
res[i] = v1[i] + v2[i];
return res;
}
static public Vector operator+(Vector v, Double d)
{
Vector res = new Vector(v.Length);
for (int i = 0; i < v.Length; i++ )
res[i] = v[i] + d;
return res;
}
static public Vector operator +(Double d, Vector v)
{
return (v + d);
}
static public Vector operator -(Vector v, Double d)
{
Vector res = new Vector(v.Length);
for (int i = 0; i < v.Length; i++)
res[i] = v[i] - d;
return res;
}
static public Vector operator -(Double d, Vector v)
{
Vector res = new Vector(v.Length);
for (int i = 0; i < v.Length; i++)
res[i] = d - v[i];
return res;
}
static public Vector operator *(Vector v, Double d)
{
Vector res = new Vector(v.Length);
for (int i = 0; i < v.Length; i++)
res[i] = v[i] * d;
return res;
}
static public Vector operator *(Double d, Vector v)
{
return (v * d);
}
static public Vector operator -(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
throw new Exception("size of vectors are different");
Vector res = new Vector(v1.Length);
for (int i = 0; i < res.Length; i++)
res[i] = v1[i] - v2[i];
return res;
}
static public Vector operator *(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
throw new Exception("size of vectors are different");
Vector res = new Vector(v1.Length);
for (int i = 0; i < res.Length; i++)
res[i] = v1[i] * v2[i];
return res;
}
static public Boolean operator ==(Vector v1, Vector v2)
{
if (v1.Length != v2.Length)
return false;
for (int i = 0; i < v1.Length; i++)
if(v1[i] != v2[i]) return false;
return true;
}
static public Boolean operator !=(Vector v1, Vector v2)
{
return (!(v1 == v2));
}
static public explicit operator Int32(Vector v)
{
return v.Length;
}
public override string ToString()
{
String res = "<";
for (int i = 0; i < elements.Length; i++)
res += " " + elements[i];
res += " >";
return res;
}
public override bool Equals(Object v)
{
return (this == (Vector)v);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
Vector v1 = new Vector(new Double[]{1, 2, 3, 4});
Vector v2 = new Vector(2, 3, 4, 5);
Console.WriteLine(v1 + " + " + v2 + " = " + (v1 + v2));
Console.WriteLine(v1 + " - " + v2 + " = " + (v1 - v2));
Console.WriteLine(v1 + " * " + v2 + " = " + (v1 * v2));
Console.WriteLine(v1 + " * " + 7 + " = " + (v1 * 7));
Console.ReadKey(true);
}
}
}