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 ,取相反数
相关推荐
花酒锄作田14 小时前
Pydantic校验配置文件
python
hboot14 小时前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉
韩师傅2 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L2 天前
LangGraph的MessageState and HumanMessage
python