• 将未知数看成是虚数
  • 将常数看成是实数
  • 最终求解。
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解一元一次方程的更多相关文章

  1. 用Python解方程

    一元一次方程 例题1: 这是北师大版小学六年级上册课本95页的一道解方程练习题: 大家可以先口算一下,这道题里面的x的值为200 接下来我们用python来实现,代码如下,每一句代码后面都写有解释语: ...

  2. C++第9周(春)项目5 - 一元一次方程类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目5]设计一元一次方程类.求形如ax+b= ...

  3. python 解压 压缩包

    转 http://m.blog.csdn.net/blog/wice110956/26597179# 这里讨论使用Python解压如下五种压缩文件: .gz .tar  .tgz .zip .rar ...

  4. java练习题:解一元二次方程、判断闰年、判断标准身材、三个数取最大值

    1.解一元二次方程 注:求根公式为(-b+根号德尔塔)/2a,(-b-根号德尔塔)/2a Scanner sc=new Scanner(System.in); System.out.println(& ...

  5. python解无忧公主的数学时间编程题001.py

    python解无忧公主的数学时间编程题001.py """ python解无忧公主的数学时间编程题001.py http://mp.weixin.qq.com/s?__b ...

  6. python解无忧公主的数学时间097.py

    python解无忧公主的数学时间097.py """ python解无忧公主的数学时间097.py codegay 2016年3月30日 00:17:26 http:// ...

  7. python解无忧公主数学题107.py

    python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...

  8. python解无忧公主数学题108

    """ python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...

  9. POJ 2891 Strange Way to Express Integers (解一元线性方程组)

    求解一元线性同余方程组: x=ri(mod ai) i=1,2,...,k 解一元线性同余方程组的一般步骤:先求出前两个的解,即:x=r1(mod a1)     1x=r2(mod a2)     ...

随机推荐

  1. oracle提交commit后回退恢复

    -------------------------------------------------------begin---------------------------------------- ...

  2. nested exception is java.lang.IllegalArgumentException: warning no match for this type name: res [Xlint:invalidAbsoluteTypeName]

    注:内有单词(sping)写错,请忽略,不影响程序运行 运行时报错: Exception in thread "main" org.springframework.beans.fa ...

  3. UVA - 10934 Dropping water balloons(装满水的气球)(dp)

    题意:有k个气球,n层楼,求出至少需要多少次实验能确定气球的硬度.气球不会被实验所“磨损”. 分析: 1.dp[i][j]表示第i个气球,测试j次所能确定的最高楼层. 2.假设第i-1个气球测试j-1 ...

  4. Go语言之冒泡排序

    package main //main函数 import "fmt" //相当于#include func main() { ar := [], , , , , , , , , } ...

  5. Dlib笔记二:matrix或array2d与cv::Mat的互转

    因为经常习惯的用OpenCV来做图像处理,所以难免希望将其他库的图像数据与OpenCV互转,所以今天就记录下这种互转的方法. 1.dlib::matrix/dlib::array2d转cv::Mat ...

  6. Python小数据池

    一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了,什么是内存地址呢? 你只要创建一个数据(对象)那么都 ...

  7. RK3399开发板Android镜像烧写之Windows系统映像烧写

    4.1.1 l RKTool  驱动安装(基于迅为iTOP-3399开发板)DriverAssitant_v4.5.zip 文件,打开 驱动安装成功,如下图: 注意事项:1.目前支持的操作系统包括:X ...

  8. js对象属性名和属性值生成新数组时都作为属性值

    const obj = { id:1, name:'zhangsan', age:18 } const arr = []; Object.getOwnPropertyNames(obj).forEac ...

  9. 吴裕雄--天生自然 PHP开发学习:超级全局变量

    <!DOCTYPE html> <html> <body> <?php $x = 75; $y = 25; function addition() { $GL ...

  10. try{}catch{}finally{}使用总结

    import java.util.Scanner; class MyException extends Exception { public MyException(String Message) { ...