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