python解决一元一次方程组

运用第三方库

python 复制代码
# 有多个未知数,且包含
'4+3x=8''-5+12y=0''6a-5+1=2-2a''-4b=10'
test = '-4b=10'
# 解决首个为负号的问题
if test[0]=="-":
    last_fuhao = test[0]
    test = test[1:]
else:
    last_fuhao = "+" # 因为要把符号也搬进去,所以有个初始的符号位
# print("第一个符号",last_fuhao)
# print("处理后的方程",test)
# 第一步:将= 替换成减号,并添加()
left,right = test.split("=")
# 把右边的处理掉,因为移位必须换符号,因此把 + 换 -,- 换 +
true_right = ""
for r in range(len(right)):
    if  right[r] =="+":
        true_right+="-"
    elif right[r] =="-":
        true_right+="+"
    else:
        true_right +=right[r]
# print(true_right)
sum_result = left+"-"+f"{true_right}"+"+" # 在程序中是已符号结尾
print("处理后的方程:",sum_result[:-1])
# 遍历:找出含有未知数的东西,添加到列表中去
# 检测数字是否为阿拉伯数字,如果是,就往后找看看有没有包含字母,有则截取到列表中去,
# 从符号开始检索起
zimu_list = []
num_str = ""
# print(ord("+"))  # 97-122  # 48-57  # 42-47

temp_str = ""

for i in range(len(sum_result)):
    # print(sum_result[i])
    if 42 <= ord(sum_result[i]) <= 47:  # 符号前停止,并判断是否有未知数
        # 未知数都在最后一个,所以直接找最后一个是否是未知数
        # print(temp_str[-1])
        if 97<= ord(temp_str[-1]) <= 122:
            zimu_list.append(last_fuhao+temp_str)
        else:
            num_str+=(last_fuhao+temp_str)
        last_fuhao = sum_result[i]
        temp_str = ""
    # 将已知数 或者 未知数拼接起来
    elif sum_result[i] in "0123456789":
        temp_str+=sum_result[i]
    elif 97<= ord(sum_result[i]) <= 122:
        temp_str+=sum_result[i]
# print("纯数字",(num_str))
beizhuchu = eval(num_str) # 被除数
# print(zimu_list)
chushu = ""
for i in zimu_list:
    # print(i[0:-1])
    chushu+=i[0:-1]
chushu = eval(chushu)
print("未知数的结果:",(beizhuchu / chushu)*-1) # 因为未知数没有移位,所以最后要乘以 -1 ,取相反数
相关推荐
IVEN_11 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang12 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮12 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling12 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮15 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽16 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers