使*运算符返回两个矢量的点积(dot product)。
注意:两个矢量点积定义为两个矢量的大小与两个矢量之间夹角余弦的乘积。
代码一直出错,有没有好心人帮忙看看问题在哪里!!
这是代码:
using System;
namespace Ch12Ex01
{
public class Vector
{
public double? R = null;
public double? Theta = null;
public double? ThetaRadians
{
// Convert degrees to radians.
get
{
return (Theta * Math.PI / 180.0);
}
}
public Vector(double? r, double? theta)
{
// Normalize.
if (r < 0)
{
r = -r;
theta += 180;
}
theta = theta % 360;
// Assign fields.
R = r;
Theta = theta;
}
public static double? operator *(Vector op1, Vector op2)
{
try
{
double angleDiff = (double)(op2.ThetaRadians.Value - op1.ThetaRadians.Value);
return op1.R.Value * op2.R.Value * Math.Cos(angleDiff);
}
catch
{
return null;
}
}
public static Vector operator -(Vector op1) => new Vector(-op1.R, op1.Theta);
public static Vector operator -(Vector op1, Vector op2) => op1 + (-op2);
public override string ToString()
{
// Get string representation of coordinates.
string rString = R.HasValue ? R.ToString() : "null";
string thetaString = Theta.HasValue ? Theta.ToString() : "null";
// Return (r, theta) string.
return string.Format($"({rString}, {thetaString})" );
}
}
class Program
{
static void Main(string[] args)
{
Vector v1 = GetVector("vector1");
Vector v2 = GetVector("vector1");
Console.WriteLine($"{v1} * {v2} = {v1 * v2}");
Console.WriteLine($"{v1} - {v2} = {v1 - v2}");
Console.ReadKey();
}
static Vector GetVector(string name)
{
Console.WriteLine($"Input {name} magnitude:" );
double? r = GetNullableDouble();
Console.WriteLine($"Input {name} angle (in degrees):");
double? theta = GetNullableDouble();
return new Vector(r, theta);
}
static double? GetNullableDouble()
{
double? result;
string userInput = Console.ReadLine();
try
{
result = double.Parse(userInput);
}
catch
{
result = null;
}
return result;
}
}
}
注意:两个矢量点积定义为两个矢量的大小与两个矢量之间夹角余弦的乘积。
代码一直出错,有没有好心人帮忙看看问题在哪里!!
这是代码:
using System;
namespace Ch12Ex01
{
public class Vector
{
public double? R = null;
public double? Theta = null;
public double? ThetaRadians
{
// Convert degrees to radians.
get
{
return (Theta * Math.PI / 180.0);
}
}
public Vector(double? r, double? theta)
{
// Normalize.
if (r < 0)
{
r = -r;
theta += 180;
}
theta = theta % 360;
// Assign fields.
R = r;
Theta = theta;
}
public static double? operator *(Vector op1, Vector op2)
{
try
{
double angleDiff = (double)(op2.ThetaRadians.Value - op1.ThetaRadians.Value);
return op1.R.Value * op2.R.Value * Math.Cos(angleDiff);
}
catch
{
return null;
}
}
public static Vector operator -(Vector op1) => new Vector(-op1.R, op1.Theta);
public static Vector operator -(Vector op1, Vector op2) => op1 + (-op2);
public override string ToString()
{
// Get string representation of coordinates.
string rString = R.HasValue ? R.ToString() : "null";
string thetaString = Theta.HasValue ? Theta.ToString() : "null";
// Return (r, theta) string.
return string.Format($"({rString}, {thetaString})" );
}
}
class Program
{
static void Main(string[] args)
{
Vector v1 = GetVector("vector1");
Vector v2 = GetVector("vector1");
Console.WriteLine($"{v1} * {v2} = {v1 * v2}");
Console.WriteLine($"{v1} - {v2} = {v1 - v2}");
Console.ReadKey();
}
static Vector GetVector(string name)
{
Console.WriteLine($"Input {name} magnitude:" );
double? r = GetNullableDouble();
Console.WriteLine($"Input {name} angle (in degrees):");
double? theta = GetNullableDouble();
return new Vector(r, theta);
}
static double? GetNullableDouble()
{
double? result;
string userInput = Console.ReadLine();
try
{
result = double.Parse(userInput);
}
catch
{
result = null;
}
return result;
}
}
}