C#编程问题,程序无错误但没结果 private void Form1_Load(object sender, EventArgs e) 25
Fractiona=newFraction(1,3);Fractionb=newFraction(3,-12);textBox1.Text+=("a=");a.Show(...
Fraction a = new Fraction(1, 3);
Fraction b = new Fraction(3, -12);
textBox1.Text += ("a= ");
a.Show(textBox1);
textBox1.Text += ("\r\nb= ");
b.Show(textBox1);
Fraction c = new Fraction();
c = a + b;textBox1.Text += ("\r\na+b= ");c.Show(textBox1);c = a * b;textBox1.Text += ("\r\na*b= ");c.Show(textBox1);
}
}
public class Fraction
{
int m, n; public Fraction() { }
public Fraction(int num, int den)
{
int t = gcd(num, den); m = num / t;n = den / t;
}
int gcd(int x, int y)
{
int r;
while (true)
{
if (x % y == 0) { r = n; break; }
else
{
r = x % y;x = y;y = r;
}
}
return y;
}
public static Fraction operator +(Fraction a, Fraction b)
{
return new Fraction(a.m * b.n + b.m * a.n, a.n * b.n);
}
public static Fraction operator *(Fraction a, Fraction b)
{
return new Fraction(a.m * b.m , a.n * b.n);
}
public void Show(TextBox t)
{
if (m * n > 0)
if (n != 1)
t.Text += (m + "/" + n);
else
t.Text += (m);
else
if (n != 1) t.Text += ("-" + System.Math.Abs(m) + "/" + System.Math.Abs(n));
else
t.Text += ("-" + System.Math.Abs(m));
}
}; 展开
Fraction b = new Fraction(3, -12);
textBox1.Text += ("a= ");
a.Show(textBox1);
textBox1.Text += ("\r\nb= ");
b.Show(textBox1);
Fraction c = new Fraction();
c = a + b;textBox1.Text += ("\r\na+b= ");c.Show(textBox1);c = a * b;textBox1.Text += ("\r\na*b= ");c.Show(textBox1);
}
}
public class Fraction
{
int m, n; public Fraction() { }
public Fraction(int num, int den)
{
int t = gcd(num, den); m = num / t;n = den / t;
}
int gcd(int x, int y)
{
int r;
while (true)
{
if (x % y == 0) { r = n; break; }
else
{
r = x % y;x = y;y = r;
}
}
return y;
}
public static Fraction operator +(Fraction a, Fraction b)
{
return new Fraction(a.m * b.n + b.m * a.n, a.n * b.n);
}
public static Fraction operator *(Fraction a, Fraction b)
{
return new Fraction(a.m * b.m , a.n * b.n);
}
public void Show(TextBox t)
{
if (m * n > 0)
if (n != 1)
t.Text += (m + "/" + n);
else
t.Text += (m);
else
if (n != 1) t.Text += ("-" + System.Math.Abs(m) + "/" + System.Math.Abs(n));
else
t.Text += ("-" + System.Math.Abs(m));
}
}; 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询