关于运算符的重载
下列程序定义了一实部为real,虚部为imag的复数类complex,要求重载运算符“+”、“-”、“*”实现复数对象的加法、减法、乘法运算,并编写测试程序。class Complex
{
private:
double real, imag;
public:
complex(double r, double i)
{
real=r;
imag=i;
}
};
下面是我写的程序,但是有错误,请帮我修改一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 实验二_2
{
class Program
{
static void Main(string[] args)
{
Complex c1 = new Complex( );
Complex c2 = new Complex( );
Console.WriteLine("请输入一个复数的实部和虚部:");
c1.Real =double.Parse ( Console.ReadLine());
c1.Imag =double.Parse (Console.ReadLine());
Console.WriteLine("请输入一个复数的实部和虚部:")
c2.Real =double.Parse (Console.ReadLine());
c2.Imag =double.Parse ( Console.ReadLine());
Complex c3= double.Parse (operator+(c1,c2));
Console.WriteLine( "两个复数的和为:{0}",c3.Write( ));
Complex c4=double.Parse (operator-(c1,c2));
Console.WriteLine( "两个复数的差为:{0}",c4.Write( ));
Complex c5=double.Parse (operator*(c1,c2));
Console.WriteLine( "两个复数的积为:{0}",c5.Write( ));
}
class Complex
{
private double real,imag;
public double Real
{
get { return real;}
set { real=value;}
}
public double Imag
{
get { return imag;}
set { imag =value;}
}
public Complex (double r,double i)
{
real=r;
imag=i;
}
public static Complex operator+(Complex c1,Complex c2)
{
return new Complex (c1.real+c2.real,c1.imag +c2.imag );
}
public static Complex operator-(Complex c1,Complex c2)
{
return new Complex (c1.real-c2.real ,c1.imag-c2.imag);
}
public static Complex operator*(Complex c1,Complex c2)
{;
return new Complex (c1.real*c2.real ,c1.imag*c2.imag);
}
public void Write()
{
Console.WriteLine ("{0}+{1}i",real,imag);
}
}
}
}