随笔 - 130  文章 - 0  评论 - 12  阅读 - 6954

[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   溪流  阅读(52)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2007年6月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
1 2 3 4 5 6 7

点击右上角即可分享
微信分享提示