2013-03-07 46 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公式不匹配。