鱼C论坛

 找回密码
 立即注册
查看: 2487|回复: 3

[萌新报道] 新手写了个计算化学分子质量

[复制链接]
发表于 2020-12-26 15:24:50 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zzxzrm 于 2020-12-26 15:27 编辑
  1. def jisuan_init(che_list,string):
  2.           a=0
  3.           b=0
  4.           c=""
  5.           result=0
  6.          
  7.           result_s="Mr("+string+")="
  8.           result_s2=""
  9.           while True:
  10.                
  11.                if not(a+1>len(string)):
  12.                     pass
  13.                     
  14.                     if not(a+1>=len(string)):
  15.                          pass
  16.                         
  17.                         
  18.                     else:
  19.                          if a+1==len(string):
  20.                               index=che_list.index(string[a])
  21.                               result=result+int(che_list[index+1])*1
  22.                               result_s2+="+"+che_list[index+1]
  23.                               result_s+="+"+che_list[index+1]+"="+str(result)
  24.                          else:
  25.                               
  26.                               result_s2+="+"+che_list[index+1]
  27.                               result_s+="="+str(result)
  28.                          break
  29.                if string[a]  in che_list and (string[a+1] in che_list or string[a+1].isdecimal()):
  30.                     
  31.                     if not(a+1>=len(string)):#判断有没有到最后一个字符
  32.                         
  33.                          if string[a+1].isdecimal():
  34.                               
  35.                               index=che_list.index(string[a])
  36.                               result=result+int(che_list[index+1])*int(string[a+1])
  37.                               
  38.                               result_s=result_s+c+che_list[index+1]+"×"+string[a+1]
  39.                               result_s2=result_s2+c+che_list[index+1]+"×"+string[a+1]
  40.                               a=a+1
  41.                          else:
  42.                               
  43.                               index=che_list.index(string[a])
  44.                               
  45.                               result=result+int(che_list[index+1])
  46.                               
  47.                               result_s=result_s+c+che_list[index+1]
  48.                               result_s2+=c+che_list[index+1]
  49.                               print("over","a的值",str(a),string[a])
  50.                     
  51.                     else:
  52.                         
  53.                                    
  54.                          if string[a].isdecimal():
  55.                               
  56.                               index=che_list.index(string[a-1])
  57.                               result=result+int(che_list[index+1])*int(string[a])
  58.                               result_s=result_s+c+che_list[index+1]+"×"+string[a]+"="+str(result)
  59.                               result_s2=result_s2+c+che_list[index+1]+"×"+string[a]+"="+str(result)
  60.                               break
  61.                          else:
  62.                               index=che_list.index(string[a])#57X
  63.                               result=result+int(che_list[index+1])*1
  64.                               result_s=result_s+c+che_list[index+1]
  65.                               result_s2=result_s2+c+che_list[index+1]
  66.                elif string[a] in che_list and not(string[a+1] in che_list) and  not(string[a+1].isdecimal()):
  67.                     
  68.                     if string[a]+string[a+1] in che_list:
  69.                          index=che_list.index(string[a]+string[a+1])
  70.                         
  71.                          a+=1
  72.                          if not(a+1>=len(string)):
  73.                               if string[a+1].isdecimal():
  74.                                    
  75.                                    
  76.                                    result=result+(int(che_list[index+1])*int(string[a+1]))
  77.                                    result_s=result_s+c+che_list[index+1]+"×"+string[a+1]
  78.                                    result_s2=result_s2+c+che_list[index+1]+"×"+string[a+1]
  79.                                    a+=1
  80.                               else:
  81.                                    result=result+int(che_list[index+1])*1
  82.                                    result_s=result_s+c+che_list[index+1]
  83.                                    result_s2=result_s2+c+che_list[index+1]
  84.                          else:
  85.                               result=result+int(che_list[index+1])*1
  86.                               result_s=result_s+c+che_list[index+1]
  87.                               result_s2=result_s2+c+che_list[index+1]
  88.                if not(a+1>=len(string))  :   
  89.                     a+=1
  90.                     #print(a,"节点 下轮循环")
  91.                     c="+"
  92.                else:
  93.                     result_s+="="+str(result)
  94.                     
  95.                     break
  96.                         
  97.           return "\n\n"+result_s,result,result_s2
  98. class chem_count():
  99.      def __init__(self):
  100.           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;"
  101.           chemistry_list=chemistry.split(";")
  102.           chemistry_list=chemistry_list[0:-1]
  103.           self.che_list=[]
  104.           for i in chemistry_list:
  105.                self.che_list.append(i.split()[2])
  106.                self.che_list.append(i.split()[3])
  107.      
  108.          
  109.                
  110.      def jisuan(self,string):
  111.           f=string.find("(")
  112.           s=string.find(")")
  113.           t=s
  114.           result_s="Mr("+string+")="
  115.          
  116.          
  117.           if [f,s,t]!=[-1,-1,-1]:
  118.                print([f,s,t])
  119.                if t >len(string):
  120.                     t=1
  121.                else:
  122.                     print(t)
  123.                     if string[t+1].isdecimal():
  124.                          t=string[t+1]
  125.                          a=1
  126.                     else:
  127.                          t=1
  128.                          a=0
  129.                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)]
  130.                print(res,"res")
  131.                string=string[0:f]+string[s+1+a:]
  132.                print(string,"str")
  133.                r=jisuan_init(self.che_list,string)
  134.                print(r)
  135.                return ("\n\n"+result_s+str(r[2])+"+"+str(res[1])+"="+str(res[1]+r[1]))
  136.           else:
  137.                return jisuan_init(self.che_list,string)[0]

  138. if __name__=="__main__":
  139.      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;"
  140.      
  141.      try:   
  142.           c=chem_count()
  143.           print("友情提醒~~~请注意化学符号的大小写哦~~~")
  144.          
  145.           while True:
  146.                push=input()
  147.                string=str(push)
  148.                if push=="Quit":
  149.                     print("累死了~~~休息一下吧!!!")
  150.                     break
  151.                elif push!="":
  152.                         
  153.                     print(c.jisuan(push))
  154.                elif push=="":
  155.                         
  156.                     print("元素符号不会输吗")
  157.      except:
  158.           print("出错了!!!")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-26 15:33:15 | 显示全部楼层
可以
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-26 16:01:59 | 显示全部楼层
TQL
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-26 16:25:52 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 05:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表