कॉम्प्लेक्स नंबर: a + bi जहां a वास्तविक भाग, b काल्पनिक भाग, i = √-1। C# में System.Numerics.Complex स्ट्रक्चर बिल्ट-इन है (`.NET Framework 4.0+`, `.NET Core`), लेकिन कस्टम स्ट्रक्चर भी बना सकते हैं।
कस्टम Complex स्ट्रक्चर उदाहरण
using System;
public struct Complex
{
public int Real;
public int Imaginary;
public Complex(int real, int imaginary)
{
Real = real;
Imaginary = imaginary;
}
// ऑपरेटर ओवरलोडिंग: +
public static Complex operator +(Complex one, Complex two)
{
return new Complex(one.Real + two.Real, one.Imaginary + two.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 $"{Real} + {Imaginary}i";
}
}
class Demo
{
static void Main()
{
Complex val1 = new Complex(7, 1);
Complex val2 = new Complex(2, 6);
Complex res = val1 + val2;
Console.WriteLine("पहला: {0}", val1);
Console.WriteLine("दूसरा: {0}", val2);
Console.WriteLine("परिणाम (योग): {0}", res);
Console.ReadLine();
}
}
आउटपुट
पहला: 7 + 1i दूसरा: 2 + 6i परिणाम (योग): 9 + 7i
बिल्ट-इन System.Numerics.Complex
using System.Numerics; Complex c1 = new Complex(3, 4); Complex c2 = new Complex(1, 2); Complex sum = c1 + c2; // (4, 6) Complex product = c1 * c2; // (-5, 10) double magnitude = c1.Magnitude; // 5 double phase = c1.Phase; // ~0.927 रेडियन
मुख्य बिंदु
structउपयोग करें (वैल्यू टाइप, हीप आवंटन नहीं)।- ऑपरेटर ओवरलोडिंग:
+,-,*,/,==,!=। - बिल्ट-इन
Complexमें ट्रिग्नोमेट्रिक, लॉगरिदमिक, पावर फंक्शन्स शामिल।