tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
90
Task/Quaternion-type/C-sharp/quaternion-type-1.cs
Normal file
90
Task/Quaternion-type/C-sharp/quaternion-type-1.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
using System;
|
||||
|
||||
struct Quaternion : IEquatable<Quaternion>
|
||||
{
|
||||
public readonly double A, B, C, D;
|
||||
|
||||
public Quaternion(double a, double b, double c, double d)
|
||||
{
|
||||
this.A = a;
|
||||
this.B = b;
|
||||
this.C = c;
|
||||
this.D = d;
|
||||
}
|
||||
|
||||
public double Norm()
|
||||
{
|
||||
return Math.Sqrt(A * A + B * B + C * C + D * D);
|
||||
}
|
||||
|
||||
public static Quaternion operator -(Quaternion q)
|
||||
{
|
||||
return new Quaternion(-q.A, -q.B, -q.C, -q.D);
|
||||
}
|
||||
|
||||
public Quaternion Conjugate()
|
||||
{
|
||||
return new Quaternion(A, -B, -C, -D);
|
||||
}
|
||||
|
||||
// implicit conversion takes care of real*quaternion and real+quaternion
|
||||
public static implicit operator Quaternion(double d)
|
||||
{
|
||||
return new Quaternion(d, 0, 0, 0);
|
||||
}
|
||||
|
||||
public static Quaternion operator +(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
return new Quaternion(q1.A + q2.A, q1.B + q2.B, q1.C + q2.C, q1.D + q2.D);
|
||||
}
|
||||
|
||||
public static Quaternion operator *(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
return new Quaternion(
|
||||
q1.A * q2.A - q1.B * q2.B - q1.C * q2.C - q1.D * q2.D,
|
||||
q1.A * q2.B + q1.B * q2.A + q1.C * q2.D - q1.D * q2.C,
|
||||
q1.A * q2.C - q1.B * q2.D + q1.C * q2.A + q1.D * q2.B,
|
||||
q1.A * q2.D + q1.B * q2.C - q1.C * q2.B + q1.D * q2.A);
|
||||
}
|
||||
|
||||
public static bool operator ==(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
return q1.A == q2.A && q1.B == q2.B && q1.C == q2.C && q1.D == q2.D;
|
||||
}
|
||||
|
||||
public static bool operator !=(Quaternion q1, Quaternion q2)
|
||||
{
|
||||
return !(q1 == q2);
|
||||
}
|
||||
|
||||
#region Object Members
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is Quaternion)
|
||||
return Equals((Quaternion)obj);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return A.GetHashCode() ^ B.GetHashCode() ^ C.GetHashCode() ^ D.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("Q({0}, {1}, {2}, {3})", A, B, C, D);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEquatable<Quaternion> Members
|
||||
|
||||
public bool Equals(Quaternion other)
|
||||
{
|
||||
return other == this;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
34
Task/Quaternion-type/C-sharp/quaternion-type-2.cs
Normal file
34
Task/Quaternion-type/C-sharp/quaternion-type-2.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
|
||||
static class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Quaternion q = new Quaternion(1, 2, 3, 4);
|
||||
Quaternion q1 = new Quaternion(2, 3, 4, 5);
|
||||
Quaternion q2 = new Quaternion(3, 4, 5, 6);
|
||||
double r = 7;
|
||||
|
||||
Console.WriteLine("q = {0}", q);
|
||||
Console.WriteLine("q1 = {0}", q1);
|
||||
Console.WriteLine("q2 = {0}", q2);
|
||||
Console.WriteLine("r = {0}", r);
|
||||
|
||||
Console.WriteLine("q.Norm() = {0}", q.Norm());
|
||||
Console.WriteLine("q1.Norm() = {0}", q1.Norm());
|
||||
Console.WriteLine("q2.Norm() = {0}", q2.Norm());
|
||||
|
||||
Console.WriteLine("-q = {0}", -q);
|
||||
Console.WriteLine("q.Conjugate() = {0}", q.Conjugate());
|
||||
|
||||
Console.WriteLine("q + r = {0}", q + r);
|
||||
Console.WriteLine("q1 + q2 = {0}", q1 + q2);
|
||||
Console.WriteLine("q2 + q1 = {0}", q2 + q1);
|
||||
|
||||
Console.WriteLine("q * r = {0}", q * r);
|
||||
Console.WriteLine("q1 * q2 = {0}", q1 * q2);
|
||||
Console.WriteLine("q2 * q1 = {0}", q2 * q1);
|
||||
|
||||
Console.WriteLine("q1*q2 {0} q2*q1", (q1 * q2) == (q2 * q1) ? "==" : "!=");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue