কম্পিউটার টিউটোরিয়াল

C++ প্রোগ্রামিং-এ কমপ্লেক্স নাম্বার (Complex Numbers)


এই অংশে আমরা দেখব C++-এ কমপ্লেক্স নাম্বার কীভাবে তৈরি ও ব্যবহার করা যায়। আমরা একটি কমপ্লেক্স টাইপ ক্লাস তৈরি করতে পারি, যা কমপ্লেক্স নাম্বারের বাস্তব (real) এবং কাল্পনিক (imaginary) অংশ দুটি সদস্য ভেরিয়েবল হিসেবে ধারণ করবে। এই ক্লাসটি হ্যান্ডেল করার জন্য কিছু মেম্বার ফাংশন থাকবে।

এই উদাহরণে আমরা একটি কমপ্লেক্স টাইপ ক্লাস তৈরি করব, কমপ্লেক্স নাম্বার সঠিক ফরম্যাটে দেখানোর জন্য একটি ফাংশন, এবং দুটি কমপ্লেক্স নাম্বার যোগ ও বিয়োগ করার জন্য দুটি অতিরিক্ত মেথড।

উদাহরণ

#include<iostream> using namespace std; class complex{    int real, img;    public:       complex(){          //default constructor to initialize complex number to 0+0i          real = 0; img = 0;       }       complex(int r, int i){          //parameterized constructor to initialize complex number.          real = r; img = i;       }       void set();       void get();       void display();       friend complex add(complex, complex);       friend complex sub(complex, complex); }; void complex::set(){    cout << "Enter Real part: ";    cin >> real;    cout << "Enter Imaginary Part: ";    cin >> img; } void complex::get(){    cout << "The complex number is: "<< real << "+" << img << "i" << endl; } void complex::display(){    if(img < 0)    if(img == -1)       cout << "The complex number is: "<< real << "-i" << endl;    else       cout << "The complex number is: "<< real << img << "i" << endl;    else    if(img == 1)       cout << "The complex number is: "<< real << " + i"<< endl;    else    cout << "The complex number is: "<< real << " + " << img << "i" << endl; } complex add(complex c1, complex c2){    complex res;    res.real = c1.real + c2.real;//addition for real part    res.img = c1.img + c2.img;//addition for imaginary part    return res;//the result after addition } complex sub(complex c1, complex c2){    complex res;    res.real = c1.real - c2.real;//subtraction for real part    res.img = c1.img - c2.img;//subtraction for imaginary part    return res;//the result after subtraction } main(){    complex n1(3, 2), n2(4, -3);    complex result;    result = add(n1,n2);    result.display();    result = sub(n1,n2);    result.display(); }

আউটপুট

The complex number is: 7-i The complex number is: -1 + 5i

  1. C++-এ অপারেটর ওভারলোডিং ব্যবহার করে কমপ্লেক্স নম্বর যোগ করার প্রোগ্রাম

  2. সি++-এ ইকুইডিজিটাল নম্বর

  3. সি++-এ এমিরপ নম্বর

  4. সি++-এ ডাডেনি নম্বর