永发信息网

创建一个名为Rational的类,进行分数运算。编写一个程序测试该类。要求使用C#语言

答案:1  悬赏:30  手机版
解决时间 2021-02-01 05:59
创建一个名为Rational的类,进行分数运算。编写一个程序测试该类。要求使用C#语言
最佳答案
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
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
学生需要手机的理由
万通网络地址在什么地方,想过去办事
浙江科技学院2014年美术类文化分是多少
名字:春燕能编成对联码?
被誉为大众情人的水果是:A.葡萄B.樱桃C.苹果D
银样蜡枪头的意思是什么啊?知道的请说下!
语文《魏与赵攻韩,韩告急于齐>问题
网聊时一个已婚男人,看过我的照片后,总说想
科慧网吧地址在什么地方,我要处理点事
【康熙字典11画的字】康熙字典里13画和4画的
馨怡超世纪网络空间地址在哪,我要去那里办事
我去参加婚礼穿一条浅色的裙子和一条鹅黄色的
1冻13单元九楼一三号是什么意思
林更新真的有一个姐姐吗
点文的意思是什么啊?知道的请说下!
推荐资讯
顺丰快递能到黄冈市团风县但店镇吗?
京东的Apple EarPods MD827FE/A耳机为什么比
有36块糖,分给小红和小明,小红的糖是小明5倍,
从闽清到张家港的动车
一斤45元那三两多少钱
有声读物的意思是什么啊?知道的请说下!
了解一下拥有上海市户口的好处
馆劳的意思是什么啊?知道的请说下!
穿越火线7月占卜中,抽到3次天龙,他是算一把
我在当地证券交易所开通了创业板,可以在网上
【关于团结的谚语】关于团结方面的谚语有什么
枯山水景观中常用的树有哪些?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?