打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C# 实现 复数 运算 类

实现复数的加、减、乘、除,求实部、虚部、模和命令行输出。源代码如下:

    /// <summary>

    /// 复数类

    /// </summary>

    public class Complex

    {

 

        /// <summary>

        /// 默认构造函数

        /// </summary>

        public Complex()

            : this(0, 0)

        {

        }

 

        /// <summary>

        /// 只有实部的构造函数

        /// </summary>

        /// <param name="real">实部</param>

        public Complex(double real)

            : this(real, 0) { }

 

        /// <summary>

        /// 由实部和虚部构造

        /// </summary>

        /// <param name="real">实部</param>

        /// <param name="image">虚部</param>

        public Complex(double real, double image)

        {

            this.real = real;

            this.image = image;

        }

 

        private double real;

        /// <summary>

        /// 复数的实部

        /// </summary>

        public double Real

        {

            get { return real; }

            set { real = value; }

        }

 

        private double image;

        /// <summary>

        /// 复数的虚部

        /// </summary>

        public double Image

        {

            get { return image; }

            set { image = value; }

        }

 

        ///重载加法

        public static Complex operator +(Complex c1, Complex c2)

        {

            return new Complex(c1.real + c2.real, c1.image + c2.image);

        }

 

        ///重载减法

        public static Complex operator -(Complex c1, Complex c2)

        {

            return new Complex(c1.real - c2.real, c1.image - c2.image);

        }

 

        ///重载乘法

        public static Complex operator *(Complex c1, Complex c2)

        {

            return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);

        }

 

        /// <summary>

        /// 求复数的模

        /// </summary>

        /// <returns></returns>

        public double ToModul()

        {

            return Math.Sqrt(real * real + image * image);

        }

 

        /// <summary>

        /// 重载ToString方法

        /// </summary>

        /// <returns>打印字符串</returns>

        public override string ToString()

        {

            if (Real == 0 && Image == 0)

            {

                return string.Format("{0}", 0);

            }

            if (Real == 0 && (Image != 1 && Image != -1))

            {

                return string.Format("{0} i", Image);

            }

            if (Image == 0)

            {

                return string.Format("{0}", Real);

            }

            if (Image == 1)

            {

                return string.Format("i");

            }

            if (Image == -1)

            {

                return string.Format("- i");

            }

            if (Image < 0)

            {

                return string.Format("{0} - {1} i", Real, -Image);

            }

            return string.Format("{0} + {1} i", Real, Image);

        }

    }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
.Net6 WebApi图片预览和下载
超强C#图片上传,加水印,自动生成缩略图源代码(2)
ASP.NET MVC实现网站验证码功能(中)
c#截图工具
asp.net 2.0 验证模块的实现
C# 后台判断一个字符串是否为整数或者带两位小数的数字
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服