python解一元一次方程
- 将未知数看成是虚数
- 将常数看成是实数
- 最终求解。
import re class Item:
def __init__(self,imag=0,real=0):
self.imag = imag
self.real = real
def __str__(self):
return format("(%.6f : %.6fX)")%(self.real,self .imag)
def __repr__(self):
return self.__str__()
def _calc(a,b,op):
if op == '+':
a.imag += b.imag
a.real += b.real
return a
elif op == '-':
a.imag -= b.imag
a.real -= b.real
elif op == '*':
if b.imag == 0:
a.imag *= b.real
a.real *= b.real
else:
a.imag,a.real,b.imag,b.real= b.imag,b.real,a.imag,a.real
a.imag *= b.real
a.real *= b.real
elif op == '/':
if b.real== 0:
raise Exception
a.imag /= b.real
a.real /= b.real
return a
def calculate(list):
def _ca(oplist,nulist,i,stop,opers):
op = oplist[-1]
while op in opers:
first,second = nulist[-2:]
_calc(first,second,op)
del nulist[-1]
del oplist[-1]
if len(oplist):
op = oplist[-1]
else:
op = stop
else:
oplist.append(i) oplist = []
nulist = []
for i in list:
if isinstance(i,str):
if i == '(':
oplist.append(i)
elif i in '+-':
if len(oplist):
_ca(oplist,nulist,i,"(","+-*/")
else:
oplist.append(i)
elif i in "*/":
if len(oplist):
_ca(oplist,nulist,i,"(","*/")
else:
oplist.append(i)
else:
if len(oplist):
_ca(oplist,nulist,i,"stop","+-*/")
del oplist[-1]
del oplist[-1]
else:
nulist.append(i)
_ca(oplist,nulist,i,"stop","+-*/")
return nulist[0]
if __name__ == "__main__":
# data = "((-3x))=9-9+2*x"
# data = "((-1+2x)/3)-(7+(8-9))*(1/2) = 5x + (3x-2)"
# data = "2x=10"
data = "(((4x)))=5+1x"
data = " "+re.subn("\\s+|=(.*)",lambda obj:"-(%s)"%obj.groups(1) if '=' in obj.group() else "",data)[0] regex = re.compile("(?<=[-+*/( ])-?\\d+x|(?<=[-+*/( ])-?\\d+|[-+*/()x]")
data = re.findall(regex,data)
calclist = []
for i in data:
if re.fullmatch("-?\\d+",i):
calclist.append(Item(real=int(i)))
elif re.fullmatch("-?\\d+x",i):
calclist.append(Item(imag=int(i[:-1])))
elif i == "x":
calclist.append(Item(imag=1))
else:
calclist.append(i)
result = calculate(calclist)
print(-result.real/result.imag)
来源:http://www.1994july.club/seo/?p=1703
python解一元一次方程的更多相关文章
- 用Python解方程
一元一次方程 例题1: 这是北师大版小学六年级上册课本95页的一道解方程练习题: 大家可以先口算一下,这道题里面的x的值为200 接下来我们用python来实现,代码如下,每一句代码后面都写有解释语: ...
- C++第9周(春)项目5 - 一元一次方程类
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目5]设计一元一次方程类.求形如ax+b= ...
- python 解压 压缩包
转 http://m.blog.csdn.net/blog/wice110956/26597179# 这里讨论使用Python解压如下五种压缩文件: .gz .tar .tgz .zip .rar ...
- java练习题:解一元二次方程、判断闰年、判断标准身材、三个数取最大值
1.解一元二次方程 注:求根公式为(-b+根号德尔塔)/2a,(-b-根号德尔塔)/2a Scanner sc=new Scanner(System.in); System.out.println(& ...
- python解无忧公主的数学时间编程题001.py
python解无忧公主的数学时间编程题001.py """ python解无忧公主的数学时间编程题001.py http://mp.weixin.qq.com/s?__b ...
- python解无忧公主的数学时间097.py
python解无忧公主的数学时间097.py """ python解无忧公主的数学时间097.py codegay 2016年3月30日 00:17:26 http:// ...
- python解无忧公主数学题107.py
python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...
- python解无忧公主数学题108
""" python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...
- POJ 2891 Strange Way to Express Integers (解一元线性方程组)
求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1) 1x=r2(mod a2) ...
随机推荐
- cf 261B.Maxim and Restaurant
什么什么期望的,不会! (题解http://blog.sina.com.cn/s/blog_140e100580102wj4e.html(看不懂)) #include<bits/stdc++.h ...
- 【每日Scrum】第九天冲刺
一.计划会议内容 尝试数据库的连接与ui应用 二.任务看板 任务看板 已完成:登录与个人界面布局实现 进行中:连接数据库,地图主界面 待进行:功能整合 三.scrum讨论照片 四.产品的状态 无 五. ...
- 如何通过C语言获取主机WLAN下的IPv4地址,MAC地址
#include "stdio.h" #include "windows.h" void GetHostWLAN_IPv4_AND_MAC(char IPv4[ ...
- SQL审核平台-Yearning安装部署实践
相关文档: https://guide.yearning.io/ yearning简介 http://python.yearning.io/install/ yearning安装 Yearning ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 注释
程序的注释是解释性语句,可以在 C++ 代码中包含注释,这将提高源代码的可读性.所有的编程语言都允许某种形式的注释. C++ 支持单行注释和多行注释.注释中的所有字符会被 C++ 编译器忽略. C++ ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring AOP(面向切面编程)
面向切面编程(AOP)和面向对象编程(OOP)类似,也是一种编程模式.Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的. AOP ...
- 代码神器:拒绝重复编码,这款IDEA插件了解一下.....
作者:HeloWxl www.jianshu.com/p/e4192d7c6844 Easycode是idea的一个插件,可以直接对数据的表生成entity.controller.service.da ...
- [APIO2018]铁人两项(圆方树)
过了14个月再重新看这题,发现圆方树从来就没有写过.然后写了这题发现自己APIO2018打铁的原因竟然是没开long long,将树的部分的O(n)写挂了(爆int),毕竟去年APIO时我啥都不会,连 ...
- 微服务之docker(一)
一.docker介绍及使用 1.docker简介 (1)Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windo ...
- 寒假day13
今天看了计算机网络的相关面试题