【Python】方程计算器

安装依赖:

requirements.txt:

复制代码
sympy>=1.12
rich>=13.0
PyQt6>=6.0
matplotlib>=3.5

安装:

bash 复制代码
pip install -r requirements.txt

源代码:

python 复制代码
from sympy import symbols, sympify, solve, Eq, S, Rational, sqrt
from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
from rich.prompt import Prompt
from rich import print as rprint
import re
import random

console = Console()

transformations = standard_transformations + (implicit_multiplication_application,)

def display_welcome():
    welcome_text = Text()
    welcome_text.append("🧮 ", style="bold yellow")
    welcome_text.append("方程计算器", style="bold cyan")
    welcome_text.append(" - 欢迎使用! ", style="bold white")
    welcome_text.append("🧮", style="bold yellow")
    
    panel = Panel(
        welcome_text,
        title="[bold magenta]✨ 交互式方程求解器 ✨[/bold magenta]",
        title_align="center",
        border_style="bright_blue",
        padding=(1, 2)
    )
    console.print(panel)
    
    console.print("\n[green]📌 功能说明:[/green]")
    console.print("  • 支持输入多个方程(用分号 ; 分隔)")
    console.print("  • 支持指定变量(用逗号 , 分隔)")
    console.print("  • 最多显示 10 组解")
    console.print("  • 输入 'example' 获取随机方程示例")
    console.print("  • 输入 'quit' 或 'exit' 退出程序")
    console.print("\n[yellow]📌 注意事项:[/yellow]")
    console.print("  • 乘法必须用 * (如: 3*x)")
    console.print("  • 幂运算用 ** (如: x**2)\n")

def generate_linear_equation():
    a = random.randint(1, 10)
    b = random.randint(-10, 10)
    c = random.randint(-10, 10)
    equation = f"{a}*x + {b} = {c}"
    variables = "x"
    return equation, variables

def generate_quadratic_equation():
    a = random.randint(1, 5)
    b = random.randint(-10, 10)
    c = random.randint(-10, 10)
    equation = f"{a}*x**2 + {b}*x + {c} = 0"
    variables = "x"
    return equation, variables

def generate_cubic_equation():
    a = random.randint(1, 3)
    b = random.randint(-5, 5)
    c = random.randint(-10, 10)
    d = random.randint(-10, 10)
    equation = f"{a}*x**3 + {b}*x**2 + {c}*x + {d} = 0"
    variables = "x"
    return equation, variables

def generate_linear_system():
    a1 = random.randint(1, 5)
    b1 = random.randint(1, 5)
    c1 = random.randint(1, 10)
    a2 = random.randint(1, 5)
    b2 = random.randint(1, 5)
    c2 = random.randint(1, 10)
    equation = f"{a1}*x + {b1}*y = {c1}; {a2}*x + {b2}*y = {c2}"
    variables = "x,y"
    return equation, variables

def generate_quadratic_system():
    a = random.randint(1, 3)
    b = random.randint(1, 3)
    c = random.randint(1, 10)
    d = random.randint(1, 3)
    e = random.randint(1, 3)
    f = random.randint(1, 10)
    equation = f"x + y = {c}; x*y = {f}"
    variables = "x,y"
    return equation, variables

def generate_random_example():
    examples = [
        ("一次方程", generate_linear_equation),
        ("二次方程", generate_quadratic_equation),
        ("三次方程", generate_cubic_equation),
        ("一次方程组", generate_linear_system),
        ("二次方程组", generate_quadratic_system)
    ]
    
    choice = random.choice(examples)
    equation, variables = choice[1]()
    return choice[0], equation, variables

def display_examples():
    console.print("\n" + "="*50)
    console.print("[bold cyan]🎲 随机生成方程示例 🎲[/bold cyan]")
    console.print("="*50 + "\n")
    
    for i in range(3):
        if i == 2:
            type_name = "复杂三次方程"
            equation, variables = generate_cubic_equation()
        else:
            type_name, equation, variables = generate_random_example()
        console.print(f"[yellow]示例 {i+1} - {type_name}:[/yellow]")
        console.print(f"  方程: [cyan]{equation}[/cyan]")
        console.print(f"  变量: [green]{variables}[/green]\n")
    
    console.print("="*50 + "\n")

def parse_equation(equation_str, variables_str):
    equations = []
    eq_list = [eq.strip() for eq in equation_str.split(';') if eq.strip()]
    
    var_list = [v.strip() for v in variables_str.split(',') if v.strip()]
    var_symbols = symbols(var_list)
    
    for eq in eq_list:
        if '=' in eq:
            left, right = eq.split('=', 1)
            left_expr = parse_expr(left.strip(), transformations=transformations)
            right_expr = parse_expr(right.strip(), transformations=transformations)
            equations.append(Eq(left_expr, right_expr))
        else:
            equations.append(parse_expr(eq.strip(), transformations=transformations))
    
    return equations, var_symbols

def solve_equation(equations, var_symbols):
    try:
        solutions = solve(equations, var_symbols, dict=True)
        return solutions
    except Exception as e:
        console.print(f"[yellow]⚠️  求解过程出错: {str(e)}[/yellow]")
        return None

def display_solutions(solutions, var_symbols):
    if not solutions:
        console.print("\n[red]❌ 无解或无法求解![/red]")
        return
    
    if len(solutions) == 0:
        console.print("\n[red]❌ 方程无解![/red]")
        return
    
    if solutions[0] == {}:
        console.print("\n[yellow]⚠️  方程有无穷多解![/yellow]")
        return
    
    display_count = min(len(solutions), 10)
    
    table = Table(title="[bold cyan]📊 求解结果 📊[/bold cyan]", show_header=True, header_style="bold magenta")
    table.add_column("序号", style="cyan", justify="center", width=6)
    
    for var in var_symbols:
        table.add_column(f"{var}", style="green", justify="center")
    
    for i, sol in enumerate(solutions[:display_count]):
        row = [f"[yellow]{i+1}[/yellow]"]
        for var in var_symbols:
            value = sol.get(var, None)
            if value is None:
                row.append("[red]N/A[/red]")
            else:
                try:
                    from sympy import Rational, nsimplify
                    float_val = float(value)
                    
                    if float_val.is_integer():
                        row.append(f"[green]{int(float_val)}[/green]")
                    else:
                        rational_val = nsimplify(value)
                        if isinstance(rational_val, Rational):
                            numerator = rational_val.p
                            denominator = rational_val.q
                            row.append(f"[green]{float_val:.4f}[/green] [dim]({numerator}/{denominator})[/dim]")
                        else:
                            symbolic_str = str(rational_val)
                            if symbolic_str != str(float_val):
                                row.append(f"[green]{float_val:.4f}[/green] [dim]({symbolic_str})[/dim]")
                            else:
                                row.append(f"[green]{float_val:.4f}[/green]")
                except:
                    row.append(f"[green]{value}[/green]")
        table.add_row(*row)
    
    console.print("\n")
    console.print(table)
    
    if len(solutions) > 10:
        console.print(f"\n[yellow]⚠️  还有 {len(solutions) - 10} 组解未显示...[/yellow]")

def get_user_input(prompt_text, error_msg, validation_func=None):
    while True:
        try:
            user_input = Prompt.ask(prompt_text, console=console).strip()
            if user_input.lower() in ['quit', 'exit']:
                return None
            
            if validation_func and not validation_func(user_input):
                console.print(f"[red]❌ {error_msg}[/red]")
                continue
            
            return user_input
        except KeyboardInterrupt:
            console.print("\n\n[yellow]👋 感谢使用,再见![/yellow]")
            exit(0)

def validate_equation(equation_str):
    if not equation_str or equation_str.isspace():
        return False
    return True

def validate_variables(variables_str):
    if not variables_str or variables_str.isspace():
        return False
    return bool(re.match(r'^[a-zA-Z]+(?:,[a-zA-Z]+)*$', variables_str))

def main():
    display_welcome()
    
    while True:
        console.print("\n" + "="*50)
        
        equation_input = get_user_input(
            "📝 请输入方程(多个方程用分号分隔)",
            "方程不能为空!",
            validate_equation
        )
        
        if equation_input is None:
            console.print("\n[yellow]👋 感谢使用,再见![/yellow]")
            break
        
        if equation_input.lower() == 'example':
            display_examples()
            continue
        
        variables_input = get_user_input(
            "🎯 请输入变量(用逗号分隔)",
            "变量格式不正确! 请使用字母,如: x,y",
            validate_variables
        )
        
        if variables_input is None:
            console.print("\n[yellow]👋 感谢使用,再见![/yellow]")
            break
        
        console.print("\n[yellow]🔍 正在求解...[/yellow]")
        
        try:
            equations, var_symbols = parse_equation(equation_input, variables_input)
            solutions = solve_equation(equations, var_symbols)
            display_solutions(solutions, var_symbols)
        except Exception as e:
            console.print(f"\n[red]❌ 求解出错: {str(e)}[/red]")
        
        console.print("\n" + "-"*50)
        continue_input = Prompt.ask("🔄 继续计算?", console=console, choices=["y", "n"], default="y").strip().lower()
        
        if continue_input != 'y':
            console.print("\n[yellow]👋 感谢使用,再见![/yellow]")
            break

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        console.print("\n\n[yellow]👋 感谢使用,再见![/yellow]")

运行结果:

注意:

乘号使用'* ',除号或分数使用'/ ',幂运算用'**'

相关推荐
默_笙5 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003965 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫5 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技5 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时5 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
C语言小火车5 天前
C/C++ 为什么需要编译器?
开发语言·c++
奇思妙想聪明勤奋的小羊5 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1235 天前
2026年第38周GitHub趋势周报
python·科技·github