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