typedef struct Complex{
double real;
int img;
} Complex;
我見過的人使用它作爲像類型:我有一個結構,但我不知道如何使用它
Complex sqrt(double x) {
}
如何做到「真實」和「IMG」戲在這種類型的功能中的作用?由於
typedef struct Complex{
double real;
int img;
} Complex;
我見過的人使用它作爲像類型:我有一個結構,但我不知道如何使用它
Complex sqrt(double x) {
}
如何做到「真實」和「IMG」戲在這種類型的功能中的作用?由於
它可以像這樣使用:
Complex sqrt(double x) {
Complex c = {0.0, 0.0};
if (x>= 0.0)
c.real = square_root(x);
else
c.img = square_root(-x);
return c;
}
我不知道這是否是一個錯誤,但複雜:: IMG應該也是雙倍的。
(注意複數是實數的超集,所以一個複數可以在雙的地方使用,如果它的虛部爲零)
你可能會使用這樣的:
Complex sqrt(double x) {
Complex r;
r.real = f(x);
r.img = g(x);
return r;
}
在這個例子中,f(x)
和g(x)
將是計算複數x
的平方根的實部和虛部函數的調用。 (在現實中,你可能會計算sqrt()
函數內部平方根,但我只是顯示這個作爲如何使用Complex
結構的例子。)
這裏是一個reference that explains structures in C,這可能對你有所幫助。
Complex numbers在數學中有廣泛的用途 - 作用是什麼將取決於應用程序的上下文。
我想,如果簽名是
Complex sqrt(double x);
那麼x代表了真正的價值。所以Complex.img可以是0/1,表示x是正面還是負面。
示例(以x作爲實數)
//C like pseudocode
Complex sqrt(double x){
Complex result={0,0};
if (x==0) return result;
if (x<0){
result.img =1;
real = abs(x);
}
result.real= sqrt_(x);//calculates square root of a positive value.
return result;
}
//some other place
double r =-4.0;
Complex root = sqrt(r);
//prints "Square root of -4.0 is 2i"
printf("Square root of %.2f is %.2f%c",r,root.real,(root.img?'i':''));
虛部應該是一個雙過。
對於一個真正的(雙X):
Sqrt(x).Real = x >= 0 : Math::Sqrt(x) : 0;
Sqrt(x).Imaginary = x < 0 : Math::Sqrt(x) : 0;
像FinnNk建議,讀了一些關於複雜的數學問題。
複數的平方根通常不是在你手上的計算器計算....
退房它是用來改變的變量極座標DeMoivre定理 - 對於其中有一個封閉的形式一個複數的平方根公式a + ib。
Paul
怎麼了?它通過值返回一個'Complex'結構體。 第一個代碼段使用複雜的結構名稱和變量定義:它不能工作。 – ntd 2009-10-25 22:23:46
你是否錯過了'typedef'? – pmg 2009-10-25 22:27:21
請記住,C中的結構和typedef都有*獨立的*名稱空間。因此'Complex'和'struct Complex'都是具有上述定義的有效類型。此外,強制性的:http://en.wikipedia.org/wiki/Complex_number。 – 2009-10-25 22:36:30