【问题标题】:Sum of squares complex numbers in descending order按降序排列的复数平方和
【发布时间】:2021-02-26 15:37:04
【问题描述】:

我想使用三个源代码(Complex2.h、Complex2.cpp、Vector3)按降序计算复数平方和 例如) (5+6i)^2 (3+4i)^2 (1+2i)^2....

我正在尝试在 Vector3 中添加和更改一些代码以获得结果,但现在卡在了中间.. 有人给点建议吗?

//Complex2.h
#ifndef  COMPLEX2_H_INCLUDED
#define  COMPLEX2_H_INCLUDED
#include    <ostream>
using namespace std;
class  Complex2 {
    double  rPart, iPart;           // real part and imaginary part
public:
    // operator
    Complex2(double r = 0, double i = 0) : rPart(r), iPart(i) {}
    Complex2  conj()  const {
        return Complex2(rPart, -iPart);
    }
    Complex2  operator+(const Complex2 &c) const;
    Complex2  operator+(double r) const;
    Complex2  operator-(const Complex2 &c) const;
    Complex2  operator-(double r) const;
    Complex2  operator*(const Complex2 &c) const;
    Complex2  operator*(double r) const;
    Complex2  operator/(const Complex2 &c) const;
    Complex2  operator/(double r) const;
    Complex2& operator+=(const Complex2 &c);
    Complex2& operator-=(const Complex2 &c);
    Complex2& operator*=(const Complex2 &c);
    Complex2& operator/=(const Complex2 &c);
    bool      operator==(const Complex2 &c) const;
    bool      operator!=(const Complex2 &c) const;
    double    real() const { return rPart; }
    double    imag() const { return iPart; }
    void  display() const;      // print complex value
    friend  Complex2  operator+(double r, const Complex2& c);
    friend  ostream& operator<<(ostream& os, const Complex2& c);
};
#endif

//Complex2.cpp
#include    <iostream>
#include    "Complex2.h"
using namespace std;

Complex2 Complex2::operator+(const Complex2 &c) const
{
    return Complex2(rPart + c.rPart, iPart + c.iPart);
}    

Complex2 Complex2::operator+(double r) const
{
    return Complex2(rPart + r, iPart);
}

Complex2 Complex2::operator-(const Complex2 &c) const
{
    return Complex2(rPart - c.rPart, iPart - c.iPart);
}

Complex2 Complex2::operator-(double r) const
{
    return Complex2(rPart - r, iPart);
}

Complex2 Complex2::operator*(const Complex2 &c) const
{
    return Complex2(rPart * c.rPart - iPart * c.iPart, rPart * c.iPart + iPart * c.rPart);
}

Complex2 Complex2::operator*(double r) const
{
    return Complex2(rPart * r, iPart * r);
}

Complex2 Complex2::operator/(const Complex2 &c) const
{
    double  d = c.rPart * c.rPart + c.iPart * c.iPart;
    return Complex2((rPart * c.rPart + iPart * c.iPart) / d, (iPart * c.rPart - rPart * c.iPart) /     d);
}

Complex2 Complex2::operator/(double r) const
{
    return Complex2(rPart / r, iPart / r);
}

Complex2& Complex2::operator+=(const Complex2 &c)
{
    rPart += c.rPart;    iPart += c.iPart;
    return *this;
}

Complex2& Complex2::operator-=(const Complex2 &c)
{
    rPart -= c.rPart;    iPart -= c.iPart;
    return *this;
}

Complex2& Complex2::operator*=(const Complex2 &c)
{
    *this = *this * c;
    return *this;
}

Complex2& Complex2::operator/=(const Complex2 &c)
{
    *this = *this / c;
    return *this;
}

bool Complex2::operator==(const Complex2 &c) const
{
    return  rPart == c.rPart && iPart == c.iPart;
}

bool Complex2::operator!=(const Complex2 &c) const
{
    return  rPart != c.rPart || iPart != c.iPart;
}

void  Complex2::display() const
{
    cout << "(" << rPart;
    if (iPart > 0)
        cout << "+j" << iPart;
    else if (iPart < 0)
        cout << "-j" << -iPart;
    cout << ")";
}    

Complex2  operator+(double r, const Complex2& c)
{
    return Complex2(r + c.rPart, c.iPart);
}

ostream&  operator<<(ostream& os, const Complex2& c)
{
    os << "(" << c.rPart;   // print real part
    if (c.iPart > 0)        // print imaginary part
        os << "+j" << c.iPart;
    else if (c.iPart < 0)
        os << "-j" << -c.iPart;
    cout << ")";
    return os;
}    

//Vector3.cpp
#include  <iostream>
#include  <vector>
#include  <cstdlib>
#include  <ctime>
#include  <algorithm>
#include "Complex2.h";
using  namespace  std;

template<typename T> struct GREATER {
    bool operator()(const T& a, const T& b) const {
        return a > b;
    }
};

int  main()
{
    srand((unsigned)time(NULL));   // Initialize random number generation

    vector<Complex2> cv1(5);
    cout << "vector1 : ";
    for (auto &c : cv1) {
        c = rand() % 100;         // 0~99 random number generation
        cout << c << " ";
    }
    sort(cv1.begin(), cv1.end(), GREATER<int>());   // sort algorithm in descending
    cout << endl << "sorted vector1 : ";
    for (auto c : cv1)
        cout << c << " ";
    cout << endl << endl;

    vector<Complex2> cv2(5);
    cout << "vector2 : ";
    for (auto &c : cv2) {
        c = rand() % 100;         // 0~99 random number generation
        cout << c << " ";
    }
    sort(cv2.begin(), cv2.end(), GREATER<int>());   // sort algorithm in descending
    cout << endl << "sorted vector2 : ";
    for (auto c : cv2)
        cout << c << " ";
    cout << endl << endl;

    // vector for saving the merged result
    vector<Complex2> cv3(cv1.size() + cv2.size());
    // Save the result cv1 plus cv2 as cv3
    merge(cv1.begin(), cv1.end(),
        cv2.begin(), cv2.end(), cv3.begin(), GREATER<int>());
    cout << "the result of merged vector1 and vector2 : ";
    for (auto c : cv3)
        cout << c << " ";
    cout << endl << endl;

    return 0;
}

【问题讨论】:

  • sort(cv1.begin(), cv1.end(), GREATER&lt;int&gt;()); 这没有意义。您使用一个知道如何比较ints 的比较器,并尝试使用它来比较复数。在数学上,复数不会形成一个单一的总顺序,因此不清楚对它们进行排序意味着什么。
  • 问题陈述中提到了“平方和”,但显示的代码没有对任何数字进行求和或平方,无论是复数还是其他。
  • 您可以将GREATER 替换为std::greater

标签: c++ sorting merge


【解决方案1】:

为此,您真正需要的是定义operator&gt;

bool Complex2::operator>(const Complex2 &c) const
{
    return rPart * rPart + iPart * iPart > c.rPart * c.rPart + c.iPart * c.iPart;
}

然后你可以打电话给std::sortstd::merge

std::sort(cv1.begin(), cv1.end(), std::greater<Complex2>());

std::merge(cv1.begin(), cv1.end(), cv2.begin(), cv2.end(), 
           cv3.begin(), std::greater<Complex2>());

旁注:为什么不使用std::complex

【讨论】:

  • 这行得通,假设 OP 在写“正方形”时的意思是“量级”。
  • @dxiv 实际上,如果你对一个复数进行平方,它的大小也是平方的,除非我今天完全不在
  • 是的,因此比较幅度的平方给出了与比较幅度本身相同的排序顺序。但是OP只写了“平方”,无法比较“平方”,因为复数的平方本身就是一个复数,复数存在no total order
【解决方案2】:

这里有一件非常重要的事情:

不可能对复数排序!!!

您正在谈论对它们进行排序,并且您似乎在寻找某种排序函数,但您需要知道,无论采用何种方式对复数进行排序,排序都必须满足以下参数:

Order(Number1, Number2) AND Order(Number2, Number3) => Order(Number1, Number3)

这个表达式不可能是真的,对于你可能发明的任何排序(至少对于复数,实数排序很简单),所以你关于复数“降序”的问题没有意义。

完整的解释可以在here找到。

【讨论】:

  • 我们可以创建一个严格的弱排序,这就是我们需要排序的所有内容。它在数学上是否有用是另一回事。
猜你喜欢
  • 1970-01-01
  • 2011-08-25
  • 2019-07-18
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
  • 2020-02-08
  • 1970-01-01
相关资源
最近更新 更多