C++复数运算符重载(+与<<)

C++复数运算符重载(+与<<)

时间: 1ms        内存:128M

描述:

定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算与输出操作。
(1)参加运算的两个运算量可以都是类对象,也可以其中有一个是实数,顺序任意。例如,c1+c2,d+c1,c1+d均合法(设d为实数,c1,c2为复数)。
(2)输出的算数,在复数两端加上括号,实部和虚部均保留两位小数,如(8.23+2.00i)、(7.45-3.40i)、(-3.25+4.13i)等。
编写程序,分别求两个复数之和、整数和复数之和,并且输出。

请在下面的程序段基础上完成设计:
#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
Complex():real(0),imag(0) {}
Complex(double r,double i):real(r),imag(i) {}
Complex operator+(Complex &);
Complex operator+(double &);
friend Complex operator+(double&,Complex &);
friend ostream& operator << (ostream& output, const Complex& c);
private:
double real;
double imag;
};

//将程序需要的其他成份写在下面,只提交begin到end部分的代码
//******************** begin ********************

//********************* end ********************
int main()
{
//测试复数加复数
double real,imag;
cin>>real>>imag;
Complex c1(real,imag);
cin>>real>>imag;
Complex c2(real,imag);
Complex c3=c1+c2;
cout<<"c1+c2=";
cout<<c3;

//测试复数加实数
double d;
cin>>real>>imag;
cin>>d;
c3=Complex(real,imag)+d;
cout<<"c1+d=";
cout<<c3;

//测试实数加复数
cin>>d;
cin>>real>>imag;
c1=Complex(real,imag);
c3=d+c1;
cout<<"d+c1=";
cout<<c3;

return 0;
}

输入:

一个复数的实部和虚部,另一个复数的实部和虚部 
一个复数的实部和虚部,一个实数
一个实数,一个复数的实部和虚部

输出:

两个复数之和、复数和实数之和,实数和复数之和。

示例输入:

3 4 5 -10
3 4 5
5 3 4

示例输出:

c1+c2=(8.00-6.00i)
c1+d=(8.00+4.00i)
d+c1=(8.00+4.00i)

提示:

参考答案(内存最优[1096]):

#include<stdio.h>
#include<math.h>
int main()
{
    float a1,a2,a3,a4,a5,a6;
    float b1,b2,b3,b6;
    char s1='+',s2='-';
    scanf("%f%f%f%f",&a1,&b1,&a2,&b2);
    scanf("%f%f%f",&a3,&b3,&a4);
    scanf("%f%f%f",&a5,&a6,&b6);
    if(b1+b2>=0)
       printf("c1+c2=(%.2f%c%.2fi)\n",a1+a2,s1,fabs(b1+b2));
    else
       printf("c1+c2=(%.2f%c%.2fi)\n",a1+a2,s2,fabs(b1+b2));
    if(b3>=0)
       printf("c1+d=(%.2f%c%.2fi)\n",a3+a4,s1,fabs(b3));
    else
       printf("c1+d=(%.2f%c%.2fi)\n",a3+a4,s2,fabs(b3));
    if(b6>=0)
       printf("d+c1=(%.2f%c%.2fi)\n",a5+a6,s1,fabs(b6));
    else
       printf("d+c1=(%.2f%c%.2fi)\n",a5+a6,s2,fabs(b6));
    return 0;
}

参考答案(时间最优[0]):


#include <iostream>
#include <iomanip>
using namespace std;

class Complex
{
public:
    Complex():real(0),imag(0) {}
    Complex(double r,double i):real(r),imag(i) {}
    Complex operator+(Complex &);
    Complex operator+(double &);
    friend Complex operator+(double&,Complex &);
    friend ostream& operator << (ostream& output, const Complex& c);
private:
    double real;
    double imag;
};

//下面定义相关的函数
Complex Complex::operator+(Complex &c2)
{
    Complex c;
    c.real=this->real+c2.real;
    c.imag=this->imag+c2.imag;
    return c;
}

Complex Complex::operator+(double &d)
{

    Complex c;
    c.real=this->real+d;
    c.imag=this->imag;
    return c;
}

//Complex Complex::operator+(double d)
//{
//    Complex c(d,0);
//    return *(this)+c;
//}

Complex operator+(double &d1, Complex &c2)
{
    Complex c(d1,0);
    return c+c2;
}

ostream& operator << (ostream& output, const Complex& c)
{
    output<<setiosflags(ios::fixed);
    output<<setprecision(2);
    output<<"("<<c.real;
    if(c.imag>=0) output<<"+";
    output<<c.imag<<"i)"<<endl;
    return output;
}

int main()
{
    //测试复数加复数
    double real,imag;
    cin>>real>>imag;
    Complex c1(real,imag);
    cin>>real>>imag;
    Complex c2(real,imag);
    Complex c3=c1+c2;
    cout<<"c1+c2=";
    cout<<c3;

    //测试复数加实数
    double d;
    cin>>real>>imag;
    cin>>d;
    c3=Complex(real,imag)+d;
    cout<<"c1+d=";
    cout<<c3;

    //测试实数加复数
    cin>>d;
    cin>>real>>imag;
    c1=Complex(real,imag);
    c3=d+c1;
    cout<<"d+c1=";
    cout<<c3;

    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注