C++ 的復數類(std::complex)位于 <complex> 頭文件中,提供了一系列用于處理復數的數學函數。以下是一些常見的復數操作:
operator+operator-operator*operator/std::conjstd::absstd::argstd::realstd::imagstd::sqrtstd::powstd::floorstd::ceilstd::maxstd::min這里有一個簡單的例子展示如何使用 C++ 的復數類:
#include <iostream>
#include <complex>
int main() {
std::complex<double> c1(3, 4); // 創建一個復數 3 + 4i
std::complex<double> c2(1, 2); // 創建一個復數 1 + 2i
std::cout << "c1 + c2 = " << c1 + c2 << std::endl;
std::cout << "c1 - c2 = " << c1 - c2 << std::endl;
std::cout << "c1 * c2 = " << c1 * c2 << std::endl;
std::cout << "c1 / c2 = " << c1 / c2 << std::endl;
std::cout << "Conjugate of c1 = " << std::conj(c1) << std::endl;
std::cout << "Absolute value of c1 = " << std::abs(c1) << std::endl;
std::cout << "Argument of c1 = " << std::arg(c1) << std::endl;
return 0;
}
注意,在使用復數類時,請確保已經包含了 <complex> 頭文件。