增强版本:支持批量求解 14.4

Python
def advanced_equation_solver():
    """增强版方程组求解器"""
    print("=== 高级二元一次方程组求解器 ===\n")
    
    while True:
        print("1. 求解单个方程组")
        print("2. 批量求解方程组") 
        print("3. 退出程序")
        
        choice = input("\n请选择功能(1-3): ")
        
        if choice == '1':
            solve_single_equation()
        elif choice == '2':
            solve_multiple_equations()
        elif choice == '3':
            print("感谢使用,再见!")
            break
        else:
            print("无效选择,请重新输入!")

def solve_single_equation():
    """求解单个方程组"""
    # ...(同上一个函数的实现)

def solve_multiple_equations():
    """批量求解方程组"""
    print("\n--- 批量求解模式 ---")
    equations = []
    
    # 输入多个方程组
    while True:
        print(f"\n请输入第{len(equations)+1}个方程组的系数:")
        try:
            a1 = float(input("a1: "))
            b1 = float(input("b1: "))
            c1 = float(input("c1: "))
            a2 = float(input("a2: "))
            b2 = float(input("b2: "))
            c2 = float(input("c2: "))
            
            equations.append((a1, b1, c1, a2, b2, c2))
            
            cont = input("是否继续输入?(y/n): ")
            if cont.lower() != 'y':
                break
                
        except ValueError:
            print("输入错误,请重新输入!")
    
    # 批量求解
    print(f"\n开始求解{len(equations)}个方程组...")
    for i, eq in enumerate(equations, 1):
        a1, b1, c1, a2, b2, c2 = eq
        print(f"\n{i}个方程组:")
        print(f"{a1}x + {b1}y = {c1}")
        print(f"{a2}x + {b2}y = {c2}")
        
        D = a1 * b2 - a2 * b1
        if abs(D) > 1e-10:
            x = (c1 * b2 - c2 * b1) / D
            y = (a1 * c2 - a2 * c1) / D
            print(f"解:x = {x:.4f}, y = {y:.4f}")
        else:
            print("无唯一解")

您可能还喜欢...

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注