Skip to content

11011: 【原1011】复数类

题目

题目描述

author: 刘勤 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1011

题目描述

写一个复数类,实现以下程序主函数中所需要的功能。

#include <iostream>
using namespace std;

class MyComplex
{
private:
  double x,y;
public:
  /* Implementation of MyComplex */
};

int main()
{
  MyComplex z1;
  MyComplex z2;

  cin >> z1 >> z2;

  cout << z1 + z2 <<endl;
  cout << z1 - z2 <<endl;
  cout << z1 * z2 <<endl;
  cout << z1 / z2 <<endl;
  cout << (z1 += z2) <<endl;
  cout << (z1 -= z2) <<endl;
  cout << (z1 *= z2) <<endl;
  cout << (z1 /= z2) <<endl;

  return 0;
}

输入格式

输入包括两行,第一行是两个整数a, b(\(0 < |a|+1, |b| < 10001\)),表示复数\(a+bi\)。

第二行是两个整数c, d(\(0 < |c|+1, |d| < 10001\)),表示复数\(c+di\)。输入数据保证不出现除以0的情况。

输出格式

输出包括八行,对应所给程序中的输出。注意输出浮点数保留2位小数。

Sample Input 1

3 6
-3 5

Sample Output 1

0.00 11.00
6.00 1.00
-39.00 -3.00
0.62 -0.97
0.00 11.00
3.00 6.00
-39.00 -3.00
3.00 6.00

Sample Input 2

5 9
5 -9

Sample Output 2

10.00 0.00
0.00 18.00
106.00 0.00
-0.53 0.85
10.00 0.00
5.00 9.00
106.00 0.00
5.00 9.00

FineArtz's solution

/* 复数类 */
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

class CMP{
    //friend
    friend inline CMP operator +(const CMP&, const CMP&);
    friend inline CMP operator -(const CMP&, const CMP&);
    friend inline CMP operator *(const CMP&, const CMP&);
    friend inline CMP operator /(const CMP&, const CMP&);
    friend istream& operator >>(istream&, CMP&);
    friend ostream& operator <<(ostream&, const CMP&);
public:
    //constructor
    CMP() : x(0.0), y(0.0) {}
    CMP(const double &xx, const double &yy) : x(xx), y(yy) {}
    CMP(const int &xx, const int &yy) : x(xx), y(yy) {}
    CMP(const CMP &cmp) : x(cmp.x), y(cmp.y) {}

    //operator
    CMP& operator =(const CMP&);
    CMP& operator +=(const CMP&);
    CMP& operator -=(const CMP&);
    CMP& operator *=(const CMP&);
    CMP& operator /=(const CMP&);
    CMP operator ~() const;//conjugation
    CMP operator -() const;//minus

    double SquareModule() const { return (x * x + y * y); }
    double Module() const { return sqrt(x * x + y * y); }
private:
    double x, y;
};

CMP& CMP::operator =(const CMP &rhs){
    x = rhs.x;
    y = rhs.y;
    return *this;
}
CMP CMP::operator ~() const{
    return CMP(x, -y);
}
CMP CMP::operator -() const{
    return CMP(-x, -y);
}
CMP inline operator +(const CMP &lhs, const CMP &rhs){
    return CMP(lhs.x + rhs.x, lhs.y + rhs.y);
}
CMP inline operator -(const CMP &lhs, const CMP &rhs){
    return CMP(lhs.x - rhs.x, lhs.y - rhs.y);
}
CMP inline operator *(const CMP &lhs, const CMP &rhs){
    return CMP(lhs.x * rhs.x - lhs.y * rhs.y, lhs.x * rhs.y + lhs.y * rhs.x);
}
CMP inline operator /(const CMP &lhs, const CMP &rhs){
    CMP ret = lhs * ~rhs;
    return CMP(ret.x / rhs.SquareModule(), ret.y / rhs.SquareModule());
}
CMP& CMP::operator +=(const CMP &rhs){
    x += rhs.x;
    y += rhs.y;
    return *this;
}
CMP& CMP::operator -=(const CMP &rhs){
    x -= rhs.x;
    y -= rhs.y;
    return *this;
}
CMP& CMP::operator *=(const CMP &rhs){
    double tx = x * rhs.x - y * rhs.y;
    double ty = x * rhs.y + y * rhs.x;
    x = tx;
    y = ty;
    return *this;
}
CMP& CMP::operator /=(const CMP &rhs){
    CMP cmp = *this / rhs;
    *this = cmp;
    return *this;
}
istream& operator >>(istream &is, CMP& cmp){
    is >> cmp.x >> cmp.y;
    return is;
}
ostream& operator <<(ostream &os, const CMP& cmp){
    os << setiosflags(ios::fixed) << setprecision(2) << cmp.x << ' ' << cmp.y;
    return os;
}

int main(){
    CMP z1, z2;
    cin >> z1 >> z2;
    cout << z1 + z2 << endl;
    cout << z1 - z2 << endl;
    cout << z1 * z2 << endl;
    cout << z1 / z2 << endl;
    cout << (z1 += z2) << endl;
    cout << (z1 -= z2) << endl;
    cout << (z1 *= z2) << endl;
    cout << (z1 /= z2) << endl;
    return 0;
}

ligongzzz's solution

#include <iostream>
#include "cstdio"
using namespace std;

class MyComplex
{
private:
    double x, y;
public:
    friend istream& operator>>(istream& input, MyComplex &b) {
        input >> b.x >> b.y;
        return input;
    }
    friend ostream& operator<<(ostream& output, const MyComplex &b) {
        char op[1000];
        sprintf(op, "%.2f %.2f", b.x, b.y);
        output << op;
        //output << fixed << setprecision(2) << b.x << " " << b.y;
        return output;
    }

    MyComplex operator+(const MyComplex &b) const{
        MyComplex temp;
        temp.x = x + b.x;
        temp.y = y + b.y;
        return temp;
    }

    MyComplex operator-(const MyComplex &b) const {
        MyComplex temp;
        temp.x = x - b.x;
        temp.y = y - b.y;
        return temp;
    }

    MyComplex operator*(const MyComplex &b) const {
        MyComplex temp;
        temp.x = x * b.x - y * b.y;
        temp.y = x * b.y + y * b.x;
        return temp;
    }

    MyComplex operator/(const MyComplex &b) const {
        MyComplex temp;
        double data = b.x*b.x + b.y*b.y;
        temp.x = (x*b.x + y * b.y) / data;
        temp.y = (y*b.x - x * b.y) / data;
        return temp;
    }

    MyComplex &operator+=(const MyComplex &b) {
        *this = *this + b;
        return *this;
    }

    MyComplex &operator-=(const MyComplex &b) {
        *this = *this - b;
        return *this;
    }

    MyComplex &operator*=(const MyComplex &b) {
        *this = *this * b;
        return *this;
    }

    MyComplex &operator/=(const MyComplex &b) {
        *this = *this / b;
        return *this;
    }
};

int main()
{
    MyComplex z1;
    MyComplex z2;

    cin >> z1 >> z2;

    cout << z1 + z2 << endl;
    cout << z1 - z2 << endl;
    cout << z1 * z2 << endl;
    cout << z1 / z2 << endl;
    cout << (z1 += z2) << endl;
    cout << (z1 -= z2) << endl;
    cout << (z1 *= z2) << endl;
    cout << (z1 /= z2);

    return 0;
}

Neight99's solution

#include <iomanip>
#include <iostream>

using namespace std;

class MyComplex {
   private:
    double x, y;

   public:
    MyComplex(int a = 0, int b = 0) : x(a), y(b) {}
    MyComplex operator+(const MyComplex &) const;
    MyComplex operator-(const MyComplex &) const;
    MyComplex operator*(const MyComplex &)const;
    MyComplex operator/(const MyComplex &) const;
    MyComplex &operator+=(const MyComplex &);
    MyComplex &operator-=(const MyComplex &);
    MyComplex &operator*=(const MyComplex &);
    MyComplex &operator/=(const MyComplex &);
    friend istream &operator>>(istream &, MyComplex &);
    friend ostream &operator<<(ostream &, const MyComplex &);
};

MyComplex MyComplex::operator+(const MyComplex &right) const {
    MyComplex tmp;
    tmp.x = x + right.x;
    tmp.y = y + right.y;
    return tmp;
}

MyComplex MyComplex::operator-(const MyComplex &right) const {
    MyComplex tmp;
    tmp.x = x - right.x;
    tmp.y = y - right.y;
    return tmp;
}

MyComplex MyComplex::operator*(const MyComplex &right) const {
    MyComplex tmp;
    tmp.x = x * right.x - y * right.y;
    tmp.y = x * right.y + y * right.x;
    return tmp;
}

MyComplex MyComplex::operator/(const MyComplex &right) const {
    MyComplex tmp;
    tmp.x =
        (x * right.x + y * right.y) / (right.x * right.x + right.y * right.y);
    tmp.y =
        (y * right.x - x * right.y) / (right.x * right.x + right.y * right.y);
    return tmp;
}

MyComplex &MyComplex::operator+=(const MyComplex &right) {
    (*this) = (*this) + right;
    return *this;
}

MyComplex &MyComplex::operator-=(const MyComplex &right) {
    (*this) = (*this) - right;
    return *this;
}

MyComplex &MyComplex::operator*=(const MyComplex &right) {
    (*this) = (*this) * right;
    return *this;
}

MyComplex &MyComplex::operator/=(const MyComplex &right) {
    (*this) = (*this) / right;
    return *this;
}

istream &operator>>(istream &is, MyComplex &right) {
    is >> right.x >> right.y;
    return is;
}

ostream &operator<<(ostream &os, const MyComplex &right) {
    os << setiosflags(ios::fixed) << setprecision(2) << right.x << ' '
       << right.y;
    return os;
}

int main() {
    MyComplex z1;
    MyComplex z2;

    cin >> z1 >> z2;

    cout << z1 + z2 << endl;
    cout << z1 - z2 << endl;
    cout << z1 * z2 << endl;
    cout << z1 / z2 << endl;
    cout << (z1 += z2) << endl;
    cout << (z1 -= z2) << endl;
    cout << (z1 *= z2) << endl;
    cout << (z1 /= z2) << endl;

    return 0;
}

q4x3's solution

/**
 * 复数类
 **/
#include <iostream>
#include <iomanip>
using namespace std;

class MyComplex
{
private:
    double x,y;
public:
    friend istream& operator>>(istream &in, MyComplex &obj);
    friend ostream& operator<<(ostream &os, const MyComplex &obj);
    friend MyComplex operator+(const MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator-(const MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator*(const MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator/(const MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator+=(MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator-=(MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator*=(MyComplex &i1, const MyComplex &i2);
    friend MyComplex operator/=(MyComplex &i1, const MyComplex &i2);
};

istream& operator>>(istream &in, MyComplex &obj) {
    in >> obj.x >> obj.y;
    return in;
}

ostream& operator<<(ostream &os, const MyComplex &obj) {
    os << fixed << setprecision(2) << obj.x << " " << obj.y;
    return os;
}

MyComplex operator+(const MyComplex &i1, const MyComplex &i2) {
    MyComplex tmp;
    tmp.x = i1.x + i2.x;
    tmp.y = i1.y + i2.y;
    return tmp;
}

MyComplex operator-(const MyComplex &i1, const MyComplex &i2) {
    MyComplex tmp;
    tmp.x = i1.x - i2.x;
    tmp.y = i1.y - i2.y;
    return tmp;
}

MyComplex operator*(const MyComplex &i1, const MyComplex &i2) {
    MyComplex tmp;
    tmp.x = i1.x * i2.x - i1.y * i2.y;
    tmp.y = i1.x * i2.y + i1.y * i2.x;
    return tmp;
}

MyComplex operator/(const MyComplex &i1, const MyComplex &i2) {
    MyComplex tmp;
    int t = i2.x * i2.x + i2.y * i2.y;
    tmp.x = (i1.x * i2.x + i1.y * i2.y) / t;
    tmp.y = (i1.y * i2.x - i1.x * i2.y) / t;
    return tmp;
}

MyComplex operator+=(MyComplex &i1, const MyComplex &i2) {
    i1 = i1 + i2;
    return i1;
}

MyComplex operator-=(MyComplex &i1, const MyComplex &i2) {
    i1 = i1 - i2;
    return i1;
}

MyComplex operator*=(MyComplex &i1, const MyComplex &i2) {
    i1 = i1 * i2;
    return i1;
}

MyComplex operator/=(MyComplex &i1, const MyComplex &i2) {
    i1 = i1 / i2;
    return i1;
}

int main()
{
    MyComplex z1;
    MyComplex z2;

    cin >> z1 >> z2;

    cout << z1 + z2 <<endl;
    cout << z1 - z2 <<endl;
    cout << z1 * z2 <<endl;
    cout << z1 / z2 <<endl;
    cout << (z1 += z2) <<endl;
    cout << (z1 -= z2) <<endl;
    cout << (z1 *= z2) <<endl;
    cout << (z1 /= z2) <<endl;

    return 0;
}

satgo1546's solution

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

int main(int argc, char *argv[]) {
    double a, b, c, d;
    cin >> a >> b >> c >> d;
    printf("%.2f %.2f\n", a + c, b + d);
    printf("%.2f %.2f\n", a - c, b - d);
    printf("%.2f %.2f\n", a * c - b * d, c * b + a * d);
    printf("%.2f %.2f\n", (a * c + b * d) / (c * c + d * d), (b * c - a * d) / (c * c + d * d));
    printf("%.2f %.2f\n", a + c, b + d);
    a += c;
    b += d;
    a -= c;
    b -= d;
    printf("%.2f %.2f\n", a, b);
    double e = a * c - b * d;
    double f = c * b + a * d;
    a = e;
    b = f;
    e = (a * c + b * d) / (c * c + d * d);
    f = (b * c - a * d) / (c * c + d * d);
    printf("%.2f %.2f\n", a, b);
    printf("%.2f %.2f\n", e, f);
    return 0;
}

skyzh's solution

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

class MyComplex
{
private:
    double x,y;
public:
    MyComplex() {
        this->x = this->y = 0;
    }
    MyComplex(double x, double y) : x(x), y(y) {
    }

    friend istream& operator>> (istream& is, MyComplex &c) {
        is >> c.x >> c.y;
        return is;
    }
    friend ostream& operator<< (ostream& os, const MyComplex &c) {
        os << fixed << setprecision(2) << c.x << " " << c.y;
        return os;
    }
    friend MyComplex operator+ (const MyComplex& a, const MyComplex& b) {
        return MyComplex(a.x + b.x, a.y + b.y);
    }
    friend MyComplex operator- (const MyComplex& a, const MyComplex& b) {
        return MyComplex(a.x - b.x, a.y - b.y);
    }
    friend MyComplex operator* (const MyComplex& a, const MyComplex& b) {
        return MyComplex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
    }
    friend MyComplex operator/ (const MyComplex& a, const MyComplex& b) {
        const double _ = b.x * b.x + b.y * b.y;
        return MyComplex((a.x * b.x + a.y * b.y) / _, (- a.x * b.y + a.y * b.x) / _);
    }
    MyComplex& operator+= (const MyComplex& that) {
        *this = (*this) + that;
        return *this;
    }
    MyComplex& operator-= (const MyComplex& that) {
        *this = (*this) - that;
        return *this;
    }
    MyComplex& operator*= (const MyComplex& that) {
        *this = (*this) * that;
        return *this;
    }
    MyComplex& operator/= (const MyComplex& that) {
        *this = (*this) / that;
        return *this;
    }
};



int main()
{
  MyComplex z1;
  MyComplex z2;

  cin >> z1 >> z2;

  cout << z1 + z2 <<endl;
  cout << z1 - z2 <<endl;
  cout << z1 * z2 <<endl;
  cout << z1 / z2 <<endl;
    cout << (z1 += z2) <<endl;
  cout << (z1 -= z2) <<endl;
  cout << (z1 *= z2) <<endl;
  cout << (z1 /= z2) <<endl;
  return 0;
}

victrid's solution

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

class MyComplex
{
    friend ostream &operator<<(ostream &, const MyComplex &);
    friend istream &operator>>(istream &, MyComplex &);
    friend MyComplex operator+(const MyComplex &, const MyComplex &);
    friend MyComplex operator-(const MyComplex &, const MyComplex &);
    friend MyComplex operator!(const MyComplex &);
    friend MyComplex operator*(const MyComplex &, const MyComplex &);
    friend MyComplex operator/(const MyComplex &, const MyComplex &);
private:
    double x, y;

public:
    MyComplex &operator=(const MyComplex &);
    MyComplex &operator+=(const MyComplex &);
    MyComplex &operator-=(const MyComplex &);
    MyComplex &operator*=(const MyComplex &);
    MyComplex &operator/=(const MyComplex &);
};
ostream &operator<<(ostream &ost, const MyComplex &z)
{
    ost <<setiosflags(ios::fixed)<<setprecision(2)<< z.x << ' ' << z.y;
    return ost;
}
istream &operator>>(istream &ist, MyComplex &z)
{
    ist >> z.x >> z.y;
    return ist;
}
MyComplex operator+(const MyComplex &a, const MyComplex &b)
{
    MyComplex Temp;
    Temp.x = a.x + b.x;
    Temp.y = a.y + b.y;
    return Temp;
}
MyComplex operator-(const MyComplex &a, const MyComplex &b)
{
    MyComplex Temp;
    Temp.x = a.x - b.x;
    Temp.y = a.y - b.y;
    return Temp;
}
MyComplex operator!(const MyComplex &a)
{
    MyComplex Temp;
    Temp.x = a.x;
    Temp.y = -a.y;
    return Temp;
}
MyComplex operator*(const MyComplex &a, const MyComplex &b)
{
    MyComplex Temp;
    Temp.x = a.x * b.x - a.y * b.y;
    Temp.y = a.x * b.y + a.y * b.x;
    return Temp;
}
MyComplex operator/(const MyComplex &a, const MyComplex &b)
{
    MyComplex Temp;
    Temp = a * (!b);
    Temp.x /= (b * (!b)).x;
    Temp.y /= (b * (!b)).x;
    return Temp;
}
MyComplex &MyComplex::operator=(const MyComplex &a)
{
    x = a.x;
    y = a.y;
    return *this;
}
MyComplex &MyComplex::operator+=(const MyComplex &a)
{
    *this = *this + a;
    return *this;
}
MyComplex &MyComplex::operator-=(const MyComplex &a)
{
    *this = *this - a;
    return *this;
}
MyComplex &MyComplex::operator*=(const MyComplex &a)
{
    *this = *this * a;
    return *this;
}
MyComplex &MyComplex::operator/=(const MyComplex &a)
{
    *this = *this / a;
    return *this;
}
int main()
{
    MyComplex z1;
    MyComplex z2;

    cin >> z1 >> z2;

    cout << z1 + z2 << endl;
    cout << z1 - z2 << endl;
    cout << z1 * z2 << endl;
    cout << z1 / z2 << endl;
    cout << (z1 += z2) << endl;
    cout << (z1 -= z2) << endl;
    cout << (z1 *= z2) << endl;
    cout << (z1 /= z2) << endl;

    return 0;
}

yyong119's solution

#include <iostream>
#include <cstdio>
double a,b,c,d,e,f;
int main(){
    scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
    printf("%.2lf%s%.2lf\n",a+c," ",b+d);
    printf("%.2lf%s%.2lf\n",a-c," ",b-d);
    printf("%.2lf%s%.2lf\n",a*c-b*d," ",a*d+b*c);
    e=(a*c+b*d)/(c*c+d*d); f=(b*c-a*d)/(c*c+d*d);
    printf("%.2lf%s%.2lf\n",e," ",f);
    e=a+c; f=b+d; a=e; b=f;
    printf("%.2lf%s%.2lf\n",a," ",b);
    e=a-c; f=b-d; a=e; b=f;
    printf("%.2lf%s%.2lf\n",a," ",b);
    e=a*c-b*d; f=a*d+b*c; a=e; b=f;
    printf("%.2lf%s%.2lf\n",a," ",b);
    e=(a*c+b*d)/(c*c+d*d); f=(b*c-a*d)/(c*c+d*d); a=e; b=f;
    printf("%.2lf%s%.2lf",a," ",b);
    return 0;
}