[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
 */
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C#中,可以通过重载运算符来实现矩阵的乘法运算符重载。通过重载乘法运算符,我们可以实现两个矩阵对象之间的乘法操作。重载乘法运算符的方法是在类中定义一个名为"operator*"的方法。该方法接受两个矩阵对象作为参数,并返回一个新的矩阵对象,表示两个矩阵的乘法结果。 下面是一个示例代码,展示了如何在C#重载矩阵乘法运算符: ```csharp public class Matrix { // 矩阵的行数和列数 private int rows; private int columns; // 矩阵的数据 private int[,] data; // 构造函数 public Matrix(int rows, int columns) { this.rows = rows; this.columns = columns; this.data = new int * matrix2.data[k, j]; } result.data[i, j = sum; } } return result; } } // 示例用法 Matrix matrix1 = new Matrix(2, 3); Matrix matrix2 = new Matrix(3, 2); Matrix result = matrix1 * matrix2; ``` 在上面的示例中,我们定义了一个名为Matrix的类,其中包含了重载乘法运算符的方法。在使用时,我们可以创建两个矩阵对象matrix1和matrix2,并通过乘法运算符*将它们相乘得到结果矩阵result。 请注意,为了进行矩阵乘法运算,我们需要确保第一个矩阵的列数与第二个矩阵的行数相等。如果不满足这个条件,就无法进行矩阵乘法运算。 以上是关于C#矩阵乘法运算符重载的解释和示例代码。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C#运算符重载](https://blog.csdn.net/q269399361/article/details/52924869)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C#中运算符重载](https://blog.csdn.net/cloproad/article/details/79028076)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cnStreamlet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值