创建一个名为Rational的类,进行分数运算。编写一个程序测试该类。要求使用C#语言
答案:1 悬赏:30 手机版
解决时间 2021-02-01 05:59
- 提问者网友:暗中人
- 2021-01-31 22:39
创建一个名为Rational的类,进行分数运算。编写一个程序测试该类。要求使用C#语言
最佳答案
- 五星知识达人网友:行雁书
- 2021-01-31 23:45
public class Rational {
public Rational() : this(0, 0) { }
public Rational(int numeraror, int denominator) {
if (numeraror == 0 || denominator == 0) {
this.numeraror = 0;
this.denominator = 0;
} else {
int n = numeraror;
int d = denominator;
if (numeraror > denominator) n = d + (d = n) * 0;
int t = d / n;
if (t != numeraror && t != denominator) {
denominator = denominator / t;
numeraror = numeraror / t;
}
this.numeraror = numeraror;
this.denominator = denominator;
}
}
private readonly int numeraror;
private readonly int denominator;
public int Numeraror {
get { return numeraror; }
}
public int Denominator {
get { return denominator; }
}
public override bool Equals(object obj) {
return obj is Rational ? (obj as Rational).denominator == denominator && (obj as Rational).numeraror == numeraror : false;
}
public override int GetHashCode() {
return ((int)denominator ^ (int)(denominator >> 32)) ^ ((int)numeraror ^ (int)(numeraror >> 32));
}
public override string ToString() {
return denominator == 0 ? "NaN" : string.Format("{0}/{1}", numeraror, denominator);
}
public string ToFloatString() {
return denominator == 0 ? "NaN" : string.Format("{0:f5}", ((float)numeraror) / denominator);
}
public Rational Add(Rational r) {
return new Rational(this.Numeraror + r.Numeraror, this.Denominator + r.Denominator);
}
public Rational Subtract(Rational r) {
return new Rational(this.Numeraror - r.Numeraror, this.Denominator - r.Denominator);
}
public Rational Multiply(Rational r) {
return new Rational(this.Numeraror * r.Numeraror, this.Denominator * r.Denominator);
}
public Rational Divide(Rational r) {
return new Rational(r.Numeraror == 0 ? 0 : this.Numeraror / r.Numeraror, r.Denominator == 0 ? 0 : this.Denominator / r.Denominator);
}
public static Rational operator +(Rational r1, Rational r2) {
return r1.Add(r2);
}
public static Rational operator -(Rational r1, Rational r2) {
return r1.Subtract(r2);
}
public static Rational operator *(Rational r1, Rational r2) {
return r1.Multiply(r2);
}
public static Rational operator /(Rational r1, Rational r2) {
return r1.Divide(r2);
}
public static bool operator ==(Rational r1, Rational r2) {
return r1.Equals(r2);
}
public static bool operator !=(Rational r1, Rational r2) {
return !r1.Equals(r2);
}
public static bool operator >(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator > ((float)r2.Numeraror) / r2.Denominator;
}
public static bool operator <(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator < ((float)r2.Numeraror) / r2.Denominator;
}
public static bool operator >=(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator >= ((float)r2.Numeraror) / r2.Denominator;
}
public static bool operator <=(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator <= ((float)r2.Numeraror) / r2.Denominator;
}
}参考资料:CodeMonk
public Rational() : this(0, 0) { }
public Rational(int numeraror, int denominator) {
if (numeraror == 0 || denominator == 0) {
this.numeraror = 0;
this.denominator = 0;
} else {
int n = numeraror;
int d = denominator;
if (numeraror > denominator) n = d + (d = n) * 0;
int t = d / n;
if (t != numeraror && t != denominator) {
denominator = denominator / t;
numeraror = numeraror / t;
}
this.numeraror = numeraror;
this.denominator = denominator;
}
}
private readonly int numeraror;
private readonly int denominator;
public int Numeraror {
get { return numeraror; }
}
public int Denominator {
get { return denominator; }
}
public override bool Equals(object obj) {
return obj is Rational ? (obj as Rational).denominator == denominator && (obj as Rational).numeraror == numeraror : false;
}
public override int GetHashCode() {
return ((int)denominator ^ (int)(denominator >> 32)) ^ ((int)numeraror ^ (int)(numeraror >> 32));
}
public override string ToString() {
return denominator == 0 ? "NaN" : string.Format("{0}/{1}", numeraror, denominator);
}
public string ToFloatString() {
return denominator == 0 ? "NaN" : string.Format("{0:f5}", ((float)numeraror) / denominator);
}
public Rational Add(Rational r) {
return new Rational(this.Numeraror + r.Numeraror, this.Denominator + r.Denominator);
}
public Rational Subtract(Rational r) {
return new Rational(this.Numeraror - r.Numeraror, this.Denominator - r.Denominator);
}
public Rational Multiply(Rational r) {
return new Rational(this.Numeraror * r.Numeraror, this.Denominator * r.Denominator);
}
public Rational Divide(Rational r) {
return new Rational(r.Numeraror == 0 ? 0 : this.Numeraror / r.Numeraror, r.Denominator == 0 ? 0 : this.Denominator / r.Denominator);
}
public static Rational operator +(Rational r1, Rational r2) {
return r1.Add(r2);
}
public static Rational operator -(Rational r1, Rational r2) {
return r1.Subtract(r2);
}
public static Rational operator *(Rational r1, Rational r2) {
return r1.Multiply(r2);
}
public static Rational operator /(Rational r1, Rational r2) {
return r1.Divide(r2);
}
public static bool operator ==(Rational r1, Rational r2) {
return r1.Equals(r2);
}
public static bool operator !=(Rational r1, Rational r2) {
return !r1.Equals(r2);
}
public static bool operator >(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator > ((float)r2.Numeraror) / r2.Denominator;
}
public static bool operator <(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator < ((float)r2.Numeraror) / r2.Denominator;
}
public static bool operator >=(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator >= ((float)r2.Numeraror) / r2.Denominator;
}
public static bool operator <=(Rational r1, Rational r2) {
return ((float)r1.Numeraror) / r1.Denominator <= ((float)r2.Numeraror) / r2.Denominator;
}
}参考资料:CodeMonk
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯