定义复数类Complex,构造两个对象,并调用成员函数对它们进行加减乘除运算.
来源:学生作业帮助网 编辑:作业帮 时间:2024/08/29 12:20:31
定义复数类Complex,构造两个对象,并调用成员函数对它们进行加减乘除运算.
定义复数类Complex,构造两个对象,并调用成员函数对它们进行加减乘除运算.
定义复数类Complex,构造两个对象,并调用成员函数对它们进行加减乘除运算.
//定义复数类,构造两个对象,并调用成员函数对它们进行加减乘除
#include "iostream.h"
class Complex
{
private :
double real;
double imag;
public :
void set();
void add(Complex a,Complex b); //+
void minus(Complex a,Complex b); //-
void multiply(Complex a,Complex b); //*
void divide(Complex a,Complex b); //÷
bool judgeZero();
void show();
};
void Complex::add(Complex a,Complex b) //+
{
real=a.real+b.real;
imag=a.imag+b.imag;
}
void Complex::minus(Complex a,Complex b) //-
{
real=a.real-b.real;
imag=a.imag-b.imag;
}
void Complex::multiply(Complex a,Complex b) //*
{
real=a.real*b.real - a.imag*b.imag;
imag=a.real*b.imag + a.imag*b.real;
}
void Complex::divide(Complex a,Complex b) //÷
{
double p=b.real*b.real + b.imag*b.imag;
real=(a.real*b.real + a.imag*b.imag) / p;
imag=(a.imag*b.real - a.real*b.imag) / p;
}
void Complex::set()
{
char ch1,ch2;
cin>>real>>ch1>>imag>>ch2;
if(ch1=='-') imag=-imag;
}
void Complex::show()
{
if(real==0)
{
if(imag==0) cout