TXT

complex

By Diana Ferguson,2014-09-24 10:57
9 views 0
complex

using System;

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

    using System.Collections;

    //12??Éè?ÆÒ??ö??ÊýÀàComplex??ÖØÔØ+??-??*??/ÔËËã?ûÀ?ʵÏÖÁ???ÊýµÄËÄÔ

    òÔËËã????ʵÏÖ?Óint->ComplexµÄÒþÊ?ÀàÐÍת???? //?âÊÔÓÃÀýÈçÏÂ???ÉÒÔ??Ð??âÊÔ?úÂë???º //public static void Main(){

    // int i=1;

    // Complex c1=new Complex(1,1); // Complex c2=new Complex(3,4); // Complex c3=c1+c2;

    // Êä?ö??Êýc3; c3=c1-c2; // Êä?ö??Êýc3; c3=c1*c2; // Êä?ö??Êýc3; c3=c1/c2; // Êä?ö??Êýc3; c3=c1-i; // Êä?ö??Êýc3;

    //}

namespace Csun

    {

     class complex

     {

     public int r;

     public int v;

     public complex() { }

     public complex(int r,int v)

     {

     this.r=r;

     this.v=v;

     }

     public complex (int r)

     {

     this.r=r;

     this.v=0;

     }

     public static complex operator+(complex a,complex b)

     {

     return new complex(a.r+b.r,a.v+b.v);

     }

     public static complex operator-(complex a,complex b)

     {

     return new complex(a.r-b.r,a.v-b.v);

     }

     public static complex operator *(complex a, complex b)

     {

     return new complex(a.r * b.r-a.v*b.v, b.v*a.r +a.v*b.r);

     }

     public static complex operator /(complex x1, complex x2)

     {

     complex result = new complex();

     double a = x2.r * x2.r + x2.v * x2.v;

     x2.v = -x2.v;

     result = x1 * x2;

     result.r = (int)(result.r/a);

     result.v = (int)(result.v/a);

     return result;

     }

     public static complex operator -(complex x1)

     {

     return new complex(-x1.r, -x1.v);

     }

     public static implicit operator complex(int a)

     {

     return new complex (a);

     }

     public static explicit operator int(complex a)

     {

     return (int)Math.Sqrt(a.r*a.r+a.v*a.v);

     }

     public override string ToString()

     {

     return r+" + "+ v+"i";

     }

     }

     class Test

     {

     static void Main(string[] args)

     {

     int i = 8;

     complex c1=new complex(1,1);

     complex c2=new complex(3,4);

     complex c3=c1+c2;

     Console.WriteLine(c3);

     c3 = c1 * c2;

     Console.WriteLine(c3);

     c3 = c2 / c1;

     Console.WriteLine(c3);

     c3=c1-i;

     Console.WriteLine(c3);

     }

     }

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com