第九周(运算符重载复数类)

/*

*copyright(c) 2015,烟台大学计算机学院

*All rights reserved。

*文件名称:第九周(运算符重载)

*作者:王忠

*完成日期:2015.5.13

*版本号:v1.0

*

*问题描述:定义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+(const Complex &c2);
    Complex operator-(const Complex &c2);
    Complex operator*(const Complex &c2);
    Complex operator/(const Complex &c2);
    friend Complex operator-(Complex &c2);
    void display();
    friend ostream& operator <<(ostream& output,const Complex& c);
    friend istream& operator >>(istream& input,Complex& c);
private:
    double real;
    double imag;
};
//下面定义成员函数
    Complex Complex::operator+(const Complex &c2)
    {
        Complex c;
        c.real=real+c2.real;
        c.imag=imag+c2.imag;
        return c;
    }
    Complex Complex::operator-(const Complex &c2)
    {
        Complex c;
        c.real=real-c2.real;
        c.imag=imag-c2.imag;
        return c;
    }
    Complex Complex::operator*(const Complex &c2)
    {
        Complex c;
        c.real=real*c2.real-imag*c2.imag;
        c.imag=imag*c2.real+real*c2.imag;
        return c;
    }
    Complex Complex::operator/(const Complex &c2)
    {
        Complex c;
        c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
        c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
        return c;
    }
void Complex::display()
{
    cout<<real;
    if(imag>0)
        cout<<'+'<<imag<<'i'<<endl;
    else
        cout<<imag<<'i'<<endl;
}
Complex operator-(Complex &c2)
{
    Complex c;
    return (c-c2);
}
ostream& operator <<(ostream& output,const 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+bi or a-bi)\n";
        input>>a>>sign>>b>>i;
    }while(!(sign=='+'||sign=='-')&&i=='i');
    c.real=a;
    c.imag=(sign=='+')?b:-b;
    return input;
}
//下面定义用于测试的main()函数
int main()
{
    Complex c1,c2,c3;
    cout<<"c1: "<<endl;;
    cin>>c1;
    cout<<"c2: "<<endl;
    cin>>c2;

    cout<<"c1=";
    c1.display();
    cout<<"c2=";
    c2.display();
    c3=c1+c2;
    cout<<"c1+c2=";
    c3.display();
    c3=c1-c2;
    cout<<"c1-c2=";
    c3.display();
    c3=c1*c2;
    cout<<"c1*c2=";
    c3.display();
    c3=c1/c2;
    cout<<"c1/c2=";
    c3.display();
    c3=-c3;
    c3.display();
    return 0;
}

又成功一个,耶

时间: 2024-07-29 03:47:44

第九周(运算符重载复数类)的相关文章

第九周项目一-复数类的中的运算符重载(续)

在复数类中的运算符重载基础上 (1)再定义一目运算符 -,-c相当于0-c. (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然 /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:赵嵩 * 文件:Demo.cpp * 完成时间:2015年05月16日 * 版本号:v1.0 */ #include <iostream> using

第八周-运算符重载-Time类中的运算符重载

代码如下: #include <iostream> using namespace std; class CTime { private: unsigned short int hour; //时 unsigned short int minute; //分 unsigned short int second; //秒 public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s); void display();

第九周(运算符重载时间类)

/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:第九周(运算符重载时间类) *作者:王忠 *完成日期:2015.5.13 *版本号:v1.0 * *问题描述:实现Time类中的运算符重载.定义对时间对象的自增和自减一目运算符 //一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator-

第九周(运算符重载分数类)

/* *copyright(c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:第九周(运算符重载分数类) *作者:王忠 *完成日期:2015.5.13*版本号:v1.0 * *问题描述:定义分数的一目运算+和-,分别代表分数取正和求反,将"按位取反运算符"~重载为分数的求倒数运算. *输入描述: *程序输出: #include <iostream> #include <Cmath> using namespace std

第九周项目1-复数类中的运算符重载(续)

在复数类中的运算符重载基础上 (1)再定义一目运算符 -,-c相当于0-c. (2)定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然. /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:Demo.cpp * 完成时间:2015年05月14日 * 版本号:v1.0 */ #include <iostream> using

第八周 项目一-复数类中的运算符重载(1)

问题: (1)请用类的成员函数,定义复数类重载运算符+.-.*./,使之能用于复数的加减乘除 class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r; imag=i;} Complex operator+(const Complex &c2); Complex operator-(const Complex &c2); Complex operator*(const Comple

第九周项目3-分数类中的运算符重载(续)

在分数类中的运算符重载基础上 (1)定义分数的一目运算+和-,分别代表分数取正和求反,将"按位取反运算符"~重载为分数的求倒数运算. (2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然. /* * Copyright (c) 2015,烟台大学计算机学院 * All right reserved. * 作者:邵帅 * 文件:Demo.cpp * 完成时间:2015年05月14日 * 版本号:v1.0 *

第九周项目二-Time类中的运算符重载(续)

<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">在Time类中的运算符重载基础上</span><br style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height:

第九周 项目二-Time类中的运算符重载(续)

在Time类中的运算符重载基础上 (1)定义对时间对象的自增和自减一目运算符 //一目运算符的重载 CTime operator++(int);//后置++,下一秒 CTime operator++();//前置++,下一秒,前置与后置返回值不一样 CTime operator--( int);//后置--,前一秒 CTime operator--();//前置--,前一秒 (2)定义Time类中的<<和>>运算符重载,实现时间的输入输出,改造原程序中对运算结果显示方式,使程序读起