[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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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