请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义Complex(复数)类,在成员函数中包含重载类型转换运算符:
operator double() { return real; }

Input

一个复数与一个double数

Output

d1的值和复数形式的此值

Sample Input

3 4
2.5

Sample Output

d1=5.50
c2=(5.50, 0.00)
/* C++代码 */

#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()
{return ;}

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;

}


相关文章:

  • 2021-05-16
  • 2021-08-20
  • 2021-05-25
  • 2021-12-02
  • 2021-05-31
  • 2021-08-29
  • 2022-02-17
  • 2021-08-22
猜你喜欢
  • 2021-10-26
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2021-12-29
  • 2021-07-16
相关资源
相似解决方案