我试图完成程序,但答案是错误的,我无法确定到底是什么。
问题:给定两条线的方程(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)`