当我尝试输出方法调用时,我得到一个空白屏幕,当我按任意键时,我显示了命名空间,然后控制台关闭。有人看到我在这里失踪了吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace programming_2
{
class Driver
{
static void Main(string[] args)
{
fraction frac = new fraction(); // we'll get 0/1
frac = new fraction(1, 5); // we'll get 1/5
// frac = new fraction(25); // we'll get 25/1
//frac = new fraction(9.25); // we'll get 37/4
//frac = new fraction("6.25"); // we'll get 25/4
//frac = new fraction(System.Console.ReadLine());
Console.Write(frac);
// displays the current value of frac object;
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
}
}
}
分数等级
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace programming_assignment_2
{
class fraction : ICloneable, IEnumerable<fraction>, IEnumerator<fraction>, IComparer<fraction>
{
private int num, den;
// Constructors
public fraction()
{
Initialize(0, 1);
}
public fraction(int num)
{
CheckMinValue(num);
Initialize(num, 1);
}
public fraction(fraction f)
{
Initialize(f.num, f.den);
}
public fraction(int num, int den)
{
CheckDenominatorZero(den);
CheckMinValue(num);
CheckMinValue(den);
fraction f = new fraction((decimal)num, (decimal)den);
Initialize(f.num, f.den);
}
private static void CheckMinValue(int n)
{
if (n == int.MinValue)
throw new OverflowException();
}
private void Initialize(int num, int den)
{
this.num = num;
this.den = den;
}
private fraction(decimal r1, decimal r2)
{
decimal gcd = GCD(Math.Abs(r1), Math.Abs(r2));
int num = (int)(r1 / gcd);
int den = (int)(r2 / gcd);
CheckMinValue(num);
CheckMinValue(den);
if (r2 < 0)
{
num = -num;
den = -den;
}
this.num = num;
this.den = den;
}
public int Numerator
{
get { return this.num; }
}
public int Denominator
{
get { return this.den; }
}
// Overloaded Operators ( +-*/^ )
// Add
public static fraction operator +(fraction a, fraction b)
{
decimal d1 = (decimal)a.num * b.den + (decimal)b.num * a.den;
decimal d2 = (decimal)a.den * b.den;
return new fraction(d1, d2);
}
public static fraction operator +(fraction a, int b)
{
return a + new fraction(b);
}
public static fraction operator +(int a, fraction b)
{
return new fraction(a) + b;
}
// Substract
public static fraction operator -(fraction a, fraction b)
{
decimal r1 = (decimal)a.num * b.den - (decimal)b.num * a.den;
decimal r2 = (decimal)a.den * b.den;
return new fraction(r1, r2);
}
public static fraction operator -(fraction a, int b)
{
return a - new fraction(b);
}
public static fraction operator -(int a, fraction b)
{
return new fraction(a) - b;
}
// Multiply
public static fraction operator *(fraction a, fraction b)
{
decimal r1 = (decimal)a.num * b.num;
decimal r2 = (decimal)a.den * b.den;
return new fraction(r1, r2);
}
public static fraction operator *(fraction a, int b)
{
return a * new fraction(b);
}
public static fraction operator *(int a, fraction b)
{
return new fraction(a) * b;
}
// Divide
public static fraction operator /(fraction a, fraction b)
{
decimal r1 = (decimal)a.num * b.den;
decimal r2 = (decimal)a.den * b.num;
if (r2 == 0)
throw new DivideByZeroException();
else
return new fraction(r1, r2);
}
public static fraction operator /(fraction a, int b)
{
return a / new fraction(b);
}
public static fraction operator /(int a, fraction b)
{
return new fraction(a) / b;
}
// Exponents
public static fraction operator ^(fraction a, int n)
{
return new fraction((decimal)Math.Pow(a.num, n), (decimal)Math.Pow(a.den, n));
}
// Comparison operators
public static bool operator ==(fraction a, fraction b)
{
return (decimal)a.num * b.den == (decimal)b.num * a.den;
}
public static bool operator !=(fraction a, fraction b)
{
return (!(a == b));
}
public static bool operator >(fraction a, fraction b)
{
return (decimal)a.num * b.den > (decimal)b.num * a.den;
}
public static bool operator >=(fraction a, fraction b)
{
return (!(a < b));
}
public static bool operator <(fraction a, fraction b)
{
return (decimal)a.num * b.den < (decimal)b.num * a.den;
}
public static bool operator <=(fraction a, fraction b)
{
return (!(a > b));
}
public static decimal GCD(decimal a, decimal b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
private static void CheckDenominatorZero(decimal den)
{
if (den == 0)
throw new ArithmeticException("The denominator can't be zero!!");
}
}
}
答案 0 :(得分:1)
什么是分数类?它中是否有一个ToString()方法可以打印出来?
这适合我。
static void Main(string[] args)
{
var frac = new fraction(1,5);
Console.WriteLine(frac);
Console.WriteLine("Press any key to continue..");
Console.ReadKey();
}
public class fraction
{
public int nom { get; set; }
public int denom { get; set; }
public fraction(int n, int d)
{
nom = n;
denom = d;
}
public override string ToString()
{
return string.Format("{0}\\{1}", nom, denom);
}
}
答案 1 :(得分:0)
在你的代码中看起来你有一个无限循环:
public fraction(int num, int den)
{
CheckDenominatorZero(den);
CheckMinValue(num);
CheckMinValue(den);
// You are creating a new instance in the creation of a new instance
fraction f = new fraction((decimal)num, (decimal)den);
Initialize(f.num, f.den);
}
试试这个:
// Remove this line:
fraction f = new fraction((decimal)num, (decimal)den);
// And change the next line to this:
Initialize(num, den);
更改后:
public fraction(int num, int den)
{
CheckDenominatorZero(den);
CheckMinValue(num);
CheckMinValue(den);
Initialize(num, den);
}
另外,正如其他人所说,你应该覆盖ToString,这样你的控制台输出看起来不错:
public override string ToString()
{
return string.Format("{0}\\{1}", this.num, this.den);
}