Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#의 복소수

<시간/>

WITH 작업을 하고 C#에서 복소수를 표시하려면 실수 및 허수 값을 확인해야 합니다.

7+5i와 같은 복소수는 실수부 7과 허수부 5의 두 부분으로 구성됩니다. 여기서 허수부는 i의 배수입니다.

완전한 숫자를 표시하려면 -

를 사용하십시오.
public struct Complex

두 복소수를 더하려면 실수부와 허수부를 더해야 합니다. -

public static Complex operator +(Complex one, Complex two) {
   return new Complex(one.real + two.real, one.imaginary + two.imaginary);
}

다음 코드를 실행하여 C#에서 복소수 작업을 시도할 수 있습니다.

예시

using System;
public struct Complex {
   public int real;
   public int imaginary;
   public Complex(int real, int imaginary) {
      this.real = real;
      this.imaginary = imaginary;
   }
   public static Complex operator +(Complex one, Complex two) {
      return new Complex(one.real + two.real, one.imaginary + two.imaginary);
   }
   public override string ToString() {
      return (String.Format("{0} + {1}i", real, imaginary));
   }
}
class Demo {
   static void Main() {
      Complex val1 = new Complex(7, 1);
      Complex val2 = new Complex(2, 6);
      // Add both of them
      Complex res = val1 + val2;
      Console.WriteLine("First: {0}", val1);
      Console.WriteLine("Second: {0}", val2);
      // display the result
      Console.WriteLine("Result (Sum): {0}", res);
      Console.ReadLine();
   }
}

출력

First: 7 + 1i
Second: 2 + 6i
Result (Sum): 9 + 7i