C++ 定义一个复数类,随机生成一个无序的复数向量(有重复项) (1)在主函数中测试无序向量的置乱、查找(实部和虚部均相同)、插入、删除和唯一化的操作
时间: 2024-05-22 12:15:15 浏览: 103
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex() {
real = 0;
imag = 0;
}
Complex(double r, double i) {
real = r;
imag = i;
}
double getReal() const {
return real;
}
double getImag() const {
return imag;
}
void setReal(double r) {
real = r;
}
void setImag(double i) {
imag = i;
}
Complex operator+(const Complex& c) const {
return Complex(real + c.real, imag + c.imag);
}
Complex operator-(const Complex& c) const {
return Complex(real - c.real, imag - c.imag);
}
Complex operator*(const Complex& c) const {
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
Complex operator/(const Complex& c) const {
double denominator = c.real * c.real + c.imag * c.imag;
return Complex((real * c.real + imag * c.imag) / denominator, (imag * c.real - real * c.imag) / denominator);
}
friend bool operator==(const Complex& c1, const Complex& c2) {
return c1.real == c2.real && c1.imag == c2.imag;
}
friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real << "+" << c.imag << "i";
return out;
}
};
void shuffle(vector<Complex>& vec) {
srand(time(NULL));
int n = vec.size();
for (int i = 0; i < n; i++) {
int j = rand() % n;
swap(vec[i], vec[j]);
}
}
int find(const vector<Complex>& vec, const Complex& c) {
int n = vec.size();
for (int i = 0; i < n; i++) {
if (vec[i] == c) {
return i;
}
}
return -1;
}
void insert(vector<Complex>& vec, const Complex& c) {
vec.push_back(c);
}
void remove(vector<Complex>& vec, const Complex& c) {
int n = vec.size();
for (int i = 0; i < n; i++) {
if (vec[i] == c) {
vec.erase(vec.begin() + i);
break;
}
}
}
void unique(vector<Complex>& vec) {
int n = vec.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (vec[i] == vec[j]) {
vec.erase(vec.begin() + j);
n--;
j--;
}
}
}
}
int main() {
vector<Complex> vec;
vec.push_back(Complex(1, 2));
vec.push_back(Complex(3, 4));
vec.push_back(Complex(5, 6));
vec.push_back(Complex(1, 2));
vec.push_back(Complex(7, 8));
vec.push_back(Complex(9, 10));
cout << "Before shuffle: ";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
shuffle(vec);
cout << "After shuffle: ";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
Complex c(1, 2);
int index = find(vec, c);
if (index != -1) {
cout << "Found " << c << " at index " << index << endl;
} else {
cout << "Not found " << c << endl;
}
Complex d(5, 6);
insert(vec, d);
cout << "After insert " << d << ": ";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
remove(vec, c);
cout << "After remove " << c << ": ";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
unique(vec);
cout << "After unique: ";
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
return 0;
}
阅读全文