增加运算符重载功能:+

来源:互联网 发布:vscode 切换大小写 编辑:程序博客网 时间:2024/05/20 18:42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 重载a_bi
{
    public class Complex
    {
        private int real;
        private int imag;
        public Complex(int real, int imag)
        {
            this.real = real;
            this.imag = imag;
        }
        public int Real
        {
            get { return real;}
            set { real = value; }
        }
        public int Imag
        {
            get { return imag; }
            set { imag = value; }
        }




        //public static Complex Add(Complex a, Complex b)
        //{
        //    return new Complex(a.real + b.real, a.imag + b.imag);
        //}
        //public Complex Add(Complex a)
        //{
        //    return new Complex(real + a.real, imag + a.imag);
        //}
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imag + c2.imag);
        }


        //overload operator(-)  
        public static Complex operator -(Complex c1, Complex c2)
        {
            return new Complex(c1.real - c2.real, c1.imag - c2.imag);
        }


        //overload operator(*)  
        public static Complex operator *(Complex c1, Complex c2)
        {
            return new Complex(c1.real * c2.real - c1.imag * c2.imag,
             c1.real * c2.imag + c1.imag * c2.real);
        }


        //overload operator(/)  
        public static Complex operator /(Complex c1, Complex c2)
        {
            return new Complex(-c1.real * c2.real +
               c1.imag * c2.imag, -c1.real * c2.imag + c1.imag * c2.real);
        }


        public override string ToString()
        {
            if (Real == 0 && Imag == 0)
            { return string.Format("{0}i", 0); }
            if (Real == 0 && (Imag != 1 && Imag != -1))
            { return string.Format("{0}i", Imag); }
            if (Imag == 0)
            {
                return string.Format("{0}", Real);
            }
            if (Imag == 1)
            {
                return string.Format("i");
            }
            if (Imag == -1)
            {
                return string.Format("- i");
            }
            if (Imag < 0)
            {
                return string.Format("{0} - {1} i", Real, -Imag);
            }
            return string.Format("{0} + {1} i", Real, Imag);
        }


    }
    class Program
    {
        
        static void Main(string[] args)
        {
            int e, f, g, h;
            Console.WriteLine("请输入第一个数:");
           
            e = Convert.ToInt32(Console.ReadLine());
            f = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个数:");
            g = Convert.ToInt32(Console.ReadLine());
            h = Convert.ToInt32(Console.ReadLine());


            Complex num1 = new Complex(e,f );
            Complex num2 = new Complex(g ,h );
            
            
            Complex a = num1 + num2;
            Complex b = num1 - num2;
            Complex c = num1 * num2;
            Complex d = num1 / num2;


            //Print the numbers and the Result using the overriden ToString method:  
            Console.WriteLine("First complex number:  {0}", num1);
            Console.WriteLine("Second complex number: {0}", num2);
            Console.WriteLine("加法结果是是: {0}", a);
            Console.WriteLine("减法结果是: {0}", b);
            Console.WriteLine("乘法结果是: {0}", c);
            Console.WriteLine("除法结果是: {0}", d);


            Console.ReadKey();
        }
    }
}