2016-07-29 6 views
0
def roots4(a,b,c,d): 
    d = b * b - 4 * a * c 
    if a != 0 and d == 0: 
     roots4(a,b,c,d) 
     x = -b/ (2*a) 
    if a != 0 and d > 0: 
     roots4(a,b,c,d) 
     x1 = (-b + math.sqrt(d))/2.0/a 
     x2 = (-b - math.sqrt(d))/2.0/a 
    if a != 0 and d < 0: 
     roots4(a,b,c,d) 
     xre = (-b)/(2*a) 
     xim = (math.sqrt(d))/ (2*a) 
     print x1 = xre + ixim 
     strx1 = "x2 = %6.2f + i %6.2f" %(xre, xim) 
     print strx1 

이것은 프로젝트 코드입니다. 제가하려는 것은 roots4(a,b,c,d)을 정의하는 것입니다. 예를 들어 a != 0 and d == 0 인 경우 roots4(a,b,c,d)x = -b/ (2*a)을 풀어 x을 찾아야합니다. 등등 ... 내가 뭘 잘못하고 있는지 모르겠다. 어떤 팁?if 문을 사용하여 함수 정의하기

+3

왜 재귀 호출 잘 될 때 d < 0 경우에만로 인쇄하려면? 원하는 효과, 솔루션 인쇄 또는 반환 무엇입니까? – LutzL

+0

매개 변수로 d를 전달할 필요가 없습니다. – zezollo

+0

'a == 0'이면 무엇을합니까? 방정식 "x = -b/(2 * a)"를 풀면 무슨 뜻입니까? (해결할 수있는 방정식이 아직도 있습니까?) – zezollo

답변

0

의견과 지미의 답변에서 지적한 것처럼 재귀 호출이 필요하지 않습니다. 여기

당신이 그것을 수정 (그것은 당신이 좋아하는 방식을 적용) 수있는 방법의 예 :

출력
#!/usr/bin/env python2 
# -*- coding: utf-8 -*- 

import math 


def roots4(a, b, c): 
    """Prints the solutions of ax² + bx + c = 0, if a != 0""" 
    if a == 0: 
     print 'This function is meant to solve a 2d degree equation, '\ 
      'not a 1st degree one.' 
    else: 
     d = b * b - 4 * a * c 
     solutions = [] 
     if d == 0: 
      solutions = [str(-b/(2 * a))] 
     elif d > 0: 
      solutions = [str((-b + math.sqrt(d))/2.0/a), 
         str((-b - math.sqrt(d))/2.0/a)] 
     elif d < 0: 
      xre = str((-b)/(2 * a)) 
      xim = str((math.sqrt(-d))/(2 * a)) 
      solutions = [xre + " + i" + xim, 
         xre + " - i" + xim] 

     print "\nEquation is: {}x² + {}x + {} = 0".format(a, b, c) 

     if len(solutions) == 1: 
      print "There's only one solution: " + solutions[0] 
     else: 
      print "Solutions are: " + " and ".join(solutions) 

roots = [(0.0, 0.0, 0.0), 
     (0.0, 0.0, 1.0), 
     (0.0, 2.0, 4.0), 
     (1.0, 2.0, 1.0), 
     (1.0, -5.0, 6.0), 
     (1.0, 2.0, 3.0)] 

for r in roots: 
    roots4(*r) 

:

$ ./test_script2.py 
This function is meant to solve a 2d degree equation, not a 1st degree one. 
This function is meant to solve a 2d degree equation, not a 1st degree one. 
This function is meant to solve a 2d degree equation, not a 1st degree one. 

Equation is: 1.0x² + 2.0x + 1.0 = 0 
There's only one solution: -1.0 

Equation is: 1.0x² + -5.0x + 6.0 = 0 
Solutions are: 3.0 and 2.0 

Equation is: 1.0x² + 2.0x + 3.0 = 0 
Solutions are: -1.0 + i1.41421356237 and -1.0 - i1.41421356237 
+0

나는 이것을 정확하게하고있다! 예! 내 뿌리를 제외하고 있습니다 –

+0

(0.0, 0.0, 0.0) \t (0.0, 0.0, 1.0) \t (0.0, 2.0, 4.0) \t (1.0, 2.0, 1.0) \t (1.0, -5.0, 6.0) \t (1.0, 2.0, 3.0) –

+0

튜플로 뿌리를 얻었습니까? – zezollo

0

자, 재귀 함수로 무한 루프를 만든 것처럼 보입니다. 보이는 것처럼 지금은

d = b * b - 4 * a * c 

또 다시 떨어져 어떤 계산을하지 않습니다.

프로그램 흐름을 고려하십시오.

에 도달 할 때마다
roots4(a,b,c,d) 

까지 다시 올라갑니다.

1

당신은 아마

... 
      roots4(a,b,c,d) 
    ... 

이 적층 무한 루프가 발생합니다.

첫째, 왜 재귀 호출이 필요합니까? d의 매개 변수는 무엇입니까?

둘째, ixim은 무엇입니까? xim * 1j과 같아야합니까? print x1 = xre + ixim에서 무엇을 기대합니까?

이이

from math import sqrt 


    def roots4(a,b,c): 
     if a != 0.: 
      x_left = -b/(2*a) 

      d = b * b - 4 * a * c 
      if d == 0.: 
       x_right = 0. 
      elif d > 0.: 
       x_right = sqrt(d)/(2 * a) 
      else: 
       xim = sqrt(-d)/(2 * a) 
       strx1 = "x1 = %6.2f + i %6.2f" %(x_left, xim) 
       print strx1 
       strx2 = "x2 = %6.2f - i %6.2f" %(x_left, xim) 
       print strx2 
       x_right = xim * 1j 
      x1 = x_left + x_right 
      x2 = x_left - x_right 
     else: 
      raise ValueError("incorrect leading coefficient in given square equation") 
+0

'x1, x2'를 반환하려면 계산 직후에'return x1, x2'와 같은 것을 써야합니다 –