【问题标题】:Problem with using a Class in complex numbers calculator project在复数计算器项目中使用类的问题
【发布时间】:2021-07-07 09:52:28
【问题描述】:

我是 Thomas,是一名计算机科学专业的学生。对于我最新的 Python OOP 项目,我一直在尝试做一个简单的复数计算器。

我的任务是用 Python 制作一个 2-class 项目

首先是类有3个方法:

1st - 创建一个复数 2nd - 显示一个复数 3rd - 删除复数

第二类有四种方法:加法、减法、乘法和除法。 我做了第一堂课,它的方法,我认为它们在基本层面上工作得很好(当然代码是原始的,它需要做很多工作:()

我偶然发现了第二节课的问题。我不能正确使用方法:加、减、乘和除。 我想要使​​用以前创建的两个复数的数学方法,但我找不到正确完成任务的方法。

感谢任何帮助。谢谢!

我的代码:

class Complex (object):
    def __init__(self, real, imaginary):
        self.real = real
        self.imaginary = imaginary

    def create_number(self):
        self.real = (float(input("Enter a real part of the complex number")))
        self.imaginary = (float(input("Enter an imaginary part of the complex number")))
        return Complex(self.real, self.imaginary)

    def display_number(self):
        print(f"Your complex number is: {Complex(self.real, self.imaginary)}")

    def erase_number(self):
        (self.real, self.imaginary) = (0, 0)
        print(f"Number is erased: {(self.real, self.imaginary)}")

    def __str__(self):
        if type(self.real) == int and type(self.imaginary) == int:
            if self.imaginary >= 0:
                return '%d+%di' % (self.real, self.imaginary)
            elif self.imaginary < 0:
                return '%d%di' % (self.real, self.imaginary)
        else:
            if self.imaginary >= 0:
                return '%f+%fi' % (self.real, self.imaginary)
            elif self.imaginary < 0:
               return '%f%fi' % (self.real, self.imaginary)


class Calculator(object):
    def __init__(self, real, imaginary):
        self.real = real
        self.imaginary = imaginary

    def __add__(self, other):
        result_real = self.real+other.real
        result_imaginary = self.imaginary+other.imaginary
        result = Complex(result_real, result_imaginary)
        return result

    def __sub__(self, other):
        result_real = self.real-other.real
        result_imaginary = self.imaginary-other.imaginary
        result = Complex(result_real, result_imaginary)
        return result

    def __mul__(self, other):
        result_real = (self.real*other.real-self.imaginary*other.imaginary)
        result_imaginary = (self.real*other.imaginary+other.real*self.imaginary)
        result = Complex(result_real, result_imaginary)
        return result

    def __truediv__(self, other):
        result_real = float(float(self.real*other.real+self.imaginary*other.imaginary)/float(other.real*other.real+other.imaginary*other.imaginary))
        result_imaginary = float(float(other.real*self.imaginary-self.real*other.imaginary)/float(other.real*other.real+other.imaginary*other.imaginary))
        result = Complex(result_real, result_imaginary)
        return result


c1 = Complex(0, 0)
c2 = Complex(0, 0)

calc1 = Calculator(0, 0)
calc2= Calculator(0, 0)

choice = 1
while choice != 0:
    print("0. Exit")
    print("1. Construction of a complex number 1")
    print("2. Construction of a complex number 2")
    print("3. Display complex number 1")
    print("4. Display complex number 2")
    print("5. Erase complex number 1")
    print("6. Erase complex number 2")
    print("7. Addition")
    print("8. Subtraction")
    print("9. Multiplication")
    print("10. Division")
    choice = int(input("Enter choice: "))

    if choice == 1:
        print("Result: ", c1.create_number())
    elif choice == 2:
        print("Result: ", c2.create_number())
    elif choice == 3:
        print("Result: ", c1.display_number())
    elif choice == 4:
        print("Result: ", c2.display_number())
    elif choice == 5:
        print("Result: ", c1.erase_number())
    elif choice == 6:
        print("Result: ", c2.erase_number())
    elif choice == 7:
        print("Result: ", calc1.__add__(calc2))
    elif choice == 8:
        print("Result: ", calc1.__sub__(calc2))
    elif choice == 9:
        print("Result: ", calc1.__mul__(calc2))
    elif choice == 10:
        print("Result: ", calc1.__truediv__(calc2))
    elif choice == 0:
        print("Exiting!")
    else:
        print("Invalid choice!!")

【问题讨论】:

  • “我无法正确使用方法:添加,...” - 你的问题到底是什么?
  • 例如,当我在 if 循环中使用选项 7 时,add 方法返回值 0.0。我找不到创建正确 Calculator 对象的方法,或者代码可能需要更改,因为我找不到解决问题的方法。
  • 一些关于接近 OOP 的一般建议:尝试将关系建模为现实世界中的关系。在这种情况下:用户将只有一个计算器,并且计算器应该创建/记住用户可以输入的任何数字
  • 谢谢您,这是一个宝贵的建议。可悲的是,目前,我正在努力让我的代码正常工作,但将来我的代码将反映现实生活中的规则:)

标签: python oop complex-numbers


【解决方案1】:

目前 Calculator 和 Complex 对象之间没有关系。 Calculator 的实部/虚部属性初始化为 0,但从未更新。

解决此问题的一种方法是使用对 Complex 对象的引用来初始化 Calculator:

class Calculator(object):
    def __init__(self, complex_number):
        self.complex_number = complex_number

    def __add__(self, other):
        result_real = self.complex_number.real+other.complex_number.real
        result_imaginary = self.complex_number.imaginary+other.complex_number.imaginary
        result = Complex(result_real, result_imaginary)
        return result
    # other methods should be modified as appropriate..


c1 = Complex(0, 0)
c2 = Complex(0, 0)

calc1 = Calculator(c1)
calc2= Calculator(c2)

用户将在调用 create_number 时更新 c1,当用户调用 add 时,计算器将引用该(现已更新)复杂对象。

【讨论】:

  • 当我从 Calculator 方法中删除实部和虚部参数时,我在 Calculator 类中的其他方法:add、sub。 mul、truediv 不可用,因为方法使用它们进行计算。你能告诉我一个正确的“添加”类应该是什么样子吗?此外,我注意到您添加到类中的复杂对象的名称以小写字母开头。我知道,按照规则,它应该是小写的,但我创建的对象以大写开头。它可以与小写一起使用吗?
  • 我已经编辑了答案以显示 add 方法的外观。小写的属性名称应该可以工作,但我也对其进行了更改更明显的是它指的是别的东西。
  • 终于成功了。我之前摆弄过 complex_number 参数,但有一天我放弃了,决定寻求帮助。我不知道我需要多长时间才能理解这个问题。谢谢 !现在我将尝试做你之前提到的事情:'一个用户只有一个计算器,计算器应该创建/记住用户可能输入的任何数字'
【解决方案2】:

这可能无法回答您的问题,但无论如何都是有效的知识。

__add__ 系列方法(“魔术方法”)仅供 Python 内部使用。

当你写a + b 时,python 会检查a 是否恰好有一个__add__ 方法,并使用它,或者如果没有使用b__radd__ 方法。

所以人们应该简单地间接“使用”这些方法:print("Result: ", calc1 + calc2)

【讨论】:

  • 谢谢。我会阅读它:) 在我的情况下 print("Result: ", calc1 + calc2) 仍然是 0.0,因为我无法创建正确的 Calculator() 对象来使用以前创建的复数
  • 好的,我明白你所说的“当你写 a + b 时,python 会检查 a 是否有一个 add 方法,并使用它,或者如果没有使用 b 的 radd 方法。"谢谢,这是宝贵的一课。
猜你喜欢
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 2015-07-05
  • 2019-04-21
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多