Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน Proj() สำหรับ Complex Numbers ใน C++

บทความนี้สาธิตการทำงานของ proj() เพื่อทำการ projection บน complex numbers นี่คือ syntax ของ proj() method ใน C++ programming:

template <class T> complex<T> proj (const complex<T>& z);

โปรแกรมตัวอย่าง

เมธอด proj() รับพารามิเตอร์เป็น argument ซึ่งแทน complex number และคืนค่า projection ของ complex number ตามตัวอย่างด้านล่าง:

#include <iostream>
#include <complex>
using namespace std;

int main() {
    std::complex<double> c1(3, 5);
    cout << "Proj" << c1 << " = " << proj(c1) << endl;
    std::complex<double> c2(0, -INFINITY);
    cout << "Proj" << c2 << " = " << proj(c2) << endl;
    std::complex<double> c3(INFINITY, -1);
    cout << "Proj" << c3 << " = " << proj(c3) << endl;
}

จำเป็นต้อง import library complex.h ใน source เพื่อให้ได้ definition ของ projection method implementation ตัวอย่างข้างต้นให้ผลลัพธ์ของ complex number ที่ส่งผ่านมาหลังจากคอมไพล์สำเร็จ:

ผลลัพธ์

Proj(3,5) = (3,5)
Proj(0,-inf) = (inf,-0)
Proj(inf,1) = (inf,-0)