【问题标题】:Overloading operators C++, complex numbers?重载运算符 C++,复数?
【发布时间】:2020-06-14 20:09:55
【问题描述】:

我有这么大的问题。我写了一个关于复数的程序。该程序读取和写入复数,将它们相加等。 他说我应该读一下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;
}  

【问题讨论】:

  • 请只问一个问题以保持专注。
  • 嗯,所以我将编辑我的问题
  • 小测验:您希望a == ba != b 产生什么价值?不管ab 是什么?他们可能是std::strings。它们可以是其他任何东西。您是否期望比较的结果是另一个相同类型的值?当然不是,那太傻了。比较的结果是真还是假,这对你有意义吗?现在,探寻你头脑最深处的空洞,问:C++ 中什么代表truefalse?为什么,当然是bool!所以,再次问自己同样的问题:那么,你重载的 ==!= 运算符应该返回什么?
  • 1 为真,0 为假。所以我应该使用 if? friend bool operator==(const Comp&amp; x, const Comp&amp; y) { if (x.real == y.real &amp;&amp; x.imag == y.imag) return 1; else return 0; } friend bool operator!=(const Comp&amp; x, const Comp&amp; y) { if (x.real != y.real || x.imag != y.imag) return 1; else return 0; }????????????
  • 您是否注意到在 cmets 中显示代码不起作用?请在您的问题中编辑代码。

标签: c++ class oop operator-overloading complex-numbers


【解决方案1】:

如果您正确实施operator==(您在标题为“我认为我找到了解决方案”的块中所做的),那么您可以将它用于operator!=

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

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


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

Comp operator+();
Comp operator-();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 2012-12-22
    • 2016-09-26
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多