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

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值