一道复数类C++的题帮忙!!!!!!!!!!

编写一个复数类,使用成员函数或友元函数,重载运算符
" + ","-"," * "," / "。

怎么做??谢谢大家了
还有它的理论和数据结论是什么呢?

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

#define PRECISION 0.0000000000000001 //要求精度
//我们知道float和double不能直接和0比较
//float的精度只有6位,double是15到16位

class CComplex{
public:
//构造函数
CComplex(double realPart_value = 0.0, double imagePart_value = 0.0)
{
realPart = realPart_value;
imagePart = imagePart_value;
}

//打印函数
void print() const
{
cout<<"RealPart:"<<realPart<<" ImagePart:"<<imagePart<<endl;
}

//重载‘+’
CComplex operator +(CComplex &c); //两个复数相加
CComplex operator +(double r); //复数和一个实数相加

//重载'-'
CComplex operator -(CComplex &c); //同上
CComplex operator -(double r);

//重载'*'
CComplex operator *(CComplex &c);
CComplex operator *(double r);

//重载'/'
CComplex operator /(CComplex &c);
CComplex operator /(double r);

private:
double realPart; //实部
double imagePart; //虚部

};

CComplex CComplex::operator +(CComplex &c)
{
CComplex temp;
temp.realPart = realPart + c.realPart;
temp.imagePart =imagePart + c.imagePart;
return temp;
}

CComplex CComplex::operator +(double c)
{
CComplex temp;
temp.realPart = realPart + c;
temp.imagePart = imagePart;
return temp;
}

CComplex CComplex::operator -(CComplex &c)
{
CComplex temp;
temp.realPart = realPart - c.realPart;
temp.imagePart =imagePart - c.imagePart;
return temp;
}

CComplex CComplex::operator -(double c)
{
CComplex temp;
temp.realPart = realPart - c;
temp.imagePart = imagePart;
return temp;
}

CComplex CComplex::operator *(CComplex &c)
{
CComplex temp;
temp.realPart = realPart * c.realPart - imagePart * c.imagePart;
temp.imagePart =realPart * c.imagePart + imagePart * c.realPart;
return temp;
}

CComplex CComplex::operator *(double c)
{
CComplex temp;
temp.realPart = realPart * c;
temp.imagePart = imagePart * c;
return temp;
}

CComplex CComplex::operator /(CComplex &c)
{
if((c.realPart >(-PRECISION) && c.realPart < PRECISION)
&& (c.imagePart > (-PRECISION) && c.imagePart < PRECISION))
{
cout<<"The divisor can not be zero!"<<endl;
exit(-1);
}
double div = c.realPart * c.realPart + c.imagePart * c.imagePart; //分母先提出来
CComplex temp;
temp.realPart = (realPart * c.realPart + imagePart * c.imagePart) / div;
temp.imagePart = (imagePart * c.realPart - realPart * c.imagePart) / div;
return temp;
}

CComplex CComplex::operator /(double c)
{
if(c >(-PRECISION) && c < PRECISION ) //被除数不能为零,其实太小了也不行,我们否则会溢出
{
cout<<"The divisor can not be zero!"<<endl;
exit(-1);
}
CComplex temp;
temp.realPart = realPart / c;
temp.imagePart = imagePart / c;
return temp;
}

//主函数和先关测试
int main(int argc, char* argv[])
{

CComplex c1(10.0, 10.0);
CComplex c2(10.0, 10.0);
c1.print();
c2.print();

CComplex temp(0, 0);
//加 ‘+’
temp = c1 + c2;
temp.print();

temp = c1 + 10;
temp.print();

//减 ‘-’
temp = c1 - c2;
temp.print();

temp = c1 - 10;
temp.print();

//乘 ‘*’
temp = c1 * c2;
temp.print();

temp = c1 * 10;
temp.print();

//除 ‘/’
temp = c1 / c2;
temp.print();

temp = c1 / 10;
temp.print();
return 0;
}
温馨提示:内容为网友见解,仅供参考
无其他回答

一道复数类C++的题帮忙!!!
CComplex operator +(CComplex &c); \/\/两个复数相加 CComplex operator +(double r); \/\/复数和一个实数相加 \/\/重载'-'CComplex operator -(CComplex &c); \/\/同上 CComplex operator -(double r);\/\/重载'*'CComplex operator *(CComplex &c);CComplex operator *(double r);\/...

C++ 一个复数类,运算符重载 + ,实现复数和复数的相加。
\/\/实数 float b;\/\/虚数};Complex& Complex::operator+(Complex &x){ this->a += x.a; this->b += x.b; return *this;}int main(){ Complex com1(3,2),com2(2,1),com3(3,0),com4(0,2),com5(0,0); Complex com; com = com1+com2+com3+com4+...

C++编程题 定义并实现一个复数类Complex,使得下面的代码能够工作_百度...
cout<<"复数的虚部为:"<<imaginary<<endl;}void Complex::out_size(){ cout<<"该复数的模为:"<<sqrt(real*real+imaginary*imaginary)<<endl;}void Complex::out_view(){ cout<<"该复数的辐角为:"<<atan(real\/imaginary)<<endl; \/\/修改后的 }\/\/void Complex::out_it(float a,fl...

c++编程问题,其中一部分是求复数的共轭,
还有你那个输出复数的值时,会出现+-xx现象,可以在输出前加一个判断的!!对不同情形进行讨论,这样结果会更好看的!!

C++ 定义一个复数类 求和,求积,求商,求差
class CComplex { private:double real;double imaginary;public:CComplex(double re=0.0,double im=0.0):real(re),imaginary(im){} \/\/因为本类中没有任何动态内存分配问题,所以,编译器自动提供的默认拷贝构造函数也能正确的完成工作,因而 \/\/不需另外定义。\/\/加法运算符重载:CComplex operator+...

c++中 输入一个复数的实部和虚部 输出这个复数?
按照题目要求输入一个复数的实部和虚部,输出这个复数的C++程序如下 include<iostream> using namespace std;class complex{ double real,imag;public:complex(double real,double imag):real(real),imag(imag){} friend ostream &operator<<(ostream &out,complex &obj){ if(obj.imag==0)out<<obj....

急!C++定义一个复数类Complex,使下面的代码能够工作?
include <iostream> using namespace std;class Complex { private:double re;double im;public:Complex(double re,double im=0){ this->re = re;this->im = im;} void add(const Complex& c){ this->re += c.re;this->im += c.im;} void sub(const Complex& c){ this->re -= ...

C++设计一个类 实现复数加减乘除的功能,?? 代码版的啊 最好能有注释...
public:int a1,b1,a2,b2;char temp[32];void add();void cut();void multiply();void divide();private:void init();};void calc::init(){ cout<<"请输入第一个数的实部:";cin>>a1;cout<<"请输入第一个数的虚部:";cin>>b1;cout<<"请输入第二个数的实部:";cin>>a2;cout<<"...

用C++编写一个复数计算器。为复数定义一个类,形式a+bi。a,b为double...
private:double real,imag;public:complex(int r=0.0,int i=0.0){real=r;imag=i;} ~complex(){} void input(){cin>>real>>imag;} void output(){cout<<real<<'+'<<imag<<'i'<<endl;} void set(double r,double i){real=r;imag=i;} double get_r(){return real;} double ...

C++的方式来做。看清楚题目是复矩阵!!网上的答案是矩阵。
include <stdlib.h> class Complex \/\/复数类 { public:double real;double image;Complex(double real=0,double image=0){ this->real=real;this->image=image;} Complex operator+(Complex c1);\/\/重载运算符+ };Complex Complex::operator+(Complex c1){ return Complex(real+c1.real,image+c...

相似回答
大家正在搜