#include
//定义结构体变量类型
struct fushu
{
int sb; //存储复数实部
int xb; //存储复数虚部
//创建fhshu结构体类型默认值(又称结构体类型构造函数)
fushu()
{
sb = 0;
xb = 0;
}
//创建fhshu结构体类型初始化函数(又称结构体类型构造函数)
fushu(int s,int x)
{
sb = s;
xb = x;
}
//复数的赋值操作函数实现
void fushu_fuzhi(const fushu &complex1)
{
this->sb = complex1.sb;
this->xb = complex1.xb;
}
//两复数据相加函数实现
fushu fushu_add(const fushu &complex1,const fushu &complex2)
{
fushu complexFlag;
complexFlag.sb = complex1.sb + complex2.sb;
complexFlag.xb = complex1.xb + complex2.xb;
return complexFlag;
}
//两复数据相减函数实现
fushu fushu_subtraction(const fushu &complex1,const fushu &complex2)
{
fushu complexFlag;
complexFlag.sb = complex1.sb - complex2.sb;
complexFlag.xb = complex1.xb - complex2.xb;
return complexFlag;
}
//两复数据相乘函数实现
fushu fushu_multiplication(const fushu &complex1,const fushu &complex2)
{
//(a+bi)(c+di)=(ac-bd)+(bc+ad)i
fushu complexFlag;
complexFlag.sb = ((complex1.sb*complex2.sb) - (complex1.xb*complex2.xb));
complexFlag.xb = ((complex1.xb*complex2.sb) + (complex1.sb*complex2.xb));
return complexFlag;
}
//两复数据相除函数实现
fushu fushu_division(const fushu &complex1,const fushu &complex2)
{
//:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i
//由于时间问题,此除没有考虑除数为零的情况
fushu complexFlag;
complexFlag.sb = ((complex1.sb*complex2.sb) + (complex1.xb*complex2.xb)) / (complex2.sb*complex2.sb + complex2.xb*complex2.xb);
complexFlag.xb = ((complex1.xb*complex2.sb) + (complex1.sb*complex2.xb)) / ((complex2.sb*complex2.sb) + (complex2.xb*complex2.xb));
return complexFlag;
}
//输出相加后的复数数据
void outputfushu()
{
printf("复数为:==>(%d + %di)\n",this->sb,this->xb);
}
};
//typedef重定义结构体类型变量
typedef struct fushu COMPLEXTYPE;
int main()
{
/* 定义两个结构体变量*/
COMPLEXTYPE complex1(6,2),complex2(3,4);
COMPLEXTYPE complex_; //定义存储相加后的值
//加法操作
complex_.fushu_fuzhi(complex_.fushu_add(complex1,complex2));
printf("加法操作:");
complex_.outputfushu();
//减法操作
complex_.fushu_fuzhi(complex_.fushu_subtraction(complex1,complex2));
printf("减法操作:");
complex_.outputfushu();
//乘法操作
complex_.fushu_fuzhi(complex_.fushu_multiplication(complex1,complex2));
printf("乘法操作:");
complex_.outputfushu();
//除法操作
complex_.fushu_fuzhi(complex_.fushu_division(complex1,complex2));
printf("除法操作:");
complex_.outputfushu();
return 0;
}
//已经经过vc6.0调试了,是可以的,希望对你有所帮助!
那个复数的赋值函数中,this是什么意思