# coding: utf-8
def is_number(s): #判断字符是不是数字,比如字符串"-23"判断为Tue(表示是数字);'-a'判断为False(表示不是数字)
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return true
except (TypeError,ValueError):
pass
return False
def split1(a): #将组合形式转化为单个列表元素,比如'12*a*b+3*a^2*b-3*c'转化为[12*a*b,3*a^2*b,-3*c],并返回这个列表。
i=0
temp=[]
for j in range(len(a)):
if a[j]=='+':
temp.append(a[i:j])
i =j+1
elif a[j]=='-':
if j==0:
continue
temp.append(a[i:j])
i =j
elif j==len(a)-1:
temp.append(a[i:j+1])
return temp
def list_simplify(temp): #将上一步得到的列表每一个元素化简,比如[12*a*b*a,2*a^2*b*2*a]化简为[12*a^2*b,4*a^3*b]。
temp =split1(temp)
for j in range(len(temp)):
i =1
temp_x = temp[j].split('*')
if temp_x[0][0]=='-' and not is_number(temp_x[0][1]): #判断列表元素首元素是不是-1,比如列表元素为-c*3
i=-1 #这时负号其实表示-1*c,首先先提取出-1
temp_x[0]=temp_x[0][1:] #如果是形如-2*c,即不是-1的话,就不用单独提取出来
b =[1 for yy in range(len(temp_x))]
for x in range(len(temp_x)):
if is_number(temp_x[x]):
i *=(int)(temp_x[x])
b[x]=0
for x in range(len(temp_x)):
if b[x]==0:
continue
if len(temp_x[x])==1:
continue
b[x] =(int)(temp_x[x][2:])
temp_x[x]=temp_x[x][0:1]
for x in range(len(temp_x)):
if b[x]==0:
continue
else:
for y in range(x+1,len(temp_x)):
if temp_x[y]==temp_x[x]:
b[x] +=b[y]
b[y]=0
str1=''
str1 +=(str)(i)
# str1 +='*'
for x in range(len(temp_x)):
if b[x]==0:
continue
else:
if b[x]==1:
str1 +='*'
str1 += temp_x[x]
# str1 +='*'
else:
str1 +='*'
str1 += temp_x[x]
str1 +='^'
str1 +=(str)(b[x])
# str1 += '*'
temp[j]=str1
return temp
def get_number(a):
for x in range(len(a)):
if a[x]=='*':
return x
return len(a)
def multiply(a,b): #将两个字符串形式的表达式相乘,本质是先各自转化为列表,再相乘,并化简列表的每一个元素
a=list_simplify(a) #比如x="a+b",y="a-b",那么该函数multiply(x,y)最后得到的是["1*a^2","-1*a*b","1*b*a","-1*b^2"]
b=list_simplify(b)
temp=[]
for x in range(len(a)):
x1 =get_number(a[x])
temp_a =a[x][x1+1:]
for y in range(len(b)):
y1 =get_number(b[y])
flag=1
flag *=(int)(a[x][0:x1])
flag *=(int)(b[y][0:y1])
#temp_a=a[x1+1:]
temp_b=b[y][y1+1:]
str1=''
str1 +=(str)(flag)
if temp_a=='':
if temp_b!='':
str1 +='*'
str1 += temp_b
temp.append(str1)
elif temp_b=='':
temp.append(str1)
else:
if temp_b !='':
str1 +='*'
str1 +=temp_a
str1 +='*'
str1 += temp_b
temp.append(str1)
else:
str1 +='*'
str1 += temp_a
temp.append(str1)
for x in range(len(temp)):
temp[x]=''.join(list_simplify(temp[x]))
tempyy=simplify(temp)
# print(temp)
return tempyy
def get1(a): #该函数主要是将连乘形式的每一个元素都转化为列表,比如 "(1+2*c)*(2+a*b)*(a-2*c)"转化为列表[1+2*c,2+a*b,a-2*c]
temp=[] #该函数只能转化为如上面连乘形式的运算,其他还包含加减则转化不了。比如"(1+2*c)*(2+a*b)*(a-2*c)+3*c-4"转化不了。
flag=0
for i in range(len(a)):
if i==0:
if a[i]=='-' and (not a[i+1].isdigit()): #判断形如“-(a+2*b)*(1+c)”等形式,即首位是-1,其他情况不用管。
temp.append('-1')
flag +=1
if a[i]==')':
if i+1<=len(a)-1:
if a[i+1]=='^': #判断括号后面有没有次方,比如计算"(1+a-b)^2*3*(a-b)"等包含次方的形式。注意数字含有次方计算不了
j =i+2
while a[j]!='*':
j +=1
if j>len(a)-1:
break
count1=int(a[i+2:j])
temp.append(pow1(a[flag+1:i],count1))
flag =j+1
if j>len(a)-1:
break
else:
temp.append(a[flag+1:i])
flag=i+2
else:
temp.append(a[flag+1:i])
flag =i+2
elif (a[i]=='(' and i!=0)and a[i-2]!=')':
x =a[flag:i-1]
if x=="":
continue
else:
temp.append(a[flag:i-1])
flag =i
elif i==len(a)-1 and a[i]!=')':
temp.append(a[flag:i+1])
# elif i==len(a)-1 and a[i]!=')':
# temp.append(a[flag:i+1])
return temp
def get2(a): #该函数是将复合形式的运算转化为列表,比如 "(1+2*c)*(2+a*b)*(a-2*c)+3*c-4"转化为列表[(1+2*c)*(2+a*b)*(a-2*c),3*c,-4]
temp=[] #该函数可以视作是前面get1函数的加强版,即可以转换加减和乘法。
flag=0
count=0
for i in range(len(a)):
if a[i]=='+':
temp_flag=0
temp_flagx=0
for j in range(count,i):
if a[j]=='(':
temp_flag +=1
#break
for k in range(count,i):
if a[k]==')':
temp_flagx +=1
#break
if temp_flagx==temp_flag:
temp.append(a[flag:i])
cout=i
flag=i+1
elif a[i]=='-':
if i !=0:
temp_flag=0
temp_flagx=0
for j in range(count,i):
if a[j]=='(':
temp_flag +=1
#break
for k in range(count,i):
if a[k]==')':
temp_flagx +=1
#break
if temp_flagx==temp_flag:
temp.append(a[flag:i])
cout=i
flag=i
if i==len(a)-1:
temp.append(a[flag:i+1])
# print(temp)
return temp
def Calc_multiply(a): #这个函数主要是计算多项式混合运算,比如 "(1+2*c)*(3-4*a)*(a+b-2*c)+3*d-4"
temp=get2(a) #先由前面的get2函数转化为列表,然后化简输出
for i in range(len(temp)):
temp[i]=Calc(temp[i])
for i in range(len(temp)):
temp[i]=split1(temp[i])
x=[]
for i in range(len(temp)):
python实现代数多项式运算
需积分: 5 173 浏览量
2023-07-26
13:53:39
上传
评论 1
收藏 6KB ZIP 举报
童小纯
- 粉丝: 3w+
- 资源: 289
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈