10 个解决方案
#1
STL 里有复数!
#2
分为实部和需部两部分来计算。
乘法:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
除法:(a+bi)/(c+di)=((ac+bd)/(c*c+d*d))+((bc-ad)/(c*c+d*d))i
加减法就不用说了吧
乘法:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
void ChenFa(float a,float b,float c,float d,float *e,float *f){
*e = a*c-b*d;
*f = a*d+b*c;
}
除法:(a+bi)/(c+di)=((ac+bd)/(c*c+d*d))+((bc-ad)/(c*c+d*d))i
void ChuFa(float a,float b,float c,float d,float *e,float *f){
if ( c*c + d*d == 0.0 ){printf("FenMu 0.0");exit(1);};
*e = (a*c+b*d)/(c*c+d*d);
*f = (b*c-a*d)/(c*c+d*d);
}
加减法就不用说了吧
#3
明白!thank you!
#4
#5
厉害 学习了
#6
typedef struct
{
int iReal; //实部
int iVirtual; //虚部
} PluralData;
PluralData Add(PluralData data1, PluralData data2)
{
PluralData result;
result.iReal = data1.iReal + data2.iReal;
result.iVirtual = data2.iVirtual + data2.iVirtual;
return result;
}
{
int iReal; //实部
int iVirtual; //虚部
} PluralData;
PluralData Add(PluralData data1, PluralData data2)
{
PluralData result;
result.iReal = data1.iReal + data2.iReal;
result.iVirtual = data2.iVirtual + data2.iVirtual;
return result;
}
#7
#include <iostream>
#include <cmath>
using namespace std;
template <class T>
class ComplexNumber {
public:
ComplexNumber(){x = 0; y = 0;};
void print(){
cout << "(" << x << ", " << y << ")";
}
ComplexNumber operator+(ComplexNumber c);
ComplexNumber operator-(ComplexNumber c);
ComplexNumber operator*(ComplexNumber c);
ComplexNumber operator/(ComplexNumber c);
T getReal(){return x;};
T getImg(){return y;};
void SetReal(T aX){x = aX;};
void SetImg(T aY){y = aY;};
ComplexNumber conjugate(){
ComplexNumber z;
z.SetReal(x);
z.SetImg(-y);
return z;
};
double computeNormSqr(){return x*x + y*y;};
double computeNorm(){return sqrt(computeNormSqr());};
private:
T x;
T y;
};
// Addition
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator+(ComplexNumber<T> c)
{
ComplexNumber<T> aNumber;
aNumber.SetReal( x + c.getReal() );
aNumber.SetImg( y + c.getImg() );
return aNumber;
}
// Subtraction
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator-(ComplexNumber<T> c)
{
ComplexNumber<T> aNumber;
aNumber.SetReal( x - c.getReal() );
aNumber.SetImg( y - c.getImg() );
return aNumber;
}
// Multiplication
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator*(ComplexNumber<T> c)
{
ComplexNumber<T> aNumber;
aNumber.SetReal( x*c.getReal() - y*c.getImg() );
aNumber.SetImg( x*c.getImg() + y*c.getReal() );
return aNumber;
}
// Division
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator/(ComplexNumber<T> c)
{
ComplexNumber<T> num = (*this) * c.conjugate();
double numNorm = c.computeNormSqr();
num.SetReal(num.getReal()/numNorm);
num.SetImg(num.getImg()/numNorm);
return num;
}
int main (int argc, char * const argv[]) {
ComplexNumber<double> x, y, z;
x.SetReal(1); x.SetImg(2);
y.SetReal(2); y.SetImg(1);
z = x + y; z.print(); cout << endl;
z = x - y; z.print(); cout << endl;
z = x * y; z.print(); cout << endl;
z = x / y; z.print(); cout << endl;
x.SetReal(1); x.SetImg(0);
y.SetReal(0); y.SetImg(1);
z = x + y; z.print(); cout << endl;
z = x - y; z.print(); cout << endl;
z = x * y; z.print(); cout << endl;
z = x / y; z.print(); cout << endl;
return 0;
}
#8
这个比较实用些。。。
#9
这个比较实用些。。。
#1
STL 里有复数!
#2
分为实部和需部两部分来计算。
乘法:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
除法:(a+bi)/(c+di)=((ac+bd)/(c*c+d*d))+((bc-ad)/(c*c+d*d))i
加减法就不用说了吧
乘法:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i
void ChenFa(float a,float b,float c,float d,float *e,float *f){
*e = a*c-b*d;
*f = a*d+b*c;
}
除法:(a+bi)/(c+di)=((ac+bd)/(c*c+d*d))+((bc-ad)/(c*c+d*d))i
void ChuFa(float a,float b,float c,float d,float *e,float *f){
if ( c*c + d*d == 0.0 ){printf("FenMu 0.0");exit(1);};
*e = (a*c+b*d)/(c*c+d*d);
*f = (b*c-a*d)/(c*c+d*d);
}
加减法就不用说了吧
#3
明白!thank you!
#4
#5
厉害 学习了
#6
typedef struct
{
int iReal; //实部
int iVirtual; //虚部
} PluralData;
PluralData Add(PluralData data1, PluralData data2)
{
PluralData result;
result.iReal = data1.iReal + data2.iReal;
result.iVirtual = data2.iVirtual + data2.iVirtual;
return result;
}
{
int iReal; //实部
int iVirtual; //虚部
} PluralData;
PluralData Add(PluralData data1, PluralData data2)
{
PluralData result;
result.iReal = data1.iReal + data2.iReal;
result.iVirtual = data2.iVirtual + data2.iVirtual;
return result;
}
#7
#include <iostream>
#include <cmath>
using namespace std;
template <class T>
class ComplexNumber {
public:
ComplexNumber(){x = 0; y = 0;};
void print(){
cout << "(" << x << ", " << y << ")";
}
ComplexNumber operator+(ComplexNumber c);
ComplexNumber operator-(ComplexNumber c);
ComplexNumber operator*(ComplexNumber c);
ComplexNumber operator/(ComplexNumber c);
T getReal(){return x;};
T getImg(){return y;};
void SetReal(T aX){x = aX;};
void SetImg(T aY){y = aY;};
ComplexNumber conjugate(){
ComplexNumber z;
z.SetReal(x);
z.SetImg(-y);
return z;
};
double computeNormSqr(){return x*x + y*y;};
double computeNorm(){return sqrt(computeNormSqr());};
private:
T x;
T y;
};
// Addition
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator+(ComplexNumber<T> c)
{
ComplexNumber<T> aNumber;
aNumber.SetReal( x + c.getReal() );
aNumber.SetImg( y + c.getImg() );
return aNumber;
}
// Subtraction
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator-(ComplexNumber<T> c)
{
ComplexNumber<T> aNumber;
aNumber.SetReal( x - c.getReal() );
aNumber.SetImg( y - c.getImg() );
return aNumber;
}
// Multiplication
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator*(ComplexNumber<T> c)
{
ComplexNumber<T> aNumber;
aNumber.SetReal( x*c.getReal() - y*c.getImg() );
aNumber.SetImg( x*c.getImg() + y*c.getReal() );
return aNumber;
}
// Division
template <class T>
ComplexNumber<T> ComplexNumber<T>::operator/(ComplexNumber<T> c)
{
ComplexNumber<T> num = (*this) * c.conjugate();
double numNorm = c.computeNormSqr();
num.SetReal(num.getReal()/numNorm);
num.SetImg(num.getImg()/numNorm);
return num;
}
int main (int argc, char * const argv[]) {
ComplexNumber<double> x, y, z;
x.SetReal(1); x.SetImg(2);
y.SetReal(2); y.SetImg(1);
z = x + y; z.print(); cout << endl;
z = x - y; z.print(); cout << endl;
z = x * y; z.print(); cout << endl;
z = x / y; z.print(); cout << endl;
x.SetReal(1); x.SetImg(0);
y.SetReal(0); y.SetImg(1);
z = x + y; z.print(); cout << endl;
z = x - y; z.print(); cout << endl;
z = x * y; z.print(); cout << endl;
z = x / y; z.print(); cout << endl;
return 0;
}
#8
这个比较实用些。。。
#9
这个比较实用些。。。
#10
标准库里有complex类型。
http://msdn.microsoft.com/en-us/library/0352zzhd.aspx
http://msdn.microsoft.com/en-us/library/0352zzhd.aspx