zoukankan      html  css  js  c++  java
  • 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);

            }

        }

  • 相关阅读:
    让Visual Studio 2008 和 2010支持Web Services Enhancements (WSE) 3.0
    不清楚BA的角色是什么
    int的一点事,读《深入C#内存管理来分析值类型&引用类型,装箱&拆箱,堆栈几个概念组合之间的区别》
    Angular2.0视频教程来了!
    [11]缺少动态连接库.socannot open shared object file: No such file or directory
    计算机网络常考知识点总结
    计算机网络——数据链路层
    计算机网络——物理层
    Java内存模型_基础
    JAVA_Lock
  • 原文地址:https://www.cnblogs.com/oyjj/p/2133001.html
Copyright © 2011-2022 走看看