|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 zzxzrm 于 2020-12-26 15:27 编辑
- def jisuan_init(che_list,string):
- a=0
- b=0
- c=""
- result=0
-
- result_s="Mr("+string+")="
- result_s2=""
- while True:
-
- if not(a+1>len(string)):
- pass
-
- if not(a+1>=len(string)):
- pass
-
-
- else:
- if a+1==len(string):
- index=che_list.index(string[a])
- result=result+int(che_list[index+1])*1
- result_s2+="+"+che_list[index+1]
- result_s+="+"+che_list[index+1]+"="+str(result)
- else:
-
- result_s2+="+"+che_list[index+1]
- result_s+="="+str(result)
- break
- if string[a] in che_list and (string[a+1] in che_list or string[a+1].isdecimal()):
-
- if not(a+1>=len(string)):#判断有没有到最后一个字符
-
- if string[a+1].isdecimal():
-
- index=che_list.index(string[a])
- result=result+int(che_list[index+1])*int(string[a+1])
-
- result_s=result_s+c+che_list[index+1]+"×"+string[a+1]
- result_s2=result_s2+c+che_list[index+1]+"×"+string[a+1]
- a=a+1
- else:
-
- index=che_list.index(string[a])
-
- result=result+int(che_list[index+1])
-
- result_s=result_s+c+che_list[index+1]
- result_s2+=c+che_list[index+1]
- print("over","a的值",str(a),string[a])
-
- else:
-
-
- if string[a].isdecimal():
-
- index=che_list.index(string[a-1])
- result=result+int(che_list[index+1])*int(string[a])
- result_s=result_s+c+che_list[index+1]+"×"+string[a]+"="+str(result)
- result_s2=result_s2+c+che_list[index+1]+"×"+string[a]+"="+str(result)
- break
- else:
- index=che_list.index(string[a])#57X
- result=result+int(che_list[index+1])*1
- result_s=result_s+c+che_list[index+1]
- result_s2=result_s2+c+che_list[index+1]
- elif string[a] in che_list and not(string[a+1] in che_list) and not(string[a+1].isdecimal()):
-
- if string[a]+string[a+1] in che_list:
- index=che_list.index(string[a]+string[a+1])
-
- a+=1
- if not(a+1>=len(string)):
- if string[a+1].isdecimal():
-
-
- result=result+(int(che_list[index+1])*int(string[a+1]))
- result_s=result_s+c+che_list[index+1]+"×"+string[a+1]
- result_s2=result_s2+c+che_list[index+1]+"×"+string[a+1]
- a+=1
- else:
- result=result+int(che_list[index+1])*1
- result_s=result_s+c+che_list[index+1]
- result_s2=result_s2+c+che_list[index+1]
- else:
- result=result+int(che_list[index+1])*1
- result_s=result_s+c+che_list[index+1]
- result_s2=result_s2+c+che_list[index+1]
- if not(a+1>=len(string)) :
- a+=1
- #print(a,"节点 下轮循环")
- c="+"
- else:
- result_s+="="+str(result)
-
- break
-
- return "\n\n"+result_s,result,result_s2
- class chem_count():
- def __init__(self):
- chemistry="1 氢 H 1 ;2 氦 He 4 ;3 锂 Li 7 ;4 铍 Be 9 ;5 硼 B 10.8 ;6 碳 C 12 ;7 氮 N 14 ;8 氧 O 16 ;9 氟 F 19 ;10 氖 Ne 20 ;11 钠 Na 23 ;12 镁 Mg 24 ;13 铝 Al 27 ;14 硅 Si 28 ;15 磷 P 31 ;16 硫 S 32 ;17 氯 Cl 35.5 ;18 氩 Ar 40 ;19 钾 K 39 ;20 钙 Ca 40 ;26 铁 Fe 56 ;29 铜 Cu 64 ;30 锌 Zn 65 ;35 溴 Br 80 ;36 氪 Kr 84 ;47 银 Ag 108 ;56 钡 Ba 137 ;78 铂 Pt 195 ;79 金 Au 197 ;80 汞 Hg 200;82 铅 Pb 207;"
- chemistry_list=chemistry.split(";")
- chemistry_list=chemistry_list[0:-1]
- self.che_list=[]
- for i in chemistry_list:
- self.che_list.append(i.split()[2])
- self.che_list.append(i.split()[3])
-
-
-
- def jisuan(self,string):
- f=string.find("(")
- s=string.find(")")
- t=s
- result_s="Mr("+string+")="
-
-
- if [f,s,t]!=[-1,-1,-1]:
- print([f,s,t])
- if t >len(string):
- t=1
- else:
- print(t)
- if string[t+1].isdecimal():
- t=string[t+1]
- a=1
- else:
- t=1
- a=0
- res=[str(jisuan_init(self.che_list,string[f+1:s])[2])+"×"+str(t),int(jisuan_init(self.che_list,string[f+1:s])[1]*2)]
- print(res,"res")
- string=string[0:f]+string[s+1+a:]
- print(string,"str")
- r=jisuan_init(self.che_list,string)
- print(r)
- return ("\n\n"+result_s+str(r[2])+"+"+str(res[1])+"="+str(res[1]+r[1]))
- else:
- return jisuan_init(self.che_list,string)[0]
- if __name__=="__main__":
- chemistry="1 氢 H 1 ;2 氦 He 4 ;3 锂 Li 7 ;4 铍 Be 9 ;5 硼 B 10.8 ;6 碳 C 12 ;7 氮 N 14 ;8 氧 O 16 ;9 氟 F 19 ;10 氖 Ne 20 ;11 钠 Na 23 ;12 镁 Mg 24 ;13 铝 Al 27 ;14 硅 Si 28 ;15 磷 P 31 ;16 硫 S 32 ;17 氯 Cl 35.5 ;18 氩 Ar 40 ;19 钾 K 39 ;20 钙 Ca 40 ;26 铁 Fe 56 ;29 铜 Cu 64 ;30 锌 Zn 65 ;35 溴 Br 80 ;36 氪 Kr 84 ;47 银 Ag 108 ;56 钡 Ba 137 ;78 铂 Pt 195 ;79 金 Au 197 ;80 汞 Hg 200;82 铅 Pb 207;"
-
- try:
- c=chem_count()
- print("友情提醒~~~请注意化学符号的大小写哦~~~")
-
- while True:
- push=input()
- string=str(push)
- if push=="Quit":
- print("累死了~~~休息一下吧!!!")
- break
- elif push!="":
-
- print(c.jisuan(push))
- elif push=="":
-
- print("元素符号不会输吗")
- except:
- print("出错了!!!")
复制代码 |
|