예제 #1
0
 public ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2)
 {
     double real1 = n1.Real * n2.Real;
     double imaginary1 = n1.Real * n2.Imaginary;
     double imaginary2 = n2.Real * n1.Imaginary;
     double real2 = n1.Imaginary * n2.Imaginary * -1;
     //Return the base type
     return new ComplexNumber(real1 + real2, imaginary1 + imaginary2);
 }
예제 #2
0
 public ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2)
 {
     ComplexNumber conjugate = new ComplexNumber(n2.Real, -1*n2.Imaginary);
     ComplexNumber numerator = Multiply(n1, conjugate);
     ComplexNumber denominator = Multiply(n2, conjugate);
     //Return the base type
     return new ComplexNumber(numerator.Real / denominator.Real, numerator.Imaginary);
 }
예제 #3
0
 public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
 {
     //Return the derived type
     return new ComplexNumberWithMagnitude(n1.Real - n2.Real, n1.Imaginary - n2.Imaginary);
 }
예제 #4
0
 public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
 {
     //Return the derived type
     return new ComplexNumberWithMagnitude(n1.Real + n2.Real, n1.Imaginary + n2.Imaginary);
 }
예제 #5
0
        static void Main()
        {
            // Create a client 
            DataContractCalculatorClient client = new DataContractCalculatorClient();

            // Call the Add service operation.
            ComplexNumber value1 = new ComplexNumber(); value1.real = 1; value1.imaginary = 2;
            ComplexNumber value2 = new ComplexNumber(); value2.real = 3; value2.imaginary = 4;
            ComplexNumber result = client.Add(value1, value2);
            Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }


            // Call the Subtract service operation.
            value1 = new ComplexNumber(); value1.real = 1; value1.imaginary = 2;
            value2 = new ComplexNumber(); value2.real = 3; value2.imaginary = 4;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }

            // Call the Multiply service operation.
            value1 = new ComplexNumber(); value1.real = 2; value1.imaginary = 3;
            value2 = new ComplexNumber(); value2.real = 4; value2.imaginary = 7;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }

            // Call the Divide service operation.
            value1 = new ComplexNumber(); value1.real = 3; value1.imaginary = 7;
            value2 = new ComplexNumber(); value2.real = 5; value2.imaginary = -2;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }

            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
예제 #6
0
 public ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2)
 {
     //Return the derived type
     return(new ComplexNumberWithMagnitude(n1.Real - n2.Real, n1.Imaginary - n2.Imaginary));
 }
예제 #7
0
 public ComplexNumber Add(ComplexNumber n1, ComplexNumber n2)
 {
     //Return the derived type
     return(new ComplexNumberWithMagnitude(n1.Real + n2.Real, n1.Imaginary + n2.Imaginary));
 }
예제 #8
0
        static void Main()
        {
            // Create a client
            DataContractCalculatorClient client = new DataContractCalculatorClient();

            // Call the Add service operation.
            ComplexNumber value1 = new ComplexNumber(); value1.real = 1; value1.imaginary = 2;
            ComplexNumber value2 = new ComplexNumber(); value2.real = 3; value2.imaginary = 4;
            ComplexNumber result = client.Add(value1, value2);

            Console.WriteLine("Add({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                              value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }


            // Call the Subtract service operation.
            value1 = new ComplexNumber(); value1.real = 1; value1.imaginary = 2;
            value2 = new ComplexNumber(); value2.real = 3; value2.imaginary = 4;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                              value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }

            // Call the Multiply service operation.
            value1 = new ComplexNumber(); value1.real = 2; value1.imaginary = 3;
            value2 = new ComplexNumber(); value2.real = 4; value2.imaginary = 7;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                              value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }

            // Call the Divide service operation.
            value1 = new ComplexNumber(); value1.real = 3; value1.imaginary = 7;
            value2 = new ComplexNumber(); value2.real = 5; value2.imaginary = -2;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0} + {1}i, {2} + {3}i) = {4} + {5}i",
                              value1.real, value1.imaginary, value2.real, value2.imaginary, result.real, result.imaginary);
            if (result is ComplexNumberWithMagnitude)
            {
                Console.WriteLine("Magnitude: {0}", ((ComplexNumberWithMagnitude)result).Magnitude);
            }
            else
            {
                Console.WriteLine("No magnitude was sent from the service");
            }

            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();

            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }