本题考虑对运算符进行重载。分别重载复数运算的+,-,*,/,=(赋值)运算符,以及比较大小的<=(复数1的模是否小于等于复数2的模)运算符,其中,比较运算符按复数的模进行比较。测试程序根据输入的mode值分别测试各个函数是否编写正确。
函数接口定义:
在这里描述函数接口:
#include <iostream>
using namespace std;
class Complex {
double real;
double imag;
public:
//构造函数
Complex(double real=0, double imag=0);
//operator+-*/=操作符函数
Complex operator+(const Complex& c) const;
Complex operator-(const Complex& c) const;
Complex operator*(const Complex& c) const;
Complex operator/(const Complex& c) const;
Complex operator=(const Complex& c);
//operator <=操作符函数
bool operator<=(const Complex& c) const;
//友元函数声明以帮助operator<<()函数访问Complex类的私有成员
friend ostream& operator<<(ostream& out, const Complex& c);
};
//n个复数,按模从小到达排序
void bubble_sort(Complex[],int n);
bubble_sort函数按冒泡排序的算法对n个复数按模从小到大的顺序排序。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子:
int main() {
double dReal1, dImag1, dReal2, dImag2;
int mode;
cin>>mode;
cin>>dReal1>>dImag1>>dReal2>>dImag2;
Complex c1(dReal1, dImag1);
Complex c2(dReal2, dImag2);
Complex c[6] = {c1,c2,c1+c2,c1-c2,c1*c2,c1/c2};
switch(mode)
{
case 1: cout << c[0]<<" "<<c[1];break;
case 2: cout << c[2];break;
case 3: cout << c[3];break;
case 4: cout << c[4];break;
case 5: cout << c[5];break;
case 6: bubble_sort(c,6);
for(int i=0;i<6;i++)
cout<<c[i]<<" ";
}
return 0;
}
/* 请在这里填写答案 */
输入样例1:
在这里给出一组输入。例如:
1
1 2 3 4
输出样例1:
在这里给出相应的输出。例如:
1+2i 3+4i
注意:复数的输出,实部和虚部即使为0也输出。
输入样例2:
在这里给出一组输入。例如:
2
4 5 6 7
输出样例2:
在这里给出相应的输出。例如:
10+12i
注意:复数的输出,实部和虚部即使为0也输出。
下面是我自己写的答案
Complex Complex::operator+(const Complex& c) const
{
Complex a;
a.real = real + c.real;
a.imag = imag + c.imag;
return a;
}
Complex Complex::operator-(const Complex& c) const
{
Complex a;
a.real = real - c.real;
a.imag = imag - c.imag;
return a;
}
Complex Complex::operator*(const Complex& c) const
{
Complex a;
a.real = real * c.real;
a.imag = imag * c.imag;
return a;
}
Complex Complex::operator/(const Complex& c) const
{
Complex a;
a.real = real / c.real;
a.imag = imag / c.imag;
return a;
}
Complex Complex::operator=(const Complex& c)
{
real = c.real;
imag = c.imag;
return *this;
}
bool Complex::operator<=(const Complex& c) const
{
double mo[2];
mo[0] = real * real + imag * imag;
mo[1] = c.real * c.real + c.imag * c.imag;
if (mo[0] <= mo[1])
{
return 1;
}
else
return 0;
}
ostream& operator<<(ostream& out, const Complex& c)
{
out << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i";
return out;
}
void bubble_sort(Complex c[], int n)
{
Complex mo[100];
for (int i = 0; i < n; i++)
{
mo[i] = c[i] * c[i];
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - i; j++)
{
Complex temp;
if (mo[i + 1] <= mo[i])
{
temp = c[i];
c[i] = c[i + 1];
c[i + 1] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
cout << c[i];
}
}
最后完整的代码是这样的
#include<iostream>
using namespace std;
class Complex {
double real;
double imag;
public:
//构造函数
Complex(double real = 0, double imag = 0);
//operator+-*/=操作符函数
Complex operator+(const Complex& c) const;
Complex operator-(const Complex& c) const;
Complex operator*(const Complex& c) const;
Complex operator/(const Complex& c) const;
Complex operator=(const Complex& c);
//operator <=操作符函数
bool operator<=(const Complex& c) const;
//友元函数声明以帮助operator<<()函数访问Complex类的私有成员
friend ostream& operator<<(ostream& out, const Complex& c);
};
//n个复数,按模从小到达排序
void bubble_sort(Complex[], int n);
int main() {
double dReal1, dImag1, dReal2, dImag2;
int mode;
cin >> mode;
cin >> dReal1 >> dImag1 >> dReal2 >> dImag2;
Complex c1(dReal1, dImag1);
Complex c2(dReal2, dImag2);
Complex c[6] = { c1,c2,c1 + c2,c1 - c2,c1 * c2,c1 / c2 };
switch (mode)
{
case 1: cout << c[0] << " " << c[1]; break;
case 2: cout << c[2]; break;
case 3: cout << c[3]; break;
case 4: cout << c[4]; break;
case 5: cout << c[5]; break;
case 6: bubble_sort(c, 6);
for (int i = 0; i < 6; i++)
cout << c[i] << " ";
}
return 0;
}
/* 请在这里填写答案 */
Complex Complex::operator+(const Complex& c) const
{
Complex a;
a.real = real + c.real;
a.imag = imag + c.imag;
return a;
}
Complex Complex::operator-(const Complex& c) const
{
Complex a;
a.real = real - c.real;
a.imag = imag - c.imag;
return a;
}
Complex Complex::operator*(const Complex& c) const
{
Complex a;
a.real = real * c.real;
a.imag = imag * c.imag;
return a;
}
Complex Complex::operator/(const Complex& c) const
{
Complex a;
a.real = real / c.real;
a.imag = imag / c.imag;
return a;
}
Complex Complex::operator=(const Complex& c)
{
real = c.real;
imag = c.imag;
return *this;
}
bool Complex::operator<=(const Complex& c) const
{
double mo[2];
mo[0] = real * real + imag * imag;
mo[1] = c.real * c.real + c.imag * c.imag;
if (mo[0] <= mo[1])
{
return 1;
}
else
return 0;
}
ostream& operator<<(ostream& out, const Complex& c)
{
out << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i";
return out;
}
void bubble_sort(Complex c[], int n)
{
Complex mo[100];
for (int i = 0; i < n; i++)
{
mo[i] = c[i] * c[i];
}
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - i; j++)
{
Complex temp;
if (mo[i + 1] <= mo[i])
{
temp = c[i];
c[i] = c[i + 1];
c[i + 1] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
cout << c[i];
}
}
最后应该是没有红码,但是会报错
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 "public: __cdecl Complex::Complex(double,double)" (??0Complex@@QEAA@NN@Z),函数 "public: class Complex __cdecl Complex::operator*(class Complex const &)const " (??DComplex@@QEBA?AV0@AEBV0@@Z) 中引用了该符号 Project1 C:\Users\SYSA427\source\repos\Project1\Project1\159.obj 1
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK1120 1 个无法解析的外部命令 Project1 C:\Users\SYSA427\source\repos\Project1\x64\Debug\Project1.exe 1
我不知道错哪儿了,有没有会的帮我检查检查,提点意见。