0

プログラムを完成させようとしましたが、答えが間違っており、正確に何を特定することができません。

質問:2本の線の方程式(y = mx + b)が与えられた場合、2本の線が平行であるか、同じであるか、交差しているかを判断します。交点を計算して出力します。

私のコード:

equation_1 =raw_input("Please enter the equation of your 1st line(y=mx+b): ")
equation_2 =raw_input("Please enter the equation of your 2nd line(y=mx+b): ")

plus_1 = equation_1.find('+')
plus_2 = equation_2.find('+')

x_1 = equation_1.find('x')
x_2 = equation_2.find('x')

equalsign_1 = equation_1.find('=')
equalsign_2 = equation_2.find('=')

b1 = equation_1[x_1+1:]
b2 = equation_2[x_2+1:]

m1 = equation_1[equalsign_1+1:x_1]
m2 = equation_2[equalsign_2+1:x_2]

if m1==m2 and b1!=b2:
    print "Your equations are parallel. "

elif m1==m2 and b1==b2:
    print "Your equations are the same. "

else: 
    equation_intersect_y = float(b2)-float(b1)
    equation_intersect_x = float(m2)-float(m1) # equation_intersect_x = float(m1)-float(m2)

    poi_x = float(equation_intersect_y)/float(equation_intersect_x)
    poi_y = float(b1)*float(poi_x)+float(m1)`
4

3 に答える 3

1

計算に使用した方程式poi_xが間違っています。また、計算poi_yに使用mしたコードの数式が入れ替わっていますb。役立つはずのわずかに変更されたコードを次に示します。

#! /usr/bin/env python
equation_1 ="y=2x+3"
equation_2 ="y=-0.5x+7"

plus_1 = equation_1.find('+')
plus_2 = equation_2.find('+')

x_1 = equation_1.find('x')
x_2 = equation_2.find('x')

equalsign_1 = equation_1.find('=')
equalsign_2 = equation_2.find('=')

b1 = float(equation_1[x_1+1:])
b2 = float(equation_2[x_2+1:])

m1 = float(equation_1[equalsign_1+1:x_1])
m2 = float(equation_2[equalsign_2+1:x_2])

print m1,b1,m2,b2

if m1==m2 and b1!=b2:
    print "Your equations are parallel. "

elif m1==m2 and b1==b2:
    print "Your equations are the same. "

else:
    equation_intersect_y = b2 - b1
    equation_intersect_x = m1 - m2

    poi_x = equation_intersect_y/equation_intersect_x
    poi_y = m1*poi_x+b1

    print poi_x, poi_y

出力は次のとおりです。

2.0 3.0 -0.5 7.0
1.6 6.2

そして、繰り返しを減らす少し良いコードがあります:

#! /usr/bin/env python
def parse_equation_string(eq_string):
    x_pos = eq_string.find('x')
    equal_pos = eq_string.find('=')

    b = float(eq_string[x_pos+1:])
    m = float(eq_string[equal_pos+1:x_pos])
    return m, b

def get_point_of_intersection(line1, line2):
    m1, b1 = line1
    m2, b2 = line2

    if m1==m2 and b1!=b2:
        return "The lines are parallel. "

    elif m1==m2 and b1==b2:
        return "The lines are the same. "

    else:
        equation_intersect_y = b2 - b1
        equation_intersect_x = m1 - m2

        poi_x = equation_intersect_y/equation_intersect_x
        poi_y = m1*poi_x+b1

        return poi_x, poi_y

equation_1 = "y=2x+3"
equation_2 = "y=-0.5x+7"

line_1 = parse_equation_string(equation_1)
line_2 = parse_equation_string(equation_2)

print line_1, line_2
print get_point_of_intersection(line_1, line_2)

出力は次のとおりです。

(2.0, 3.0) (-0.5, 7.0)
(1.6, 6.2)
于 2013-03-07T22:31:57.787 に答える
0

すべきではない

b1 = equation_1[x_1+1:]
b2 = equation_2[x_2+1:]

なれ

b1 = equation_1[plus_1+1:]
b2 = equation_2[plus_2+1:]

または

b1 = equation_1[x_1+2:]
b2 = equation_2[x_2+2:]

また、私は思います

m1 = equation_1[equalsign_1+1:x_1]
m2 = equation_2[equalsign_2+1:x_2]

する必要があります

m1 = equation_1[equalsign_1+1:x_1-1]
m2 = equation_2[equalsign_2+1:x_2-1]
于 2013-03-07T22:12:39.330 に答える
0

最初のいくつかの提案:

操作を実行する前に(最初の「if」ステートメントの前に)方程式をユーザーに出力します。

print "Equation 1  y={}x+{}".format(m1, b1)
print "Equation 2  y={}x+{}".format(m2, b2)

reまたは文字列関数splitおよびstripは、文字列インデックスよりも簡単な場合があります。

m1 = equation_1.split('=')[1].split('x')[0]
b1 = equation_1.split('=')[1].split('+')[1]

より難しいテストケースの前に、プログラムにいくつかの簡単なテストケースを与えます:1:y = 0x + -3 2:y = 1x+0はX=3で交差します

1: y=-1x+0
2: y=0x+3
intersects at  X = -3


1: y=2x+2
2: y=-2x+0
intersects at X = -0.5

残っているのは代数だけです。

最初に手作業でハードケースを作成します。

それらが平行または同じではないと仮定します:x1=x2およびy1=y2である点を見つけます両方のYが等しいXを見つけます:したがって:
(m1)* x1 + b1 = y1 = y2 =(m2)* x2+b2書き換えXを見つけるには:(m1)* x1 + b1 =(m2)* x2 + b2ただし、対象のポイント(X)x1 = x2の書き換え:(m1 + m2)* X = b2 -b1の書き換え:X =(b2 --b1)/(m1 + m2)

これで、equation_intersectx式と一致しないことがわかります。

于 2013-03-07T22:58:49.653 に答える