• 定义一个Complex类，为其定义构造函数，析构函数，试对下列几个运算符进行重载：++，=，！=，+，-，==，其中要求要有成员重载形式和友元重载形式，而且，++运算符要求实现先加和后加两种形式。 #include<...
定义一个Complex类，为其定义构造函数，析构函数，试对下列几个运算符进行重载：++，=，！=，+，-，==，其中要求要有成员重载形式和友元重载形式，而且，++运算符要求实现先加和后加两种形式。

#include<iostream>
using namespace std;

class Complex
{
public:

Complex()
{
real = 0;
imag = 0;
}
Complex(double r, double i)
{
real = r;
imag = i;
}
Complex operator +(Complex &c2);
Complex operator -(Complex &c2);
Complex operator ++();
Complex operator ++(int);
friend int operator ==(Complex &c1, Complex &c2);
friend int operator !=(Complex &c1, Complex &c2);
Complex operator =(Complex &c2);

~Complex()
{
cout << "Destructor called!" << endl;
}
double real;
double imag;
};

Complex Complex::operator++()//重载++前缀运算符
{
++real;
++imag;
return *this;
}

Complex Complex::operator++(int) //重载++后缀运算符
{
real++;
imag++;
return *this;
}

int operator ==(Complex &c1, Complex &c2)
{
if(c1.real == c2.real && c1.imag == c2.imag)
{
return 1;
}
else
{
return 0;
}

}

int operator !=(Complex &c1, Complex &c2)
{
if(c1.real != c2.real || c1.imag != c2.imag)
{
return 1;
}
else
{
return 0;
}
}

Complex Complex::operator+(Complex &c2)
{
Complex c3;
c3.real = real + c2.real;
c3.imag = imag + c2.imag;
return c3;
}

Complex Complex::operator-(Complex &c2)
{
Complex c3;
c3.real = real - c2.real;
c3.imag = imag - c2.imag;
return c3;
}

Complex Complex::operator =(Complex &c2)
{
if(this == &c2)
{
return *this;
}
real = c2.real;
imag = c2.imag;
return *this;
}

int main(void)
{
Complex c(1,2), c1(1,2), c2(2,1), c3 = c1+c2, c4=c2-c1;
cout << "下面进行判断是否相等" << endl;
if(c == c1)
{
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
cout << "c1 = " << c1.real << "+" << c1.imag << "i" << endl;
cout << "c == c1" << endl;
cout << "判断相等成功" << endl;

}

cout << "下面进行判断是否相等" << endl;
if(c != c2)
{
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
cout << "c2 = " << c2.real << "+" << c2.imag << "i" << endl;
cout << "c != c2" << endl;
cout << "判断不相等成功" << endl;

}

cout << "进行赋值运算" << endl;
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
cout << "c2 = " << c2.real << "+" << c2.imag << "i" << endl;
c = c2;
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
cout << "c2 = " << c2.real << "+" << c2.imag << "i" << endl;
cout << "赋值成功" << endl;

cout << "下面进行++后缀运算" << endl;
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
c++;
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
cout << "++后缀运算成功" << endl;

cout << "下面进行++前缀运算" << endl;
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
++c;
cout << "c = " << c.real << "+" << c.imag << "i" << endl;
cout << "++前缀运算成功" << endl;

cout << "下面进行+运算符重载" << endl;
//c3 = c1 + c2;
cout << "c1 + c2 = " << c3.real << "+" << c3.imag << "i" << endl;
cout << "进行+运算符重载成功" << endl;

cout << "下面进行-运算符重载" << endl;
//c3 = c1 - c2;
cout << "c2 - c1 = " << c4.real << "+" << c4.imag << "i" << endl;
cout << "进行-运算符重载成功" << endl;

return 0;
}


展开全文
• #include<iostream> #include<math.h> using namespace std; class Complex{ public: ... Complex(double real0=0,double imaginary0=0);... Complex(Complex &p); void add(Comple...


#include<iostream>
#include<math.h>
using namespace std;
class Complex{
public:
Complex(double real0=0,double imaginary0=0);
Complex(Complex &p);
void show();
double mod();
private:
double real;
double imaginary;
};
Complex::Complex(double real0,double imaginary0)
{
real=real0;
imaginary=imaginary0;
}
Complex::Complex(Complex &p)
{
real=p.real;
imaginary=p.imaginary;
}
{
real=real+b.real;
imaginary=b.imaginary;
}
void Complex::show()
{
cout<<real<<"+"<<imaginary<<"i"<<endl;
}
double Complex::mod()
{
double x;
x=sqrt(real*real+imaginary*imaginary);
return x;
}
int main()
{Complex c1(3,5);
Complex c2(4.5);
Complex c3(c1);
c3.show();
cout<<c1.mod()<<endl;
return 0;
}

Complex

刚开始没定义初始值，结果怎么都出不来，请同学帮我看了一下，问题就解决了。反思了一下自己，基本知识还是没掌握牢固。还是要打好基础，一步一步来。

转载于:https://www.cnblogs.com/lyc1103/p/10633319.html
展开全文
• 题目内容：定义复数Complex,并使用成员函数add实现复数加法，并输出...请定义Complex类。 #include using namespace std; int main() { int r1,r2,i1,i2; cin>>r1>>i1>>r2>>i2; Complex a
题目内容：定义复数类Complex,并使用成员函数add实现复数加法，并输出结果。注意虚部为负数时，要写成3-4i,不能写成3±4i,如果实部或者虚部为0，则可以不输出，例如：4i不能输出为0+4i,而是直接输出4i,4不能输出为4+0i，而是直接输出4。
请定义Complex类。 #include using namespace std; int main() { int r1,r2,i1,i2; cin>>r1>>i1>>r2>>i2; Complex a(r1,i1),b(r2,i2),c; c=a.add(b);//c=a+b c.show(); return 0; }
输入格式：实部和虚部
输出格式：见输出样例
输入样例：3 4 5 6
输出样例：8+10i
#include<iostream>
using namespace std;
class Complex{
public:
double R,I;
void show();
Complex(double r=0,double i=0)
{
R=r;
I=i;
}
{
Complex c;
c.R=R+b.R;
c.I=I+b.I;
return c;
}
};
void Complex::show(){
if(R!=0)
{
if(I>0)
cout<<R<<"+"<<I<<"i"<<endl;
else if(I==0)
cout<<R<<endl;
else
cout<<R<<"-"<<-I<<"i"<<endl;
}
else if(I!=0)
cout<<I<<"i"<<endl;
}
int main()
{
int r1,r2,i1,i2;
cin>>r1>>i1>>r2>>i2;
Complex a(r1,i1),b(r2,i2),c;
c.show();
system("pause");
return 0;
}
展开全文
• 关于complex类定义：#include using namespace std; class complex { public: complex() {real=0;im=0;} complex(float a,float b):real(a),im(b){} friend complex operat
关于complex类的定义：
#include<iostream>
using namespace std;
class complex
{
public:
complex()
{real=0;im=0;}
complex(float a,float b):real(a),im(b){}
friend complex operator+(complex& a,complex& b);
friend complex operator-(complex& a,complex& b);
friend complex operator*(complex& a,complex& b);
friend complex operator/(complex& a,complex& b);
friend istream& operator>> (istream&,complex&);
friend ostream& operator<< (ostream& output,complex& a);
void display();

private:
float real;
float im;
};

void complex::display()
{

if(real<0.1&&real>-0.1)
real = 0;
if(im<0.1&&im>-0.1)
im=0;
if(im==0)
{   cout.setf(ios::fixed);
cout.precision(1);
cout<<real;}
else if(real==0.0&&im!=0)
{
cout.setf(ios::fixed);
cout.precision(1);
cout<<im<<'i';
}
else
{
if(im>0)
{cout.setf(ios::fixed);
cout.precision(1);

cout<<real<<'+'<<im<<'i';}
else
{
cout.setf(ios::fixed);
cout.precision(1);

cout<<real<<im<<'i';
}
}
}
complex operator+(complex& a,complex& b)
{
complex ans;
ans.real = a.real+b.real;
ans.im = a.im+b.im;
return ans;
}
complex operator-(complex& a,complex& b)
{
complex ans;
ans.real = a.real-b.real;
ans.im = a.im-b.im;
return ans;
}
complex operator*(complex& a,complex& b)
{
complex ans;
ans.real = a.real*b.real-a.im*b.im;
ans.im = a.real*b.im+a.im*b.real;
return ans;
}
complex operator/(complex& a,complex& b)
{
complex ans;
ans.real = (a.real*b.real+a.im*b.im)/(b.real*b.real+b.im*b.im);
ans.im =  (a.im*b.real-a.real*b.im)/(b.real*b.real+b.im*b.im);
return ans;
}
istream& operator>> (istream& input,complex& a)
{
input>>a.real>>a.im;
return input;
}
ostream& operator<< (ostream& output,complex& a)
{
output<<'('<<a.real<<','<<a.im<<')';
return output;
}
int main()
{

complex a,b,ans1,ans2,ans3,ans4;
cin>>a>>b;
ans1=a+b;
ans2=a-b;
ans3=a*b;
ans4=a/b;

return 0;
}
展开全文
• #include using namespace std;...class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display();
• 头文件的布局 #ifndef __COMPLEX__ #define __COMPLEX__ class complex .........的两种形式 ...定义分为有指针成员的和无指针...下面介绍complex类实现无指针成员定义，string实现有指针成员定义...
• 定义一个复数类Complex，使下面的代码能够工作： Complex c1(3, 5); // 用复数3+5i初始化c1 Complex c2=4.5; // 用实数4.5初始化c2 c1.add(c2); // 将C1与c2相加，结果保存在c1中 c1.show(); //...
• #include using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;... Complex operator+(Complex &c2);... Complex operator-(Complex
• 定义一个复数类Complex，重载运算符"+".使之能用于复数的加法运算，将运算符函数重载为非成员、非友员的普通函数。编写程序求2个复数之和。 #include<iostream> usingnamespacestd; classComplex { public:...
• 定义一个复数类Complex，重载运算符“+”，使之能用于复数的加法运算。参加运算的两个运算符可以都是对象。也可以其中一个是整数，顺序任意。例如：c1+c2,i+c1,c1+i 均合法（设i为整数，c1 c2为复数）。编程序 ...
• 【问题描述】自定义一个复数类型Complex，其中含有若干成员函数，使用该可以完成复数的加法以及对复数的输出。请完成类定义，并编制主函数，说明Complex类对象，对定义的各成员函数进行调用。
• 定义一个复数 complex ，它的内部具有两个实例变量：realPart和imagPart，分别代表复数的实部和虚 部，编程实现要求的数学运算。 （1）实现两个复数相加。复数加运算的原则是：复数的实部和虚部分别相加。 （2...
• 1.public class ComplexTester{ 3. public static void main(String[] args) 4. { 5. Complex z1=new Complex(1,2); 6. Complex z2=new Complex(3,4); 7. System.out.println(z1+"+"+z2+"="+z1
• 1、定义一个复数类Complex,使得下面的代码能够工作。 Complex c1(3,5); Complex c2=4.5; c1.add(c2); c1.show(); #include <iostream> using namespace std; class Complex { public: Complex(float i, float...
• 定义一个复数类Complex，使得下面的代码能够工作。（注：下列代码需放在主函数中。） Complex c1(3,5); //用复数3+5i初始化c1 Complex c2=4.5; //用实数4.5初始化c2 c1.add(c2); //将c1与c2相加，结果保存在c1中 ...
• 定义复数类Complex，实现复数的+、-、*、/数学运算，=、+=、-=、*=、/=赋值计算，++a、a++、--a、a—自增自减，==、!=关系运算
• 定义一个复数类complex，包含两个属性：实部和虚部，包含构造函数，包含成员函数void display（）输出，包括成员函数complex add(complex a), 进行复数加法，在主函数种定义两个复数对象，进行复数之间的加法运算。...
• C++基础编程题（36）定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
• 定义一个复数类Complex，重载运算符“+”，使之能用于复数的加法运算。参加运算的两个运算量可以都是对象，也可以其中有一个是整数，顺序任意。例如：c1+c2，i+c1，c1+i均合法（设i为整数，c1，c2位复数）。编程序...
• 定义一个复数类Complex，重载运算符“＋”，“—”，使之能用于复数的加，减运算， 运算符重载函数作为Complex类的成员函数。编程序，分别求出两个复数之和，差。初值自拟 注意：重载函数要作为的成员函数 #...
• 习题 10.2 定义一个复数类Complex，重载运算符+，-，*，/，使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编程序，分别求两个复数之和、差、积和商。 代码块： #include &amp;lt...
• 定义一个复数类Complex，重载运算符“+”、“-”，“*”，“/”使之能用与复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编写程序，分别求两个复数之和，差，积和商。 #include<iostream> ...
• #include using namespace std; class Complex {public: Complex(){real=0;...Complex(double r,double i){real=r;...Complex operator+(Complex &c2); Complex operator-(Complex &c2); Comple
• 题目：设计一个复数类complex,分别将复数的实部和虚部作为属性，定义三个构造方法(没有参数时默认为实部和虚部都为0;一个参数时默认为实数，即虚部为0,两个参数时分别为实部和虚部),并定义两个成员方法计算两个复数...
• 定义一个复数类Complex，重载运算符“+”,“-”,“*”,“/”，使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编写程序，分别求两个复数之和、差、积和商。 Input 两个复数 Output 两个...
• class Fushu{ public static void main(String[] args){ ... Complex complex1=new Complex(5,6); Complex complex2=new Complex(6,2); System.out.println(complex1+"+("+complex2+")="+Complex.addit
• /* * 程序的版权和版本声明部分 ...* 文件名称：定义COMPLEX类，实现分数的输入输出，改造源程序中对运算结果显示方式，使程序读起来更自然 * 作 者： 张启立  * 完成日期： 2012年 4 月 16 日
• #include <iostream> #include <cmath> using namespace std; class com { public: com(){a = 0;} com(int x):a(x){}; friend com operator + (com &c1, com &c2);......

...