Search code examples
c#.net.net-corecomplex-numbers

Complex numbers in c#


I have an assignment to write a Complex number implementation :-

Complex c = new Complex(1.2,2.0)

Write properties real and Imaginary to get the real and imaginary part of a complex number. which are used like this:

double x = c.Real;

Write a method to add two complex numbers and return their sum. The real part is the sum of the two real parts, and the imaginary part the sum of the two imaginary parts.

Complex c = c1.Sum(c2);

Write a method to calculate the product of the two complex numbers. If one number has components x1 and y1 and the second number has components, x2 and y2:

the real part of the product = x1 *x2 - y1 *y2; the imaginary part = x1 * y2 + x2 *y1;

So I know and am pretty confident-ish with complex numbers manually such as 4 +5i where 5i is imaginary,

My questions is, I'm not sure how to get the app to know which one is imaginary, unless I make on input a predefined imaginary number.. The minute I do that though the " app " loses it's worthiness cause then it's not an complex number just some random calc app. Basically I have no clue how to proceed.. Thanks


Solution

  • Seems from your question you are confused about the construction of the Complex number. Here's a template to get you started.

    public class Complex
    {
        public Complex(double real, double imaginary)
        {
        }
    }
    

    then start with

     static void Main(string[] args)
     {
        Complex c1 = new Complex(1.2,2.0)
    
        Complex c2 = new Complex(1,3.0)
    
        Complex c3 = c1.Sum(c2);
    
        Console.WriteLine(c3.Real);
        Console.WriteLine(c3.Imaginary);
    
     }
    

    and get that working ( put whatever numbers you like in for starters )