复数运算

Complex.cs实现Complex类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test04_4_Complex
{
    class Complex
    {
        private double real;
        private double image;
        public Complex()
        {
            real = 0;
            image = 0;
        }
        public Complex(double r,double i)
        {
            real = r;
            image = i;
        }
        //输入
        public void Input(double r,double i)
        {
            real = r;
            image = i;
        }
        //输出
        public string Output()
        {
            if(image >= 0)
            {
                return (real + "+" + image + "i");
            }
            else
                return (real + "-" + (-image) + "i");
               //return real + "+" + image + "i";
        }
        /*运算符重载实现加法*/
        public static Complex operator + (Complex s,Complex p)
        {
            Complex C = new Complex();
            C.real = s.real + p.real;
            C.image = s.image + p.image;
            return C;
        }
        /*减法*/
        public static Complex operator - (Complex s,Complex p)
        {
            Complex C = new Complex();
            C.real = s.real - p.real;
            C.image = s.image - p.image;
            return C;
        }

        /*
         *乘法
         *(a+bi)(c+di)=(ac-bd)+(bc+ad)i
         */
        public static Complex operator *(Complex s, Complex p)
        {
            Complex C = new Complex();
            C.real = (s.real * p.real - s.image * p.image);
            C.image = (s.image * p.real + s.real * p.image);
            return C;
        }
        /*
         * 除法
         *(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i
         */
        public static Complex operator /(Complex s, Complex p)
        {
            Complex C = new Complex();
            C.real = (s.real * p.real + s.image * p.image) / (Math.Pow(p.image, 2) + (Math.Pow(p.real, 2)));
            C.image = (s.image * p.real - s.real * p.image) / (Math.Pow(p.image, 2) + (Math.Pow(p.real, 2)));
            return C;
        }
    }
}


Program.cs实现运算:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test04_4_Complex
{
    class Program
    {
        static void Main(string[] args)
        {
            Complex c1 = new Complex(10, 20);
            Complex c2 = new Complex(20, 30);
            Complex c3 = new Complex();

            Console.WriteLine(c1.Output());
            Console.WriteLine(c2.Output());
            c2.Input(10, -10);
            Console.WriteLine(c2.Output());
            c3 = c1 + c2;//加法
            Console.WriteLine("({0})+({1})={2}",c1.Output(),c2.Output(),c3.Output());
            c3 = c1 - c2;//减法
            Console.WriteLine("({0})-({1})={2}", c1.Output(), c2.Output(), c3.Output());
            c3 = c1 * c2;//乘法
            Console.WriteLine("({0})*({1})={2}", c1.Output(), c2.Output(), c3.Output());
            c3 = c1 / c2;//除法
            Console.WriteLine("({0})/({1})={2}", c1.Output(), c2.Output(), c3.Output());

        }
    }
}

结果就是这样:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值