首页 > 代码库 > 用C#实现复数运算

用C#实现复数运算

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

namespace Sample_01_CA
{
    public class Complex
    {
        //定义复试的实部和虚部
        private int integer;
        private int fraction;
        //构造函数
        public Complex(int integer,int fraction)
        {
            this.integer = integer;
            this.fraction = fraction;
        }
        //重载运算符
        public static Complex operator +(Complex left,Complex right)
        {
            return new Complex(left.integer + right.integer, left.fraction + right.fraction);
        }
        //重写从Object继承的ToString()方法
        public override string ToString()
        {
            return integer.ToString()+"."+fraction.ToString();
        }
        static void Main(string[] args)
        {
            Complex left = new Complex(2008, 10);
            Complex right = new Complex(100, 1);
            Console.WriteLine(left.ToString() + " + " + right.ToString() + " = " + (left + right).ToString());
            Console.Read();
        }
    };
}

用C#实现复数运算