Chemical Calculator
This example shows the implementation of a chemical calculator in the Julia language, which allows you to calculate the molar mass of chemical compounds using their formulas.
Introduction
A chemical calculator is a program that takes the chemical formula of a substance as input and returns its molar mass. This can be useful for chemists, students, and researchers who need to quickly calculate the mass of molecules for various chemical calculations. The program works with simple and complex chemical formulas, including brackets and indexes.
The main part
Here the molar masses of chemical elements are determined in the form of constants
const H = 1.008
const He = 4.002602
const Li = 6.94
const Be = 9.0121831
const B =  10.81
const C =  12.011
const N =  14.007
const O =  15.999
const F =  18.998403163
const Ne = 20.1797
const Na = 22.98976928
const Mg = 24.305
const Al = 26.9815385
const Si = 28.085
const P =  30.973761998
const S =  32.06
const Cl = 35.45
const Ar = 39.948
const K =  39.0983
const Ca = 40.078
const Sc = 44.955908
const Ti = 47.867
const V =  50.9415
const Cr = 51.9961
const Mn = 54.938044
const Fe = 55.845
const Co = 58.933194
const Ni = 58.6934
const Cu = 63.546
const Zn = 65.38
const Ga = 69.723
const Ge = 72.630
const As = 74.921595
const Se = 78.971
const Br = 79.904
const Kr = 83.798
const Rb = 85.4678
const Sr = 87.62
const Y =  88.90584
const Zr = 91.224
const Nb = 92.90637
const Mo = 95.95
const Ru = 101.07
const Rh = 102.90550
const Pd = 106.42
const Ag = 107.8682
const Cd = 112.414
const Ind = 114.818
const Sn = 118.710
const Sb = 121.760
const Te = 127.60
const I =  126.90447
const Xe = 131.293
const Cs = 132.90545196
const Ba = 137.327
const La = 138.90547
const Ce = 140.116
const Pr = 140.90766
const Nd = 144.242
const Pm = 145
const Sm = 150.36
const Eu = 151.964
const Gd = 157.25
const Tb = 158.92535
const Dy = 162.500
const Ho = 164.93033
const Er = 167.259
const Tm = 168.93422
const Yb = 173.054
const Lu = 174.9668
const Hf = 178.49
const Ta = 180.94788
const W =  183.84
const Re = 186.207
const Os = 190.23
const Ir = 192.217
const Pt = 195.084
const Au = 196.966569
const Hg = 200.592
const Tl = 204.38
const Pb = 207.2
const Bi = 208.98040
const Po = 209
const At = 210
const Rn = 222
const Fr = 223
const Ra = 226
const Ac = 227
const Th = 232.0377
const Pa = 231.03588
const U =  238.02891
const Np = 237
const Pu = 244
const Am = 243
const Cm = 247
const Bk = 247
const Cf = 251
const Es = 252
const Fm = 257
A function for calculating the molar mass
The main function that accepts a string with a chemical formula and returns the molar mass
function molar_mass(s)
    # Заменяем числа в строке на '*' и число (например, H2 -> H*2)
    s = replace(s, r"(\d+)" => (x) -> "*$x")
    
    # Заменяем элементы и открывающие скобки на '+' и элемент/скобку 
    # (например, H -> +H, Na -> +Na, ( -> +( )
    s = replace(s, r"[A-Z][a-z]{0,2}|\(" => (x) -> "+$x")
    
    # Преобразуем строку в выражение Julia и вычисляем его
    eval(Meta.parse(s))
end
An example of calculating the molar mass of an organic compound:
molar_mass("C2H5OH")
Tests to verify the correctness of the work
Tests to verify the function's operation
@assert 1.008 == molar_mass("H")                # Водород
@assert 2.016 == molar_mass("H2")               # Молекула водорода
@assert 18.015 == molar_mass("H2O")             # Вода
@assert 142.03553856000002 == molar_mass("Na2SO4")  # Сульфат натрия
@assert 84.162 == molar_mass("C6H12")           # Циклогексан
@assert 186.29500000000002 == molar_mass("COOH(C(CH3)2)3CH3")  # Сложное органическое соединение
Conclusion
In this example, we looked at the implementation of a chemical calculator in the Julia language. We have created a set of constants with molar masses of chemical elements and written a function that analyzes a chemical formula and calculates its molar mass. The program successfully passes tests for various types of chemical formulas, including simple molecules, ionic compounds, and complex organic molecules with brackets. It can be a useful tool for chemical calculations for scientific and educational purposes.
The example was developed using materials from Rosetta Code