0

我有一个作业到期,但我无法理解该作业真正要求我做什么,或者如何去做。我知道复数是什么,但我不明白 C++ 和 Python 版本的以下操作应该做什么:

op: Complex × Complex → Complex 
op: Complex × double → Complex 
op: double × Complex → Complex 

双倍的?我不明白 double 是从哪里来的。python版本也应该将复合体转换为字符串,我还是不明白它在问什么。是说将复数(整数?)字面转换为字符串数据类型吗?请让我知道您是否可以尝试帮助我理解作业的要求,以便我可以尝试对其进行编程。

复数类

用 C++、Java 和 Python 设计一个表示复数并支持加法、减法、乘法和除法等重要运算的类。对于 C++ 和 Python 版本,您需要为每个操作实现以下函数:

op: Complex × Complex → Complex
op: Complex × double → Complex  op:
double × Complex → Complex

其中 op 是 +、-、* 或 / 之一。此外,您将需要重载流插入运算符 << 以打印此类型的对象。必须定义构造函数并重载赋值运算符以允许从双精度数到复杂的隐式转换。还应包括您认为合适的任何其他方法。你的课程越完整越好。

Java 版本将没有那么多方法,因为 Java 不允许运算符重载或友元函数。同样,Java 类越完整越好。覆盖 toString() 方法。

Python 版本还应该包含用于从复合体转换为字符串的函数。

该项目所需的文件是:包含复杂类声明的 complex.h 文件,包含复杂类中声明的方法和函数的实现的 complex.cc 文件,实例化复数的 main.cc 和测试所有方法和函数、一个作为 Java 实现的 Complex.java 文件,以及一个实例化和测试 Complex 类的所有方法的 Main.java 文件。所需的 python 文件是一个 complex.py 文件。

他为我们提供了以下代码:

/*
 *
 *  Java version
 *
 */

/* Main.java */

public class Main {

    public static void main(String[] args) {

        Rational a = new Rational(1, 2);
        Rational b = new Rational(2, 3);

        int i = 5;

        System.out.println(a + " + " + b + " = " + a.add(b));
        System.out.println(a + " - " + b + " = " + a.sub(b));
        System.out.println(a + " * " + b + " = " + a.mul(b));
        System.out.println(a + " / " + b + " = " + a.div(b));

        System.out.println(a + " + " + i + " = " + a.add(i));
        System.out.println(a + " - " + i + " = " + a.sub(i));
        System.out.println(a + " * " + i + " = " + a.mul(i));
        System.out.println(a + " / " + i + " = " + a.div(i));
    }
}

/* Rational.java */

public class Rational {

    public Rational() {

        this(0);
    }

    public Rational(int num) {

        this(num, 1);
    }

    public Rational(int num, int den) {

        this.num = num;
        this.den = den;
    }

    public Rational add(Rational o) {

        return new Rational(num * o.den + o.num * den, den * o.den);
    }

    public Rational add(int n) {

        return new Rational(num + n * den, den);
    }

    public Rational div(Rational o) {

        return new Rational(num * o.den, den * o.num);
    }

    public Rational div(int n) {

        return new Rational(num, den * n);
    }

    public Rational mul(Rational o) {

        return new Rational(num * o.num, den * o.den);
    }

    public Rational mul(int n) {

        return new Rational(num * n, den);
    }

    public Rational sub(Rational o) {

        return new Rational(num * o.den - o.num * den, den * o.den);
    }

    public Rational sub(int n) {

        return new Rational(num - n * den, den);
    }

    public String toString() {

        return "(" + num + " / " + den + ")";
    }

    private int den;
    private int num;
}

/*
 *
 *  C++ version
 *
 */

/* rational.h */

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>

using std::ostream;

struct rational {

    rational(int = 0, int = 1);

    rational operator+(const rational &) const;
    rational operator-(const rational &) const;
    rational operator*(const rational &) const;
    rational operator/(const rational &) const;

    rational operator+(int) const;
    rational operator-(int) const;
    rational operator*(int) const;
    rational operator/(int) const;

    friend rational operator+(int, const rational &);
    friend rational operator-(int, const rational &);
    friend rational operator*(int, const rational &);
    friend rational operator/(int, const rational &);

    friend ostream &operator<<(ostream &, const rational &);

private:

    int den;
    int num;
};

#endif /* RATIONAL_H */

/* rational.cc */

#include <iostream>
#include "rational.h"

rational::rational(int num, int den) : num(num), den(den) {}

rational rational::operator+(const rational &o) const {

    return rational(num * o.den + o.num * den, den * o.den);
}

rational rational::operator+(int n) const {

    return rational(num + n * den, den);
}

rational rational::operator-(const rational &o) const {

    return rational(num * o.den - o.num * den, den * o.den);
}

rational rational::operator-(int n) const {

    return rational(num - n * den, den);
}

rational rational::operator*(const rational &o) const {

    return rational(num * o.num, den * o.den);
}

rational rational::operator*(int n) const {

    return rational(num * n, den);
}

rational rational::operator/(const rational &o) const {

    return rational(num * o.den, den * o.num);
}

rational rational::operator/(int n) const {

    return rational(num, den * n);
}

rational operator+(int n, const rational &o) {

    return o + n;
}

rational operator-(int n, const rational &o) {

    return rational(n) - o;
}

rational operator*(int n, const rational &o) {

    return o * n;
}

rational operator/(int n, const rational &o) {

    return rational(n) / o;
}

ostream &operator<<(ostream &out, const rational &o) {

    out << '(' << o.num << " / " << o.den << ')';
    return out;
}

/* main.cc */

#include <iostream>
#include "rational.h"

using std::cout;
using std::endl;

int main(void) {

    rational a(1, 2);
    rational b(2, 3);

    int i = 5;

    cout << a << " + " << b << " = " << a + b << endl;
    cout << a << " - " << b << " = " << a - b << endl;
    cout << a << " * " << b << " = " << a * b << endl;
    cout << a << " / " << b << " = " << a / b << endl;

    cout << a << " + " << i << " = " << a + i << endl;
    cout << a << " - " << i << " = " << a - i << endl;
    cout << a << " * " << i << " = " << a * i << endl;
    cout << a << " / " << i << " = " << a / i << endl;

    cout << i << " + " << a << " = " << i + a << endl;
    cout << i << " - " << a << " = " << i - a << endl;
    cout << i << " * " << a << " = " << i * a << endl;
    cout << i << " / " << a << " = " << i / a << endl;

    return 0;
}

#
#
# Python version
#
#

class rational:
    def __init__(self, num=0, den=1):
        self.num = num
        self.den = den

    def __add__(self, other):
        if isinstance(other, int):
            return rational(self.num + other * self.den, self.den)
        elif isinstance(other, rational):
            return rational(self.num * other.den + other.num * self.den, self.den * other.den)
        else:
            raise TypeError

    def __truediv__(self, other):
        if isinstance(other, int):
            return rational(self.num, self.den * other)
        elif isinstance(other, rational):
            return rational(self.num * other.den, self.den * other.num)
        else:
            raise TypeError

    def __float__(self):
        return float(self.num) / self.den

    def __int__(self):
        return self.num / self.den

    def __mul__(self, other):
        if isinstance(other, int):
            return rational(self.num * other, self.den)
        elif isinstance(other, rational):
            return rational(self.num * other.num, self.den * other.den)
        else:
            raise TypeError

    def __radd__(self, other):
        return self + other

    def __rtruediv__(self, other):
        return rational(other) / self

    def __rmul__(self, other):
        return self * other

    def __rsub__(self, other):
        return rational(other) - self

    def __str__(self):
        return '(' + str(self.num) + ' / ' + str(self.den) + ')'

    def __sub__(self, other):
        if isinstance(other, int):
            return rational(self.num - other * self.den, self.den)
        elif isinstance(other, rational):
            return rational(self.num * other.den - other.num * self.den, self.den * other.den)
        else:
            raise TypeError

if __name__ == '__main__':

    a = rational(1, 2)
    b = rational(2, 3)

    i = 5

    print('%s + %s = %s' % (a, b, a + b))
    print('%s - %s = %s' % (a, b, a - b))
    print('%s * %s = %s' % (a, b, a * b))
    print('%s / %s = %s' % (a, b, a / b))

    print('%s + %i = %s' % (a, i, a + i))
    print('%s - %i = %s' % (a, i, a - i))
    print('%s * %i = %s' % (a, i, a * i))
    print('%s / %i = %s' % (a, i, a / i))

    print('%i + %s = %s' % (i, a, i + a))
    print('%i - %s = %s' % (i, a, i - a))
    print('%i * %s = %s' % (i, a, i * a))
    print('%i / %s = %s' % (i, a, i / a))
4

4 回答 4

0

转换为字符串只是为了以人类可读的形式显示数字。那是在理性类中:return "(" + num + " / " + den + ")";例如打印(5 / 8)。您的复杂类可以使 toString 方法输出( 5 + 8i )

将一个复数乘以一个数字(双精度)是一个有效的运算,它会给出另一个复数作为结果。

将复数乘以实数:

(x + yi) u = xu + yu i.

使用他给出的代码,您可以完成完全相同的有理数分配(定义为两个数字 num 和 den)。您需要将该代码调整为复数,这些复数也被定义为几个数字。

PS提示:http ://www.clarku.edu/~djoyce/complex/mult.html

于 2012-12-12T09:37:12.357 回答
0

我不明白 double 是从哪里来的。

我会假设它是一个普通的double浮点值而不是一个复杂的值。

python版本也应该将复合体转换为字符串,我还是不明白它在问什么。

我假设,使用 String 而不是名为 Complex 的自定义类。

是说将复数(整数?)字面转换为字符串数据类型吗?

我会假设是这样,除非你知道。

于 2012-12-12T09:38:30.150 回答
0

复数包含实部和虚部,两者都存储为双精度数。既然您说您已经知道这些,我不会详细介绍,但是快速 google 会发现您几乎要使用的复数类的许多 Java 实现。

http://introcs.cs.princeton.edu/java/97data/Complex.java.html

是我以前用过的一个例子,它符合你的要求:

public Complex times(Complex b)
public Complex times(double alpha)

这些方法将采用复数或 alpha 应用于实部和虚部,并返回一个复数。

至于 python 字符串部分,我认为他希望你取实部和虚部,并以某种人类可读的形式表达它们:字符串

希望我能帮助 md_5

于 2012-12-12T09:39:41.897 回答
0

当作业说

op:复数 × 复数 → 复数 op:复数 × 双 → 复数 op:双数 × 复数 → 复数 其中 op 是 +、-、* 或 / 之一。

这意味着您必须在复数对之间以及实数和复数的混合对之间实现所有二进制运算。例如对于

*:复数×双→复数

你应该写这样的东西:

Complex operator*(Complex z, double t) {
  return Complex(t * z.Real(), t * z.Imag()); 
}
于 2013-01-20T20:46:05.717 回答