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

本文介绍了一个简单的 C# 复数类的设计与实现,包括加、减、乘、除等基本运算,并展示了如何使用该类进行复数运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 */
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值