私は自分のコードを投稿する方法を間違えました。それはちょっと怠惰で、私のためにすべてをやりたいだけです。以下のコードでの演算子のオーバーロードと、このコードがメインプログラムで何をしているのかを学びたいです。
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();
}
}
このコードは、ベクトルの加算、減算、乗算、および等式/不等式の演算子をオーバーライドすることにより、ベクトルのクラスを実装します。1つのオペランド(実数または整数)の演算を考慮に入れてください。操作で次元ベクトルが一致する場合は、例外をスローします。
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);
}
}
}