[C#]复数类,练习重载

using System;

namespace _07
{
    
class Complex
    
{
        
private double m_dRealPart;
        
private double m_dImagePart;

        
public Complex(double dRealPartdouble 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 op1Complex op2)
        {
            
Complex res new Complex();
            
res op1 + (-op2);
            
return res;
        }
        
static public Complex operator *(Complex op1Complex 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 op1Complex 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 == && m_dImagePart == 0)
            {
                
res "0";
            }
            
else if (m_dRealPart == && m_dImagePart != 0)
            {
                
res m_dRealPart.ToString();
            }
            
else if (m_dRealPart != && 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 abc;
            
new Complex(12);
            
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());
            
new Complex(3, -4);
            
Console.WriteLine("a = {0}"b.ToString());
            
new Complex(a);
            
Console.WriteLine("c = a = {0}"c.ToString());
            
Console.WriteLine("({0}) + ({1}) = {2}"a.ToString(), b.ToString(), (b).ToString());
            
Console.WriteLine("({0}) - ({1}) = {2}"a.ToString(), b.ToString(), (b).ToString());
            
Console.WriteLine("({0}) * ({1}) = {2}"a.ToString(), b.ToString(), (b).ToString());
            
Console.WriteLine("({0}) / ({1}) = {2}"a.ToString(), b.ToString(), (b).ToString());

            
Console.ReadKey(true);
        }
    }
}

/*
 * 学习手记
 * 
 * 没想到 C# 的重载跟 C++ 差那么多!
 * = 不能重载了
 * 也不用 this —— 这个倒明朗得多了
 * 
 * 2007-06-14
 */

 

(原发表于 CSDN:https://blog.csdn.net/cnStreamlet/article/details/1653001)

posted on 2007-06-14 23:28  溪流  阅读(51)  评论(0编辑  收藏  举报