2017-09-13 13 views
0

私はクラスの目的を掴むのに困っています。私はこの課題を適切に完了できませんでしたが、試験のためにこれを知る必要があります。私は現在、受信しています出力はクラスアクティビティに固執します。簡単なレッスンを探しています。複雑な数字

(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) 
+0

実際、 'conjugate'と' add'以外のクラスメソッドは 'self.real'と' self.imag'を変更していないので、(7,1) - 正しい出力 –

+0

"... (7、1)、(7、1)、(7、1)..これは明らかに正しくありません。 – davedwards

答えて

0

提供されているaddメソッドのように、作成しているメソッドを構造化する必要があります。 returnではなく、self.realself.imagに新しい値を割り当てる必要があります。乗算、除算、累乗の計算は加算の場合よりも少し複雑ですので、計算中に値を保持するための一時変数が必要になることがあります。

あなたのための全体のことを行うのではなく、私はちょうどあなたが他人のために使用できるフレームワーク与えるmultiplyやる:部門の

def multiply(self, c): 
    if not isinstance(c, Complex):  # type checking code copied from add() 
     return "Type Mismatch Error" # raising an exception would probably be better 

    new_real = self.real * c.real - self.imag * c.imag 
    new_imag = self.real * c.imag + c.real * self.imag 

    self.real = new_real 
    self.imag = new_imag 

を、あなたが保持するため、他の一時変数をすることができcに複素共役を掛けて得られる本当の価値です。新しい値の両方の式は、その中間値を使用できます。

関連する問題