C#编写一个复数类,要求: 1、具有实部和虚部属性 2、默认构造函数初始化实部和虚部为0 3、完成复数的加法
4、编写一个测试类或方法对复数类进行测试,显示1+2i与3+4i相加结果和3与2+3i相加的结果...
4、编写一个测试类或方法对复数类进行测试,显示1+2i与3+4i相加结果和3与2+3i相加的结果
展开
展开全部
class Complex
{
private double a;
public double A
{
get { return a; }
set { a = value; }
}
private double b;
public double B
{
get { return b; }
set { b = value; }
}
public Complex()
{
a = 0;
b = 3;
}
public void SetNumber(double x, double y)
{
a = x;
b = y;
}
public static Complex operator +(Complex x, Complex y)
{
Complex temp=new Complex();
temp.A = x.a + y.A;
temp.B = x.B + y.B;
return temp;
}
public void Display()
{
if (b == 0)
{
Console.WriteLine("{0}", a);
}
if (a == 0 && b != 0)
{
Console.WriteLine("{0}i", b);
}
if (a != 0 && b > 0)
{
Console.WriteLine("{0}+{1}i", a, b);
}
if (a != 0 && b < 0)
{
Console.WriteLine("{0}{1}i", a, b);
}
}
}
static void Main(string[] args)
{
Complex num1=new Complex();
Complex num2=new Complex();
Complex temp = new Complex();
num1.Display();
num2.Display();
num1.SetNumber(1, 2);
num2.SetNumber(3, 4);
num1.Display();
num2.Display();
temp = num1 + num2;
Console.Write("(1+2i)+(3+4i)=");
temp.Display();
num1.SetNumber(3, 0);
num2.SetNumber(2, 3);
num1.Display();
num2.Display();
temp = num1 + num2;
Console.Write("3+(2+3i)=");
temp.Display();
}
{
private double a;
public double A
{
get { return a; }
set { a = value; }
}
private double b;
public double B
{
get { return b; }
set { b = value; }
}
public Complex()
{
a = 0;
b = 3;
}
public void SetNumber(double x, double y)
{
a = x;
b = y;
}
public static Complex operator +(Complex x, Complex y)
{
Complex temp=new Complex();
temp.A = x.a + y.A;
temp.B = x.B + y.B;
return temp;
}
public void Display()
{
if (b == 0)
{
Console.WriteLine("{0}", a);
}
if (a == 0 && b != 0)
{
Console.WriteLine("{0}i", b);
}
if (a != 0 && b > 0)
{
Console.WriteLine("{0}+{1}i", a, b);
}
if (a != 0 && b < 0)
{
Console.WriteLine("{0}{1}i", a, b);
}
}
}
static void Main(string[] args)
{
Complex num1=new Complex();
Complex num2=new Complex();
Complex temp = new Complex();
num1.Display();
num2.Display();
num1.SetNumber(1, 2);
num2.SetNumber(3, 4);
num1.Display();
num2.Display();
temp = num1 + num2;
Console.Write("(1+2i)+(3+4i)=");
temp.Display();
num1.SetNumber(3, 0);
num2.SetNumber(2, 3);
num1.Display();
num2.Display();
temp = num1 + num2;
Console.Write("3+(2+3i)=");
temp.Display();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询