在重载>写相关逻辑就行了
#include <iostream>
using namespace std;
class Complex {
private:
public:
int real;
int imag;
Complex(int r, int i)
: real(r), imag(i){};
int operator>(Complex c);
};
int Complex::operator>(Complex complex) {
int res1 = real * real + imag * imag;
int res2 = complex.real * complex.real + complex.imag * complex.imag;
if (res1 > res2)
return 1;
else if (res1 == res2)
return 0;
return -1;
}
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
Complex complex1(a, b), complex2(c, d);
cout << (complex1 > complex2) << endl;
return 0;
}
只需要重载大于号也行,利用大于符号就可以判断三种情况:
A 大于 B: A > B
A 小于 B:B > A
A等于B: (!A>B)&& (!B>A)
如有用请采纳
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int a,b,c,d,x,y;
int n1 = 0,n2 = 1, n3=-1;
cin>>a>>b>>c>>d;
x = pow(a,2) + pow(b,2);
y = pow(c,2) + pow(d,2);
if(x>y)
cout<<n1;
else if(x<y)
cout<<n3;
else
cout<<n2;
return 0;
}