标签:
C# 原来也可以进行向量运算,这里解决了一个为时已久的疑惑。
operator
struct Vector { public double x, y, z; public Vector(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public Vector(Vector rhs) { this.x = rhs.x; this.y = rhs.y; this.z = rhs.z; } public override string ToString() { return "(" + x + "," + y + "," + z + ")"; } public static Vector operator +(Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; } public static Vector operator *(Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.x *= rhs.x; result.y *= rhs.y; result.z *= rhs.z; return result; } }
Vector vect1, vect2, vect3,vect4; vect1 = new Vector(3.0, 3.0, 1.0); vect2 = new Vector(2.0, -4.0, -4.0); vect3 = vect1 + vect2; vect4 = vect1 * vect2; Console.WriteLine("vect1=" + vect1.ToString()); Console.WriteLine("vect2=" + vect2.ToString()); Console.WriteLine("vect3=" + vect3.ToString()); Console.WriteLine("vect4=" + vect4.ToString());
输出:
参考:http://www.cnblogs.com/LilianChen/archive/2013/03/15/2961901.html
c# implicit explicit关键字 : http://www.cnblogs.com/chengxiaohui/articles/1914190.html
C# checked与unchecked用法: http://www.cnblogs.com/limingluzhu/archive/2011/11/07/2239130.html
发现疑惑,疑虑终究会有解决,只是在那个不经意的时候,未来某一刻。所以要相信,也要坚信,时间终究会给我们答案。
标签:
原文地址:http://www.cnblogs.com/annabook/p/4775183.html