复数类

时间:2012-12-12 09:30:13

标签: java c++ python

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

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

一倍?我不明白双重问题在哪里。另外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文件   实现,以及实例化和测试所有的Main.java文件   Complex类的方法。所需的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 个答案:

答案 0 :(得分:0)

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

将复数乘以数字(double)是一个有效的操作,它会产生另一个复数。

将复数乘以实数:

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

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

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

答案 1 :(得分:0)

  

我不明白double在哪里。

我认为它是一个普通的double浮点值,而不是复数值。

  

此外,python版本应该将复合体转换为字符串,我再也不明白它的要求。

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

  

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

除非你另有所知,否则我会这样认为。

答案 2 :(得分:0)

复数包含实部和虚部,均存储为双精度。既然你说你已经知道了这些,我就不会详细介绍了,但是快速谷歌揭示了你几乎要完成的复杂数字类的许多Java实现。

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

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

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

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

对于python字符串部分,我认为他希望你采用真实和想象的部分,并以一种人类可读的形式表达它们:一个字符串

希望我能帮到你 md_5

答案 3 :(得分:0)

当受让人说

  

op:Complex×Complex→Complex   op:复合×双→复合操作:   double×Complex→Complex   其中op是+, - ,*或/之一。

这意味着您必须在复数的夫妻之间以及在Real和Complex的混合夫妻之间实施所有二元操作。例如

*:复杂×双→复杂

你应该写一些类似的东西:

Complex operator*(Complex z, double t) {
  return Complex(t * z.Real(), t * z.Imag()); 
}