第九周实验报告(任务1)

来源:互联网 发布:淘宝网化妆品现状分析 编辑:程序博客网 时间:2024/05/16 16:56

程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 复数运算符重载  
* 作    者:     郭广建                       
* 完成日期:  2012年    04  月   22   日
* 版 本 号:  1.0

源程序:

#include<iostream>using namespace std;class Complex{public:    Complex(){real=0;imag=0;}    Complex(double r,double i){real=r;imag=i;}friend istream &operator>>(istream &input,Complex &c2);friend ostream &operator<<(ostream &output,Complex &c2);    friend Complex operator+(double d,Complex &c2);    friend Complex operator-(double d,Complex &c2);    friend Complex operator*(double d,Complex &c2);    friend Complex operator/(double d,Complex &c2);    friend Complex operator+(Complex &c2,double d);    friend Complex operator-(Complex &c2,double d);    friend Complex operator*(Complex &c2,double d);    friend Complex operator/(Complex &c2,double d);    void display();private:    double real;    double imag;};//下面定义成员函数istream &operator>>(istream &input,Complex &c2){input >>c2.real >> c2.imag ;return input;}ostream &operator<<(ostream &output,Complex &c2){output << '(' << c2.real << ',' << c2.imag <<')' <<endl;return output;}Complex operator+(double d, Complex &c2){return Complex(d + c2.real , c2.imag);}Complex operator-(double d, Complex &c2){return Complex(d - c2.real , c2.imag);}Complex operator*(double d, Complex &c2){return Complex( d * c2.real ,d * c2.imag);}Complex operator/(double d, Complex &c2){return Complex(d / c2.real ,d / c2.imag);}Complex operator+(Complex &c2,double d){return Complex(c2.real+d, c2.imag);}Complex operator-(Complex &c2,double d){return Complex(c2.real-d, c2.imag);}Complex operator*(Complex &c2,double d){return Complex(c2.real*d ,c2.imag*d);}Complex operator/(Complex &c2,double d){    return Complex(c2.real/d , c2.imag/d);}void Complex::display(){cout << '(' << real << ',' << imag << ')' <<endl;} int main(){    double d = 3.14;    Complex c1,c2,c3;cin >> c1 >> c2 ;    cout<<"c1=" << c1;    cout<<"c2=" << c2;    c3=d+c1;    cout<<"d+c1";    cout << c3;    c3=d-c1;    cout<<"d-c1";    c3.display();    c3=d*c1;    cout<<"d*c1";    c3.display();    c3=d/c1;    cout<<"d/c2=";    c3.display();c3=c2+d;    cout<<"c2+d=";    c3.display();    c3=c2-d;    cout<<"c2-d=";    c3.display();    c3=c2*d;    cout<<"c2*d=";    c3.display();    c3=c2/d;    cout<<"c2/d=";    c3.display();      system("pause");    return 0;}


运行结果:

知识积累: 综合到任务3
原创粉丝点击