第九周实验报告2

/* (程序头部注释开始) 
* 程序的版权和版本声明部分 
* Copyright (c) 2012, 烟台大学计算机学院学生  
* All rights reserved. 
* 文件名称:流运算符重载运算符(Complex类) 
 
* 作 者: 刘程程 
* 完成日期: 2012 年 4 月 17日 
* 版 本 号: 1.054 
* 对任务及求解方法的描述部分 
*
* 问题描 述:按第八周任务1,定义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-();    
    friend ostream& operator << (ostream& output,Complex& c);  
    friend istream& operator >> (istream& input,Complex& c);  
    friend Complex operator+(Complex &c1, Complex &c2);    
    friend Complex operator+(double d1, Complex &c2);    
    friend Complex operator+(Complex &c1, double d2);    
    friend Complex operator-(Complex &c1, Complex &c2);    
    friend Complex operator-(double d1, Complex &c2);    
    friend Complex operator-(Complex &c1, double d2);    
    friend Complex operator*(Complex &c1, Complex &c2);    
    friend Complex operator*(double d1, Complex &c2);    
    friend Complex operator*(Complex &c1, double d2);    
    friend Complex operator/(Complex &c1, Complex &c2);    
    friend Complex operator/(double d1, Complex &c2);    
    friend Complex operator/(Complex &c1, double d2);    
       
private:    
    double real;    
    double imag;    
};    
//实现输出的运算符重载
ostream& operator << (ostream& output,Complex& c)
{
output<<"("<<c.real;
if(c.imag>=0) output<<"+";
output<<c.imag<<"i)";
return output;
}

//实现输入的运算符重载
istream& operator >> (istream& input,Complex& c)
{
  int a,b;
  char sign,i;
  do
{
  cout<<"input a complex number(a+bi或a-bi):";
  input>>a>>sign>>b>>i;
}
while(!((sign=='+'||sign=='-')&&i=='i'));
c.real=a;
c.imag=(sign=='+')?b:-b;
return input;
}
Complex Complex::operator-()    
{    
    return(0-*this); //需要好好理解    
}    
    
//复数相加:(a+bi)+(c+di)=(a+c)+(b+d)i.      
Complex operator+(Complex &c1, Complex &c2)    
{    
    Complex c;    
    c.real=c1.real+c2.real;    
    c.imag=c1.imag+c2.imag;    
    return c;    
}    
Complex operator+(double d1, Complex &c2)  
{  
 Complex c(d1,0);  
 return c+c2;//按一般的运算有点麻烦,这种运算节省编写者的时间,但可能机器效率会不佳。。。   
}  
Complex operator+(Complex &c1, double d2)  
{  
 Complex c(d2,0);  
 return c1+c;  
}  
//复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i.     
Complex operator-(Complex &c1, Complex &c2)    
{    
    Complex c;    
    c.real=c1.real-c2.real;    
    c.imag=c1.imag-c2.imag;    
    return c;    
}    
Complex operator-(double d1, Complex &c2)    
{    
    Complex c(d1,0);    
    return c-c2;      
}    
Complex operator-(Complex &c1, double d2)    
{    
    Complex c(d2,0);    
    return c1-c;    
}    
//复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.     
Complex operator*(Complex &c1, Complex &c2)    
{    
    Complex c;    
    c.real=c1.real*c2.real-c1.imag*c2.imag;    
    c.imag=c1.imag*c2.real+c1.real*c2.imag;    
    return c;    
}    
Complex operator*(double d1, Complex &c2)    
{    
    Complex c(d1,0);    
    return c*c2;    
}    
Complex operator*(Complex &c1, double d2)    
{    
    Complex c(d2,0);    
    return c1*c; //注意位置    
}    
//复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i      
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 d1, Complex &c2)    
{    
    Complex c(d1,0);    
    return c/c2;    
}    
Complex operator/(Complex &c1, double d2)    
{    
    Complex c(d2,0);    
    return c1/c;    
}    
int main()    
{    
    Complex c1,c2,c3;    
    double d=11;    
    cout<<"c1:"; 
    cin>>c2;    
    cout<<"c2="; 
    cin>>c2;
    cout<<"d="<<d<<endl;    
    cout<<"-c1="<<(-c1);   
    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="<<(c1*c2)<<endl;   
    cout<<"c1*d="<<(c1*d)<<endl;   
    cout<<"d*c1="<<(d*c1)<<endl;  
    c3=c1/c2;    
    cout<<"c1/c2="<<(c1/c2)<<endl;   
    cout<<"c1/d="<<(c1/d)<<endl;   
    cout<<"d/c1="<<(d/c1)<<endl; 
    
    system("pause");    
    return 0;    
}    



 第九周实验报告2_第1张图片

你可能感兴趣的:(c,BI,input,任务,iostream,output)