C++ 实验 使用重载运算符实现一个复数类

实验目的:

1.掌握用成员函数重载运算符的方法

2.掌握用友元函数重载运算符的方法

 

实验要求

1.定义一个复数类,描述一些必须的成员函数,如:构造函数,析构函数,赋值函数,返回数据成员值的函数等。

2.定义运算符重载函数,通过重载运算符:+-*/,直接实现二个复数之间的加减乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求乘法“+”,“*”用友元函数实现重载,除法“-”,“/”用成员函数实现重载,参数是复数或实数。

3.通过重载运算符:>><<=,直接实现复数的输入、输出及赋值运算,通过重载运算符:= =,!=直接实现复数的比较运算,编写一个完整的程序,测试重载运算符的正确性。

 

操作菜单可参考如下格式:

1输入复数

2查看输入的复数

3复数相加

4复数相减

5复数相乘

6复数相除

7输出结果

0退出

 

#include <iostream>
#include<cstdio>
using namespace std;
class Complex
{
public:
    Complex(double r = 0,double i = 0)//构造函数
    {
        real=r;
        imag=i;
    }
    ~Complex()
    {

    }
    friend Complex operator+(Complex &c1,Complex &c2);   //重载为友员函数
    friend Complex operator*(Complex &c1,Complex &c2);
    Complex operator -(Complex&);//重载为成员函数
    Complex operator /(Complex&);
    friend istream& operator>>(istream&, Complex&);
    friend ostream& operator<<(ostream&, Complex&);
    friend bool operator==(Complex &c1,Complex &c2);
    friend bool operator!=(Complex &c1,Complex &c2);
    void display( );
private:
    double real;
    double 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);
}
Complex Complex::operator-(Complex &c)
{
    return Complex(real-c.real,imag-c.imag);
}
Complex Complex::operator/(Complex &c)
{
    return Complex(real/c.real,imag/c.imag);
}

istream& operator>>( istream& in, Complex &c )
{
    in >> c.real >> c.imag;
    return in;
}
ostream& operator<<( ostream& out, Complex &c )
{
    out << c.real << "+" << c.imag << "i\n";
    return  out;
}

bool operator == (Complex &c1,Complex &c2)
{
    if(c1.real==c2.real&&c1.imag==c2.imag)
    {
        return true;
    }
    else
    {
        return false;
    }
}
bool operator != (Complex &c1,Complex &c2)
{
    if(c1.real!=c2.real||c1.imag!=c2.imag)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void Complex::display( )
{
    cout<<real<< "+" <<imag<<"i\n"<<endl;
}
int Menu()
{
    int t;
    cout << endl;
    cout<<"=================="<<endl;
    cout<<"1.输入复数"<<endl;
    cout<<"2.查看输入的复数"<<endl;
    cout<<"3.复数相加"<<endl;
    cout<<"4.复数相减"<<endl;
    cout<<"5.复数相乘"<<endl;
    cout<<"6.复数相除"<<endl;
    cout<<"7.输出结果"<<endl;
    cout<<"0.退出"<<endl;
    cout<<"=================="<<endl;
    cout<<"请选择(0-7):";
    cin>>t;
    return t;
}

int main()
{
    int iChoice =1;
    Complex c1,c2,c3,c4;
    while (iChoice!=0)
    {
        iChoice = Menu();
        switch (iChoice)
        {
        case 1:
        {
            cout<<"请输入一个复数:"<<endl;
            cin>>c1;
            getchar();
            break;
        }
        case 2:
        {
            //c1.display();
            cout<<c1;
            break;
        }
        case 3:
        {
            cout<<"原有的复数:"<<endl;
            cout<<c1;
            cout<<"请再输入一个复数相加:"<<endl;
            cin>>c2;
            getchar();
            c3=c1+c2;
            break;
        }
        case 4:
        {
            cout<<"原有的复数:"<<endl;
            cout<<c1;
            cout<<"请再输入一个复数相减:"<<endl;
            cin>>c2;
            getchar();
            c3=c1-c2;
            break;
        }
        case 5:
        {
            cout<<"原有的复数:"<<endl;
            cout<<c1;
            cout<<"请再输入一个复数相乘:"<<endl;
            cin>>c2;
            getchar();
            c3=c1*c2;
            break;

        }
        case 6:
        {
            cout<<"原有的复数:"<<endl;
            cout<<c1;
            cout<<"请再输入一个复数相除:"<<endl;
            cin>>c2;
            getchar();
            c3=c1/c2;
            break;
        }
        case 7:
        {
            cout<<"运算的结果:"<<endl;
            cout<<c3;
            break;
        }
        case 0:
        {
            break;

        }
        }
    }


    return 0;
}

 

posted @ 2019-04-02 20:37  王陸  阅读(3027)  评论(0编辑  收藏  举报