# 救命啊,高手们这怎么做啊,马上要交的,在线等

huangchengjin630 2006-10-23 10:13:14
1、完成一个复数类、提供复数的加减运算（也可以完成乘除运算），并重写ToString方法。
...全文
111 5 打赏 收藏 举报

5 条回复

• 打赏
• 举报

• 打赏
• 举报

class Number
{
int x;
int y;
Number(int x,int y)
{
this.x=x;
this.y=y;
}
}

public Number Operator+(Number number1,Number number2)
{
int nx=number1.x+number2.x;
int ny=number1.y+number2.y;
return new Number(nx,ny)
}
cctv6012cn 2006-10-23
• 打赏
• 举报

• 打赏
• 举报

UP UP STUDY, DAY DAY UP.
dyw31415926 2006-10-23
• 打赏
• 举报

1,添加一个Number.cs (类)
public class Number
{

private int x;
private int y;
public int X
{
get
{
return x;
}
set
{
this.x = value;
}
}

public int Y
{
get
{
return y;
}
set
{
this.y = value;
}
}
public Number(int x, int y)
{
this.x = x;
this.y = y;
}

}
-----------------------------------------------------------------------------

class BetterNumber:Number
{
private int x;
private int y;
public BetterNumber (int x, int y):base(x,y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
if (this.y < 0)
{
return this.x.ToString() + this.y.ToString() + "i";
}
else
{
return this.x.ToString() + "+" + this.y.ToString() + "i";
}
}

public static BetterNumber operator +(BetterNumber number1, BetterNumber number2)
{
int nx = number1.X + number2.X;
int ny = number1.Y + number2.Y;
return new BetterNumber(nx, ny);
}
}
------------------------------------------------------------------------

private void button1_Click(object sender, EventArgs e)
{
BetterNumber num1 = new BetterNumber(1, 2);
BetterNumber num2 = new BetterNumber(3, -4);
BetterNumber num3 = num1 + num2;

MessageBox.Show(num3.ToString());
}

10.8w+

.NET技术 C#

2006-10-23 10:13