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