• 复数类完整实现 + 四则运算符重载


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ImaginaryNumber   //  本来写虚数的,一不小心写成完整的复数了
    {
        class Complex   //  虚数类
        {
            private double Real;    //  实部
            private double Img;     //  虚部
    
            public double GetReal   // 获取实部
            {
                get { return Real; }
                set { Real = value; }
            }
    
            public double GetImg    // 获取虚部
            {
                get { return Img; }
                set { Img = value; }
            }
    
            public Complex() // 构造函数
            {
                Real = Img = 0;
            }
    
            public Complex(double real, double img) // 重载构造函数
            {
                Real = real;
                Img = img;
            }
    
            public void Print() // 输出虚数,符合书写习惯,且四舍五入保留小数点后4位数字
            {
                if (Real == 0 && Img == 0)      // 为零
                    Console.WriteLine("0");
                else if (Real == 0)             // 只有虚部,纯虚数
                    Console.WriteLine("{0}i", (double)Math.Round((double)Img, 4));
                else if (Img == 0)              // 只有实部,实数
                    Console.WriteLine("{0}", (double)Math.Round((double)Real, 4));
                else if (Img < 0)               // 虚部为负
                    Console.WriteLine("{0} - {1}i", (double)Math.Round((double)Real, 4), (double)Math.Round((double)-Img, 4));
                else                            // 标准的复数 
                    Console.WriteLine("{0} + {1}i", (double)Math.Round((double)Real, 4), (double)Math.Round((double)Img, 4));
            }
    
            public static Complex operator +(Complex c1, Complex c2)   // 重载加号
            {
                return new Complex(c1.Real + c2.Real, c1.Img + c2.Img);
            }
    
            public static Complex operator -(Complex c)    // 重载取反符号
            {
                return new Complex(-c.Real, -c.Img);
            }
    
            public static Complex operator -(Complex c1, Complex c2)   // 重载减号
            {
                return new Complex(c1.Real - c2.Real, c1.Img - c2.Img);
            }
    
            public static Complex operator *(Complex c1, Complex c2)   // 重载乘号
            {
                return new Complex(c1.Real * c2.Real - c1.Img * c2.Img, c1.Real * c2.Img + c1.Img * c2.Real);
            }
    
            public static Complex operator /(Complex c1, Complex c2)   //  重载除号
            {
                return new Complex((c1.Real * c2.Real + c1.Img * c2.Img) * 1.0 / (c2.Real * c2.Real + c2.Img * c2.Img), (c1.Img * c2.Real - c1.Real * c2.Img) * 1.0 / (c2.Real * c2.Real + c2.Img * c2.Img));
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                // 随意测试
                Complex c1 = new Complex();
                Complex c2 = new Complex(5, 5);
                Complex c3 = new Complex(0, -2.3);
                Complex c4 = new Complex(3, 0);
                c4 = -c4;
                Console.Write("c1 = "); c1.Print();
                Console.Write("c2 = "); c2.Print();
                Console.Write("c3 = "); c3.Print();
                Console.Write("c4 = "); c4.Print();
                c1 = c2 + c3;
                c2 = c3 / c4;
                c3 = c1 * c2;
                c4 = c2 - c3;
                Console.Write("c1 = "); c1.Print();
                Console.Write("c2 = "); c2.Print();
                Console.Write("c3 = "); c3.Print();
                Console.Write("c4 = "); c4.Print();
                Console.ReadLine();
            }
        }
    }

  • 相关阅读:
    Oracle分析函数row_number()等的使用实例
    Oracle行列转换case when then方法案例
    Oracle日期函数
    Oracle转换函数
    Oracle数值函数
    Oracle字符函数length substr concat实例
    Oracle列转行函数LISTAGG()
    Oracle 条件判断函数decode和case when then案例
    Oracle NVL空值处理函数
    【学亮开讲】Oracle内外连接查询20181119
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/8304746.html
Copyright © 2020-2023  润新知