A. c語言定義復數
#include <complex>
#include <iostream>
using namespace std;void main()
{
complex<double> comp(1,1); cout << "The complex number = " << comp << endl; cout<<"real:"<<comp.real()<<endl;
cout<<"image:"<<comp.imag()<<endl; comp=conj(comp);
cout << "The complex number = " << comp << endl;
cout<<"real:"<<comp.real()<<endl;
cout<<"image:"<<comp.imag()<<endl;
}
B. C語言 定義一個能夠表示復數的結構類型,一個復數包括實數與虛數兩個部分
struct complex{
double r;
double i;
}
C. c語言中如何定義一個復數型數組
首先加入頭文件:
#include
<complex>
然後進行復數類型定義:
typedef
complex<float>
Comp;//這里double也可
這樣就可以使用復數結構了
如:聲明復數數組
Comp
a[5];
D. 試用C語言的結構類型定義表示復數的抽象數據類型
#include <stdio.h>
//結構體
typedef struct
{
double a;
double b;
} COMPLEX;
//加
COMPLEX add (COMPLEX a, COMPLEX b)
{
a.a += b.a;
a.b += b.b;
return a;
}
//減
COMPLEX subtract (COMPLEX a, COMPLEX b)
{
a.a -= b.a;
a.b -= b.b;
return a;
}
//乘
COMPLEX multiply (COMPLEX a, COMPLEX b)
{
COMPLEX c;
c.a = a.a*b.a - a.b*b.b;
c.b = a.a*b.b + a.b*b.a;
return c;
}
//除
COMPLEX divide (COMPLEX a, COMPLEX b) {
COMPLEX c;
double divisor = b.a*b.a + b.b*b.b;
if (divisor - 0 == 0)
exit (0);
c.a = (a.a*b.a + a.b*b.b) / divisor;
c.b = (a.b*b.a - a.a*b.b) / divisor;
return c;
}
然後讀入數據,判斷運算符號就行了。