zoukankan      html  css  js  c++  java
  • 复数类完整实现 + 四则运算符重载

    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();
            }
        }
    }

  • 相关阅读:
    在yii中使用Filter实现RBAC权限自动判断
    关于WEB设计透明和阴影
    一句话扯扯数据结构的概念点
    Console API Google 浏览器开发人员工具使用
    git提交项目时候,忽略一些文件
    学习笔记 如何解决IE6 position:fixed固定定位问题{转载}
    [转载]yii jquery折叠、弹对话框、拖拽、滑动条、ol和ul列表、局部内容切换
    Jquery 常用方法经典总结【砖】
    PHP中冒号、endif、endwhile、endfor这些都是什么
    [转载]救命的PHP代码
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/8304746.html
Copyright © 2011-2022 走看看