/*
*Copyright (c) 2014,烟台大学计算机学院void change(int a[8][8]);
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2015年5月13日
*版本号:v1.0
*
*问题描述:利用运算符重载进行复数的大小比较和计算(重载插入流运算符“<<”和流提取运算符“>>”
*/
#include<iostream>
using namespace std;
class Complex
{
public:
Complex()
{
real=0;
imag=0;
}
Complex(double r,double i)
{
real=r;
imag=i;
}
friend Complex operator+(Complex &c1,Complex &c2);
friend Complex operator-(Complex &c1,Complex &c2);
friend Complex operator*(Complex &c1,Complex &c2);
friend Complex operator/(Complex &c1,Complex &c2);
friend Complex operator+(double &i,Complex &c2);
friend Complex operator-(double &i,Complex &c2);
friend Complex operator*(double &i,Complex &c2);
friend Complex operator/(double &i,Complex &c2);
friend Complex operator+(Complex &c1,double &i);
friend Complex operator-(Complex &c1,double &i);
friend Complex operator*(Complex &c1,double &i);
friend Complex operator/(Complex &c1,double &i);
Complex operator-();
//实现输入、输出的运算符重载
friend ostream& operator << (ostream& output, const Complex& c);
friend istream& operator >> (istream& input, Complex& c);
private:
double real;
double imag;
};
//下面定义成员函数
Complex Complex::operator-()
{
real=-real;
imag=-imag;
return *this;
}
//实现输入、输出的运算符重载
istream& operator >> (istream& input, Complex& c)
{
double a,b;
char x,y;
do
{
cout<<"输入复数按照a+bi或a-bi的形式:";
input>>a>>x>>b>>y;
}
while(!((x=='+'||x=='-')&&y=='i'));
c.real=a;
c.imag=(x=='+')?b:-b;
return input;
}
ostream& operator << (ostream& output, const Complex& c)
{
output<<"("<<c.real;
if(c.imag>=0) output<<"+";
output<<c.imag<<"i)";
return output;
}
Complex operator+(Complex &c1,Complex &c2)
{
return Complex (c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(Complex &c1,Complex &c2)
{
return Complex (c1.real-c2.real,c1.imag-c2.imag);
}
Complex operator*(Complex &c1,Complex &c2)
{
return Complex (c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
Complex operator/(Complex &c1,Complex &c2)
{
Complex c;
c.real=(c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
Complex operator+(double &i,Complex &c2)
{
return Complex (i+c2.real,c2.imag);
}
Complex operator+(Complex &c1,double &i)
{
return Complex (i+c1.real,c1.imag);
}
Complex operator-(double &i,Complex &c2)
{
return Complex (i-c2.real,c2.imag);
}
Complex operator-(Complex &c1,double &i)
{
return Complex (i-c1.real,c1.imag);
}
Complex operator*(double &i,Complex &c2)
{
return Complex (i*c2.real,i*c2.imag);
}
Complex operator*(Complex &c1,double &i)
{
return Complex (i*c1.real,i*c1.imag);
}
Complex operator/(double &i,Complex &c2)
{
return Complex (i*c2.real/(c2.real*c2.real+c2.imag*c2.imag),i*c2.imag/(c2.real*c2.real+c2.imag*c2.imag));
}
Complex operator/(Complex &c1,double &i)
{
return Complex (c1.real/i,c1.imag/i);
}
//下面定义用于测试的main()函数
int main()
{
Complex c1,c2,c3;
double d=11;
cin>>c1;
cout<<"c1="<<c1<<endl;
cin>>c2;
cout<<"c2="<<c2<<endl;
cout<<"d="<<d<<endl;
cout<<"-c1="<<(-c1)<<endl;
c3=c1+c2;
cout<<"c1+c2="<<c3<<endl;
cout<<"c1+d="<<(c1+d)<<endl;
cout<<"d+c1="<<(d+c1)<<endl;
c3=c1-c2;
cout<<"c1-c2="<<c3<<endl;
cout<<"c1-d="<<(c1-d)<<endl;
cout<<"d-c1="<<(d-c1)<<endl;
c3=c1*c2;
cout<<"c1*c2="<<c3<<endl;
cout<<"c1*d="<<(c1*d)<<endl;
cout<<"d*c1="<<(d*c1)<<endl;
c3=c1/c2;
cout<<"c1/c2="<<c3<<endl;
cout<<"c1/d="<<(c1/d)<<endl;
cout<<"d/c1="<<(d/c1)<<endl;
return 0;
}
多运用才能熟悉。