私はクラスの目的を掴むのに困っています。私はこの課題を適切に完了できませんでしたが、試験のためにこれを知る必要があります。私は現在、受信しています出力はクラスアクティビティに固執します。簡単なレッスンを探しています。複雑な数字
(7,1)
(7,1)
(7,1)
明らかにこれは正しくありませんされています。誰かがこの問題の考え方を説明できるとすれば、私はとても感謝しています。私はそれを理解することができませんでした。 (コーディングに新しく、教師はこの教材を教えていないが、宿題についてこの質問を割り当てた)
関数名と変数は変更できません。ありがとうございました。
# Goal: complete the three functions multiply, divide and power below
#
# - self is Complex, which is a complex number whose Re is self.real and
# Im is self.imag
# - multiply(c) replaces self by self times c, where c is Complex
# - divide(c) is to replace self by self divided by c
# - power(n) is to replace self by self to the power n
#
# See https://en.wikipedia.org/wiki/Complex_number
#
# Submission:
# file name: EX9_8.py
# Do not change the function and class names and parameters
# Upload to Vocareum by the due date
#
#
import math
class Complex:
def __init__ (self, r, i):
self.real = r
self.imag = i
# Copy the three functions from Activity 1 of vLec9_8
def magnitude(self):
return math.sqrt(self.real**2 + self.imag**2)
def conjugate(self):
self.imag *= -1
def add(self, c):
if not isinstance(c, Complex):
return "Type Mismatch Error"
self.real += c.real
self.imag += c.imag
#The Following Functions are the assignment I am working on. The above functions were already given.
def multiply(self, c):
new = (self.real + self.imag) * (c.real + c.imag)
return new
#Procedural method: multiply self by c to change self where c is Complex
def divide(self, c):
new2 = ((self.real + self.imag)*(c.real - c.imag))/((c.real + c.imag)*(c.real - c.imag))
return new2
#Procedural method: divide self by c to change self where c is Complex
def power(self, n):
new3 = math.cos(n.real) + ((n.imag)*(math.sin(n.real)))
return new3
# Procedural method: take self to the power n to change self,
# where n is a positive integer
# test code
c1 = Complex(3,2)
c2 = Complex(4,-1)
c1.add(c2)
print((c1.real, c1.imag))
c1.power(4)
print((c1.real, c1.imag))
c1.divide(c2)
print((c1.real, c1.imag))
# should prints
#(7, 1)
#(2108, 1344)
#(416.94117647058823, 440.2352941176471)
実際、 'conjugate'と' add'以外のクラスメソッドは 'self.real'と' self.imag'を変更していないので、(7,1) - 正しい出力 –
"... (7、1)、(7、1)、(7、1)..これは明らかに正しくありません。 – davedwards