/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: Triangle.cpp
* 作 者: 计114-4 刘向一
* 完成日期: 2012 年 4月17日
* 版 本 号: V 1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: Triangle.cpp
* 作 者: 计114-4 刘向一
* 完成日期: 2012 年 4月17日
* 版 本 号: V 1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include<iostream.h>
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 &c2);
Complex operator/(Complex &c2);
friend ostream& operator << (ostream&,Complex&);
friend istream& operator >> (istream&,Complex&);
private:
double real;
double imag;
};
//下面定义成员函数
istream& operator >> (istream& input,Complex& c)
{
input>>c.real>>c.imag;
return input;
}
ostream &operator << (ostream &output,Complex &c)
{
output<<"("<<c.real<<" "<<c.imag<<"i)"<<endl;
return output;
}
//复数相加: (a+bi)+(c+di)=(a+c)+(b+d)i.
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.
Complex Complex::operator*(Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i
Complex Complex::operator/(Complex &c2)
{
Complex c;
double d=c2.real*c2.real+c2.imag*c2.imag;
c.real=(real*c2.real+imag*c2.imag)/d;
c.imag=(imag*c2.real-real*c2.imag)/d;
return c;
}
int main()
{
Complex c1,c2,c3;
cout<<"请输入复数c1的值:(以a b的形式输入)";
cin>>c1;
cout<<"请输入复数c2的值:(以a b的形式输入)";
cin>>c2;
cout<<"c1=";
cout<<c1;
cout<<"c2=";
cout<<c2;
c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;
c3=c1-c2;
cout<<"c1-c2=";
cout<<c3;
c3=c1*c2;
cout<<"c1*c2=";
cout<<c3;
c3=c1/c2;
cout<<"c1/c2=";
cout<<c3;
return 0;
}