#大家可以看一下 两道题的内容
第n次刷第4章第3题的代码。朝纲用来函数的概念
def judge_num(a, b, c, d):
return (a * d) - (b * c)
def run_cont(a, b, c, d, e, f):
cc = judge_num(a, b, c, d)
if cc == 0:
print("The equation has no solution")
else:
x = ((e * d) - (b * f)) / cc
y = ((a * f) - (e * c)) / cc
print("x is {:.2f} and y is {:.2f}".format(x, y))
run_cont(9, 4, 3, -5, -6, -21)
run_cont(1, 2, 2, 4, 4, 5)
接下来是main代码段
def main():
run_count1 = exCode07.linear_equation(9, 4, 3, -5, -6, -21)
run_count2 = exCode07.linear_equation(1, 2, 2, 4, 4, 5)
print(run_count1.method_between())
print(run_count2.method_between())
main()
接下来是linearEquation代码
但是类的名字已改为linea_equation
class linear_equation:
def __init__(self, a, b, c, d, e, f):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
def set_a(self, a):
self.a = a
def get_a(self):
return self.a
def set_b(self, b):
self.b = b
def get_b(self):
return self.b
def set_c(self, c):
self.c = c
def get_c(self):
return self.c
def set_d(self, d):
self.d = d
def get_d(self):
return self.d
def set_e(self, e):
self.e = e
def get_e(self):
return self.e
def set_f(self, f):
self.f = f
def get_f(self):
return self.f
def is_solvable(self):
a1 = self.a * self.d
a2 = self.b * self.c
zero_method = a1 - a2
if zero_method != 0:
return a1 - a2
#这个代码如果少了影响运行
else:
return False
def get_x(self):
a_1 = self.e * self.d
a_2 = self.b * self.f
return (a_1 - a_2) / linear_equation.is_solvable(self)
def get_y(self):
a_1 = self.a * self.f
a_2 = self.e * self.c
return (a_1 - a_2) / linear_equation.is_solvable(self)
def method_between(self):
if linear_equation.is_solvable(self) != 0:
a = linear_equation.get_x(self)
b = linear_equation.get_y(self)
return f"x:{a} and y:{b}"
else:
return "This is not result"