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;
}
然后读入数据,判断运算符号就行了。