[C#]复数类,练习重载
using System;
namespace _07
{
class Complex
{
private double m_dRealPart;
private double m_dImagePart;
public Complex(double dRealPart, double dImagePart)
{
m_dRealPart = dRealPart;
m_dImagePart = dImagePart;
}
public Complex(double dRealPart)
{
m_dRealPart = dRealPart;
m_dImagePart = 0;
}
public Complex()
{
m_dRealPart = 0;
m_dImagePart = 0;
}
public Complex(Complex orig)
{
m_dRealPart=orig.m_dRealPart ;
m_dImagePart=orig.m_dImagePart ;
}
static public Complex operator +(Complex op1,Complex op2)
{
Complex res = new Complex();
res.m_dRealPart = op1.m_dRealPart + op2.m_dRealPart;
res.m_dImagePart = op1.m_dImagePart + op2.m_dImagePart;
return res;
}
static public Complex operator -(Complex op)
{
Complex res = new Complex();
res.m_dRealPart = -op.m_dRealPart;
res.m_dImagePart = -op.m_dImagePart;
return res;
}
static public Complex operator -(Complex op1, Complex op2)
{
Complex res = new Complex();
res = op1 + (-op2);
return res;
}
static public Complex operator *(Complex op1, Complex op2)
{
Complex res = new Complex();
res.m_dRealPart = op1.m_dRealPart * op2.m_dRealPart - op1.m_dImagePart * op2.m_dImagePart;
res.m_dImagePart = op1.m_dImagePart * op2.m_dRealPart + op1.m_dRealPart * op2.m_dImagePart;
return res;
}
static public Complex operator /(Complex op1, Complex op2)
{
Complex res = new Complex();
double temp = op2.m_dRealPart * op2.m_dRealPart + op2.m_dRealPart * op2.m_dImagePart;
res.m_dRealPart = (op1.m_dRealPart * op2.m_dRealPart + op1.m_dImagePart * op2.m_dImagePart) / temp;
res.m_dImagePart = (op1.m_dImagePart * op2.m_dRealPart - op1.m_dRealPart * op2.m_dImagePart) / temp;
return res;
}
public double GetRealPart()
{
return m_dRealPart;
}
public double GetImagePart()
{
return m_dImagePart;
}
public double NormSquare()
{
return m_dRealPart * m_dRealPart + m_dImagePart * m_dImagePart;
}
public override string ToString()
{
string res;
if (m_dRealPart == 0 && m_dImagePart == 0)
{
res = "0";
}
else if (m_dRealPart == 0 && m_dImagePart != 0)
{
res = m_dRealPart.ToString();
}
else if (m_dRealPart != 0 && m_dImagePart == 0)
{
res = m_dImagePart.ToString() + "i";
}
else if (m_dImagePart > 0)
{
res = m_dRealPart.ToString() + "+" + m_dImagePart.ToString() + "i";
}
else
{
res = m_dRealPart.ToString() + m_dImagePart.ToString() + "i";
}
return res;
}
}
class Program
{
static void Main(string[] args)
{
Complex a, b, c;
a = new Complex(1, 2);
Console.WriteLine("a = {0}", a.ToString());
Console.WriteLine("Re(a) = {0}", a.GetRealPart());
Console.WriteLine("Im(a) = {0}", a.GetImagePart());
Console.WriteLine("|a|^2 = {0}", a.NormSquare());
b = new Complex(3, -4);
Console.WriteLine("a = {0}", b.ToString());
c = new Complex(a);
Console.WriteLine("c = a = {0}", c.ToString());
Console.WriteLine("({0}) + ({1}) = {2}", a.ToString(), b.ToString(), (a + b).ToString());
Console.WriteLine("({0}) - ({1}) = {2}", a.ToString(), b.ToString(), (a - b).ToString());
Console.WriteLine("({0}) * ({1}) = {2}", a.ToString(), b.ToString(), (a * b).ToString());
Console.WriteLine("({0}) / ({1}) = {2}", a.ToString(), b.ToString(), (a / b).ToString());
Console.ReadKey(true);
}
}
}
/*
* 学习手记
*
* 没想到 C# 的重载跟 C++ 差那么多!
* = 不能重载了
* 也不用 this —— 这个倒明朗得多了
*
* 2007-06-14
*/
(原发表于 CSDN:https://blog.csdn.net/cnStreamlet/article/details/1653001)