C#复数类Complex的封装
----------------------------------------------------------------------------------------------------------------------------------------------------------本文作者:随煜而安  
时间:   二零一五年七月二十日
----------------------------------------------------------------------------------------------------------------------------------------------------------
本文给出使用C#语言对于复数类Complex的封装。包括大多数基本的运算及方法,个人感觉总结的蛮全的。话不多说,直接上代码!
/// <summary>/// 复数类/// </summary>public class Complex{#region 字段//复数实部private double real = 0.0;//复数虚部private double imaginary = 0.0;#endregion#region 属性/// <summary>/// 获取或设置复数的实部/// </summary>public double Real{get{return real;}set{real = value;}}/// <summary>/// 获取或设置复数的虚部/// </summary>public double Imaginary{get{return imaginary;}set{imaginary = value;}}#endregion#region 构造函数/// <summary>/// 默认构造函数,得到的复数为0/// </summary>public Complex():this(0,0){}/// <summary>/// 只给实部赋值的构造函数,虚部将取0/// </summary>/// <param name="dbreal">实部</param>public Complex(double dbreal):this(dbreal,0){}/// <summary>/// 一般形式的构造函数/// </summary>/// <param name="dbreal">实部</param>/// <param name="dbImage">虚部</param>public Complex(double dbreal, double dbImage){real = dbreal;imaginary = dbImage;}/// <summary>/// 以拷贝另一个复数的形式赋值的构造函数/// </summary>/// <param name="other">复数</param>public Complex(Complex other){real = other.real;imaginary = other.imaginary;}#endregion#region 重载//加法的重载public static Complex operator +(Complex comp1, Complex comp2){return comp1.Add(comp2);}//减法的重载public static Complex operator -(Complex comp1, Complex comp2){return comp1.Substract(comp2);}//乘法的重载public static Complex operator *(Complex comp1, Complex comp2){return comp1.Multiply(comp2);}//==的重载public static bool operator ==(Complex z1, Complex z2){return ((z1.real == z2.real) && (z1.imaginary == z2.imaginary));}//!=的重载public static bool operator !=(Complex z1, Complex z2){if (z1.real == z2.real){return (z1.imaginary != z2.imaginary);}return true;}/// <summary>/// 重载ToString方法,打印复数字符串/// </summary>/// <returns>打印字符串</returns>public override string ToString(){if (Real == 0 && imaginary == 0){return string.Format("{0}", 0);}if (Real == 0 && (imaginary != 1 && imaginary != -1)){return string.Format("{0} i", imaginary);}if (imaginary == 0){return string.Format("{0}", Real);}if (imaginary == 1){return string.Format("i");}if (imaginary == -1){return string.Format("- i");}if (imaginary < 0){return string.Format("{0} - {1} i", Real, -imaginary);}return string.Format("{0} + {1} i", Real, imaginary);}#endregion#region 公共方法/// <summary>/// 复数加法/// </summary>/// <param name="comp">待加复数</param>/// <returns>返回相加后的复数</returns>public Complex Add(Complex comp){double x = real + comp.real;double y = imaginary + comp.imaginary;return new Complex(x, y);}/// <summary>/// 复数减法/// </summary>/// <param name="comp">待减复数</param>/// <returns>返回相减后的复数</returns>public Complex Substract(Complex comp){double x = real - comp.real;double y = imaginary - comp.imaginary;return new Complex(x, y);}/// <summary>/// 复数乘法/// </summary>/// <param name="comp">待乘复数</param>/// <returns>返回相乘后的复数</returns>public Complex Multiply(Complex comp){double x = real * comp.real - imaginary * comp.imaginary;double y = real * comp.imaginary + imaginary * comp.real;return new Complex(x, y);}/// <summary>/// 获取复数的模/幅度/// </summary>/// <returns>返回复数的模</returns>public double GetModul(){return Math.Sqrt(real * real + imaginary * imaginary);}/// <summary>/// 获取复数的相位角,取值范围(-π,π]/// </summary>/// <returns>返回复数的相角</returns>public double GetAngle(){#region 原先求相角的实现,后发现Math.Atan2已经封装好后注释实部和虚部都为0//if (real == 0 && imaginary == 0)//{//    return 0;//}//if (real == 0)//{//    if (imaginary > 0)//        return Math.PI / 2;//    else//        return -Math.PI / 2;//}//else//{//    if (real > 0)//    {//        return Math.Atan2(imaginary, real);//    }//    else//    {//        if (imaginary >= 0)//            return Math.Atan2(imaginary, real) + Math.PI;//        else//            return Math.Atan2(imaginary, real) - Math.PI;//    }//}#endregionreturn Math.Atan2(imaginary, real);}/// <summary>/// 获取复数的共轭复数/// </summary>/// <returns>返回共轭复数</returns>public Complex Conjugate(){return new Complex(this.real, -this.imaginary);}#endregion}

C#复数类Complex的封装相关推荐

  1. 定义一个复数类Complex,重载运算符+

    定义一个复数类Complex,重载运算符"+".使之能用于复数的加法运算,将运算符函数重载为非成员.非友员的普通函数.编写程序求2个复数之和. #include<iostre ...

  2. 定义一个复数类Complex,重载运算符“+”,

    定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算符可以都是类对象.也可以其中一个是整数,顺序任意.例如:c1+c2,i+c1,c1+i ...

  3. 1、定义一个复数类Complex,使得下面的代码能够工作。 Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show();

    一.问题描述 1.定义一个复数类Complex,使得下面的代码能够工作. Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); #includ ...

  4. 建立一个复数类Complex,其私有数据成员mX和mY表示复数的实部和虚部,构造函数Complex用于对复数的实部和虚部初始化

    建立一个复数类Complex,其私有数据成员mX和mY表示复数的实部和虚部,构造函数Complex用于对复数的实部和虚部初始化,友员函数Add,Sub,Mul和Div分别用于进行复数的加.减.乘和除法 ...

  5. Java练习题 类 编写一个程序,使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i 。

    编写一个程序,使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i . 复数类Complex必须满足如下要求: (1) 复数类Complex 的属性有: real ...

  6. 定义复数类Complex,重载运算符“+”,使之用于复数的加法运算

    定义复数类Complex,重载运算符"+",使之用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数. C++代码实现: #include<iostream> ...

  7. 定义一个复数类Complex,重载运算符“+”,“ -”,“*”,“/”使之能用于计算两个复数的加减乘除。

    定义一个复数类Complex,重载运算符"+"," -","*","/"使之能用于计算两个复数的加减乘除.运算符重载函数 ...

  8. 定义一个复数类Complex

    3. 定义一个复数类 Complex ,使得代码能够进行下面的工作 (20 分 ) : Complex c1(3, 5); // 用复数 3+5i 初始化 c1 Complex c2 = 4.5; / ...

  9. 设计复数类Complex,实现运算符重载。

    设计复数类Complex,实现运算符重载. 要求: (1)重载运算符"+"和"*",使之分别能用于复数相加和相乘.(30分) (2)重载运算符"< ...

最新文章

  1. 使用ecshop电子商务系统的100个小问题
  2. 必会Redis单节点、Sentinel和Cluster操作实战
  3. 超越RetinaFace,腾讯优图 ASFD 已在 WIDER FACE 霸榜半年!
  4. 判别两棵树是否相等 设计算法_从匈牙利算法到KM算法
  5. ctypes python_Python ctypes 使用总结
  6. 【C# interface接口】模拟MP3/AVI播放器
  7. Python基础-小程序练习(跳出多层循环,购物车,多级菜单,用户登录)
  8. 如何为***选择合适的动态密码双因素认证方案
  9. 数据结构、算法及应用 课内模板整理
  10. MFC入门示例之静态文本框、编辑框
  11. 《Linux就该这么学》培训笔记_ch18_使用MariaDB数据库管理系统
  12. 测试的第三重境界:挑战零缺陷
  13. 证券公司财务帐单分析报告自动生成系统
  14. springboot 返回输出流_Spring Boot 静态资源处理,妙招
  15. Linux 命令大全(超全实用型)
  16. Html5微信小游戏怎么运行,怎么用pixi.js开发微信小游戏
  17. matlab 模型运行速度,用matlab求解超效率DEA模型运行结果的辨认
  18. VC.【转】采用_beginthread/_beginthreadex函数创建多线程
  19. 联想拯救者2020 Y7000安装Ubuntu16.04
  20. Correlation Congruence for Knowledge Distillation

热门文章

  1. Java中序列化的好处及意义
  2. MVVM Light Toolkit使用指南
  3. 关于 webapi ajax进度条信息设置
  4. 我的iOS学习历程 - UISegmentedControl
  5. 模拟 Codeforces Round #288 (Div. 2) A. Pasha and Pixels
  6. MVC中Model BLL层Model模型互转
  7. 谷歌浏览器手势_分享一些日常手势[狗头]
  8. 与时间相关的java源码_Java 调整日期和时间
  9. 高德sdk_联手HERE,高德进军地图海外市场 | CES 2020
  10. Ansys节点数据批量一键导出脚本生成CSV (ansys数据导出利用matlab脚本)