gpt4 book ai didi

c++ - 重载运算符 C++,复数?

转载 作者:行者123 更新时间:2023-12-02 10:14:35 27 4
gpt4 key购买 nike

我有这么大的问题。我写了一个关于复数的程序。该程序读取和写入复数,将它们相加等。
他说我应该读一下 Why should I overload a C++ operator as a global function (STL does) and what are the caveats?

1) 我必须创建 5 个运算符,它们是类的成员函数,并且有一个参数:+、-、!、++、--。然后

2) 我必须创建 5 个运算符,它们是类的成员函数,其中有两个参数:=、+=、-=、*=、/=;然后

3) 我必须创建 8 个运算符,它们是全局友元函数 +、-、*、/、==、!=、<<、>> 并接受两个参数。我对最后一个没有问题:

    friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}

除了这个??????我应该在这里返回什么??????当我比较它应该是 bool 是吗????
    friend const Comp operator==(const Comp& x, const Comp& y)
{

}
friend const Comp operator!=(const Comp& x, const Comp& y)
{

}

我认为我找到了解决方案

friend bool operator==(const Comp& x, const Comp& y)
{
return (x.real == y.real && x.imag == y.imag);
}
friend bool operator!=(const Comp& x, const Comp& y)
{
return (x.real != y.real && x.imag != y.imag);
}

这是我的全部代码
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
using namespace std;


class Comp {
double real, imag;

public:
Comp(){
real;
imag;
}
double re(void) const
{
return real;
}
double im(void) const
{
return imag;
}
double mod(void) const
{
return sqrt(re()*re() + im()*im());
}
double arg(void) const
{
double faza;
if (im() >= 0)
faza = acos(re()/mod());
else
faza = 2*M_PI - acos(re()/mod());

return faza;
}
const Comp conj(void) const
{
Comp temp;
-im();
return temp;
}
~Comp(){}
/*
Comp operator+(const Comp& x);
Comp operator-(const Comp& x);
bool operator!(void);
const Comp& operator++();
const Comp operator++(int);
const Comp& operator--();
const Comp operator--(int);
Comp operator=(const Comp x);
Comp operator-=(const Comp& x);
Comp operator+=(const Comp& x);
Comp operator*=(const Comp& x);
Comp operator/=(const Comp& x);
*/
friend const Comp operator+(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator-(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real - y.real;
temp.imag = x.imag - y.imag;
return temp;
}
friend const Comp operator*(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real * y.real;
temp.imag = x.imag * y.imag;
return temp;
}
friend const Comp operator/(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real / y.real;
temp.imag = x.imag / y.imag;
return temp;
}
friend const Comp operator==(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}
friend const Comp operator!=(const Comp& x, const Comp& y)
{
Comp temp;
temp.real = x.real + y.real;
temp.imag = x.imag + y.imag;
return temp;
}

friend std::ostream& operator<<(std::ostream& wart1, const Comp& a)
{
return wart1 <<fixed << setprecision(2) << '(' << a.re() << "," << a.im() << ')' << ' ' << endl;
}
friend std::istream& operator>>(std::istream& wart2, Comp& b){
char c;
return wart2>>c>>b.real>>c>>b.imag>>c;
}
};

int main(int argc, char* argv[])
{
ifstream read(argv[1]);
if (!read)
{ cerr << "Open error: " << argv[1] << endl; exit(1);}
ofstream write(argv[2]);

if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
read.clear();
read.seekg(0);
Comp x1;
read >> x1;
write << x1;
cout << x1;
Comp x2;
read >> x2;
write << x2;
cout << x2;
cout << x1.mod() << endl;
cout << x2.mod() << endl;
cout << x1.arg() << endl;
cout << x2.arg() << endl;
cout << x1.conj();
cout << x2.conj();
write << x2;
write << x1.mod() << endl;
write << x2.mod() << endl;
write << x1.arg() << endl;
write << x2.arg() << endl;
write << x1.conj();
write << x2.conj();
Comp sum;
sum = x1 + x2;
cout << sum;
write << sum;
Comp sub;
sub = x1 - x2;
cout << sub;
write << sub;
Comp mult;
mult = x1 * x2;
cout << mult;
write << mult;
Comp div;
div = x1 / x2;
cout << div;
write << div;

return 0;
}

最佳答案

如果您实现 operator==正确(您在标题为“我认为我找到解决方案”的 block 中所做的),然后您可以将它用于 operator!= :

friend bool operator!=(const Comp& x, const Comp& y)
{
return !(x == y);
}

您的版本不正确,因为如果它们具有相同的实部和不同的虚部,它将报告它们不相等。

此外,在第 (1) 部分中,它指的是一元 + 和 -(不是我们在第 3 部分中获得的二进制版本)。所以你在注释掉的 block 中的前两个声明是不正确的,它们应该是:
Comp operator+();
Comp operator-();

关于c++ - 重载运算符 C++,复数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62377654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com