现在的位置: 首页 > 综合 > 正文

OJ15

2014年09月05日 ⁄ 综合 ⁄ 共 728字 ⁄ 字号 评论关闭

#include <iostream>

#include <iomanip>

using namespace std;

class Complex

{

public:

    Complex();

    Complex(double r);

    Complex(double r,double i);

    operator double();

    void display();

private:

    double real;

    double imag;

};

Complex::Complex(){}

Complex::Complex(double r)
{
    real=r;
    imag=0;

 

}

Complex::Complex(double r,double i)
{
    real=r;
    imag=i;

}
Complex::operator double()
{

    return real;

}

void Complex::display()
{
    cout<<"("<<real<<", "<<imag<<")"<<endl;

}

//主函数已给定如下,提交时不需要包含,会自动添加到程序尾部

 

int main()

{

    cout<<setiosflags(ios::fixed);

    cout<<setprecision(2);

    double real,imag;

    cin>>real>>imag;

    Complex c1(real,imag);

    double d1;

    cin>>d1;

    d1=d1+c1;

    cout<<"d1="<<d1<<endl;

    Complex c2=Complex(d1);

    cout<<"c2=";

    c2.display();

    return 0;

}

 

抱歉!评论已关闭.