复数运算

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

复数运算_第1张图片
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());

        }
    }
}

结果就是这样:
复数运算_第2张图片

你可能感兴趣的:(C#)