2013-03-07 49 views
0

我試圖完成該程序,但答案是錯誤的,我無法精確確定究竟是什麼。基本python程序

問題:給定兩條直線(y = mx + b)的方程,確定兩條直線是平行的,相同的還是相交的。計算並輸出交點。

我的代碼:

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

這是一個功課問題嗎? :D – crayzeewulf 2013-03-07 22:08:59

+0

你給我們沒有關於什麼是錯的信息... – 2013-03-07 22:09:35

+0

crayzeewulf-no即時通訊只是學習如何編程,這使我瘋狂 – 2013-03-07 22:10:02

回答

1

時使用的計算poi_x是錯誤的等式。此外,您的代碼用於計算poi_y的公式已互換mb。這裏有一個稍微修改代碼,應該幫助:

#! /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) 
+0

哇謝謝你! 但是它爲什麼輸出「2.0 3.0 -0.5 7.0」 如果我只想要POI?我會怎麼做? 再次感謝:) – 2013-03-07 23:29:37

+0

另外如果輸入是y = x和y = -x? 我會如何做這項工作? – 2013-03-07 23:33:17

+0

我想我會讓你自己把這兩件事情弄清楚。 [嘗試第一個](http://mattgemmell.com/2008/12/08/what-have-you-tried/):D – crayzeewulf 2013-03-07 23:46:12

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] 
+0

它仍然輸出錯誤的答案。 y = 2x + 3和y = -0.5x + 7 POI應該是(1.6,2) 我的程序: 您的方程相交於象限3(-1.60,-0.20) – 2013-03-07 22:19:12

+0

要修復x相交,需要檢查if(equation_intersect_x <0)equation_intersect_x = equation_intersect_x * -1。仍然看着y相交的問題。編輯:其實這也修復了y相交。這是你的問題。 – Gremio 2013-03-07 22:30:53

+0

是的,Crayzeewulf's是一個更清潔的解決方案。翻轉三角洲。其餘的將自行解決。 – Gremio 2013-03-07 22:37:47

0

第一對的建議:

打印back(之前的第一個「if」語句)執行操作之前的方程給用戶:

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

重或字符串函數分裂和條可以比字符串索引更容易:

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 = (m1)* x1 + b1 = y1 = y2 =(m2)* x2 + b2 找到X,其中兩個Y都相等:因此:
但在感興趣點(X)x1 = x2 重寫:(m1 + m2)* X = b2 -b1 重寫:X =(b2 - b1)/(m1 + m2)

現在我們可以看到這與您的equation_intersect x公式不匹配。