看板 C_and_CPP 關於我們 聯絡資訊
//爬文后還是沒有說明如何在C++中 //給定2個實數值在1個複數變數中 初值的設定 //以下是測試過的程式 #include <complex> #include <iostream> #include <math.h> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) { complex <double> x = (0.1, 0.2); complex <double> mu = (0.2, 0.6); cout<<"x = "<<x<<endl; cout<<"mu = "<<mu<<endl<<endl; //以上是原文初始值設定的問題 //以下是修正原文初始值設定的問題 complex <double> y = std::complex <double> (0.1, 0.2); complex <double> nu = std::complex <double> (0.2, 0.6); cout<<"y = "<<y<<endl; cout<<"nu = "<<nu<<endl<<endl; cout<<"y.real = "<<y.real()<<endl; cout<<"nu.real = "<<nu.real()<<endl<<endl; cout<<"y.imag"<<y.imag()<<endl; cout<<"nu.imag = "<<nu.imag()<<endl<<endl; system("pause"); return 0; } ※ 引述《khoguan (Khoguan Phuann)》之銘言: : ※ 引述《cation234 (貓離子)》之銘言: : : 在下找到了這個 說明了為什麼會有這種問題 : : http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/complex.html : 那裡面有幾句話不對。 : "In fact, c2=(3,7) is equivalent to c2=3,7. C++'s "," operator : suppresses the value of the first operand in favour of the second, : so c2=3,7 is equivalent to c2=7." : 當寫成 c2=(3,7); 時, c2 會變成 7, 而 3 是多餘的,沒效果。 : 但寫成 c2=3,7; 時, c2 會變成 3, 而 7 是多餘的,沒效果。 : 請自行實驗看看。 : 至於其道理,則是 comma operator 比 assignment operator 的優先權 : 還要低。 c2=3,7; 等同於 (c2=3), 7; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.20.143