C#分子和分母平方然后乘以2
C# numerator & denominator is squared when multiply 2 times
当我乘以 2 次时,分子和分母是平方的
分数文件:
using System;
namespace MathLib.Fraction
{
public struct Fraction
{
// Private Member
static int prNumer;
static int prDenom;
// Constructor
public Fraction(int _numer, int _denom)
{
if(_denom == 0)
{
throw new ArgumentException("Denominator cannot be Zero.", "_denom");
}
prNumer = _numer;
prDenom = _denom;
}
// Other
public int GetNumer()
{
return prNumer;
}
public int GetDenom()
{
return prDenom;
}
public override string ToString()
{
return prNumer + "/" + prDenom;
}
// Operator Overloading
public static Fraction operator *(Fraction left, Fraction right)
{
int numer = left.GetNumer() * right.GetNumer();
int denom = left.GetDenom() * right.GetDenom();
return new Fraction(numer, denom);
}
}
}
主文件:
using System;
using MathLib;
using MathLib.Fraction;
namespace MathLibTest
{
class Program
{
static void Main(string[] args)
{
Fraction a = new Fraction(3, 5);
Fraction b = new Fraction(6, 8);
Console.WriteLine(a * b); // 36/64
Console.WriteLine(a * b); // 1296/4096 what I expected is: 36/64 again
}
}
}
我想要完成的事情:只是制作一个我将来会用到的简单数学库。我不是在开玩笑,我以后会用到它的:)
我尝试的方法:重写整个分数class,30 分钟搜索错误和仍然错误
// 抱歉我的英语不好:)
static int prNumer;
static int prDenom;
删除static
; static
意味着只有 一个值在所有实例中共享 (除非它是 [ThreadStatic]
)。你想要一个 per-instance 值,所以:删除 static
.
我还建议标记字段 readonly
,如果您使用的是最新的 C# 版本,则使用 readonly struct
。
完整推荐版本,供参考:
public readonly struct Fraction : IEquatable<Fraction>
{
public int Numerator { get; }
public int Denominator { get; }
public Fraction(int numererator, int denominator)
{
if (denominator == 0)
{
throw new ArgumentException("Denominator cannot be Zero.", nameof(denominator));
}
// TODO: lowest common denominator
Numerator = numererator;
Denominator = denominator;
}
public override string ToString() => $"{Numerator}/{Denominator}";
public static Fraction operator *(Fraction left, Fraction right)
=> new Fraction(left.Numerator * right.Numerator, left.Denominator * right.Denominator);
public override int GetHashCode() => HashCode.Combine(Numerator, Denominator);
public override bool Equals([NotNullWhen(true)] object obj)
=> obj is Fraction other && Equals(other);
public bool Equals(Fraction other)
=> Numerator == other.Numerator && Denominator == other.Denominator;
}
当我乘以 2 次时,分子和分母是平方的
分数文件:
using System;
namespace MathLib.Fraction
{
public struct Fraction
{
// Private Member
static int prNumer;
static int prDenom;
// Constructor
public Fraction(int _numer, int _denom)
{
if(_denom == 0)
{
throw new ArgumentException("Denominator cannot be Zero.", "_denom");
}
prNumer = _numer;
prDenom = _denom;
}
// Other
public int GetNumer()
{
return prNumer;
}
public int GetDenom()
{
return prDenom;
}
public override string ToString()
{
return prNumer + "/" + prDenom;
}
// Operator Overloading
public static Fraction operator *(Fraction left, Fraction right)
{
int numer = left.GetNumer() * right.GetNumer();
int denom = left.GetDenom() * right.GetDenom();
return new Fraction(numer, denom);
}
}
}
主文件:
using System;
using MathLib;
using MathLib.Fraction;
namespace MathLibTest
{
class Program
{
static void Main(string[] args)
{
Fraction a = new Fraction(3, 5);
Fraction b = new Fraction(6, 8);
Console.WriteLine(a * b); // 36/64
Console.WriteLine(a * b); // 1296/4096 what I expected is: 36/64 again
}
}
}
我想要完成的事情:只是制作一个我将来会用到的简单数学库。我不是在开玩笑,我以后会用到它的:)
我尝试的方法:重写整个分数class,30 分钟搜索错误和仍然错误
// 抱歉我的英语不好:)
static int prNumer;
static int prDenom;
删除static
; static
意味着只有 一个值在所有实例中共享 (除非它是 [ThreadStatic]
)。你想要一个 per-instance 值,所以:删除 static
.
我还建议标记字段 readonly
,如果您使用的是最新的 C# 版本,则使用 readonly struct
。
完整推荐版本,供参考:
public readonly struct Fraction : IEquatable<Fraction>
{
public int Numerator { get; }
public int Denominator { get; }
public Fraction(int numererator, int denominator)
{
if (denominator == 0)
{
throw new ArgumentException("Denominator cannot be Zero.", nameof(denominator));
}
// TODO: lowest common denominator
Numerator = numererator;
Denominator = denominator;
}
public override string ToString() => $"{Numerator}/{Denominator}";
public static Fraction operator *(Fraction left, Fraction right)
=> new Fraction(left.Numerator * right.Numerator, left.Denominator * right.Denominator);
public override int GetHashCode() => HashCode.Combine(Numerator, Denominator);
public override bool Equals([NotNullWhen(true)] object obj)
=> obj is Fraction other && Equals(other);
public bool Equals(Fraction other)
=> Numerator == other.Numerator && Denominator == other.Denominator;
}