Parsing a chemical formula

前端 未结 5 819
离开以前
离开以前 2020-12-13 19:52

I\'m trying to write a method for an app that takes a chemical formula like \"CH3COOH\" and returns some sort of collection full of their symbols.

CH3COOH would retu

5条回答
  •  再見小時候
    2020-12-13 20:39

    The solution with regular expressions is the best approach if you need to handle only simple cases. Otherwise you need to build something like Abstract Syntax Tree and evaluate it or use Polish Notation.

    For example, TNT formula C6H2(NO2)3CH3 should be presented like:

    (+ (* C 6) (* H 2) (* (+ N (* O 2)) 3) C (+ H 3))
    

提交回复
热议问题