0

我试图完成程序,但答案是错误的,我无法确定到底是什么。

问题:给定两条线的方程(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)`
4

3 回答 3

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)
于 2013-03-07T22:31:57.787 回答
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

现在剩下的就是代数:

先手工做一个hard case:

假设它们不平行或相同:找到 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_intersect x 公式不匹配。

于 2013-03-07T22:58:49.653 回答
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 回答