複數在計算機程式設計非常有用,常常如視窗化程式時,經常要對平面點的點等進行刻畫;除此很多時候用到的兩個數一族的情況,一樣可以用複數來描述。
前段時間筆者做一個基于Window的示範程式是,就是用C語言來描述它;在此,比着來出來分享一下。
/*
* Complex(Point) struct and funtions
* Note: required header>> math.h
* defined >> Complex
* Author: Neil yan [email protected] Mar 27th,2010 updated
*/
#ifndef _Neil_COMPLEX
#define _Neil_COMPLEX
/*
* Complex point struct
*/
typedef struct{
float x;
float y;
}Complex;
/*
* the Modulus of Complex a
*/
float M(Complex a){
return sqrt( a.x * a.x + a.y * a.y );
}
/*
* Get the Complex of a^n
*/
Complex I(Complex a,int n){
Complex b,c=a;
while(--n>=1){
b.x = c.x * c.x - c.y * c.y;
b.y = 2 * c.x * c.y;
c = b;
}
return b;
}
/*
* Get the Complex of a + b
*/
Complex A(Complex a,Complex b){
Complex c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
#endif
本頭檔案沒有做複數的乘法與除法,因為其用的很少。如有需要,筆者可在完善,同時也請大加指責。
-----By Neil