Chemical calculator: Difference between revisions

→‎{{header|REXX}}: added the REXX computer programming language for this task.
(Added Fōrmulæ)
(→‎{{header|REXX}}: added the REXX computer programming language for this task.)
Line 1,045:
assert 386.664 == molar_mass('C27H46O') # Cholesterol
assert 315 == molar_mass('Uue')</lang>
 
=={{header|REXX}}==
This REXX version has some basic error checking to catch malformed chemical formulas.
<lang rexx>/*REXX program calculates the molar mass from a specified chemical formula. */
numeric digits 30 /*ensure enough decimal digits for mass*/
@.=.
/*───────────────── [↑] table of most elements with their atomic mass ─────────────────*/
@.Ac=227; @.Ag=107.8682; @.Al=26.9815385; @.Am=243; @.Ar=39.948; @.As=74.921595; @.At=210
@.Au=196.966569; @.B=10.81; @.Ba=137.327; @.Be=9.0121831; @.Bi=208.9804; @.Bk=247
@.Br=79.904; @.C=12.011; @.Ca=40.078; @.Cd=112.414; @.Ce=140.116; @.Cf=251; @.Cl=35.45
@.Cm=247; @.Co=58.933194; @.Cr=51.9961; @.Cs=132.90545196; @.Cu=63.546; @.Dy=162.5
@.Er=167.259; @.Es=252; @.Eu=151.964; @.F=18.998403163; @.Fe=55.845; @.Fm=257; @.Fr=223
@.Ga=69.723; @.Gd=157.25; @.Ge=72.63; @.H=1.008; @.He=4.002602; @.Hf=178.49; @.Hg=200.592
@.Ho=164.93033; @.I=126.90447; @.In=114.818; @.Ir=192.217; @.K=39.0983; @.Kr=83.798
@.La=138.90547; @.Li=6.94; @.Lu=174.9668; @.Mg=24.305; @.Mn=54.938044; @.Mo=95.95
@.N=14.007; @.Na=22.98976928; @.Nb=92.90637; @.Nd=144.242; @.Ne=20.1797; @.Ni=58.6934
@.Np=237; @.O=15.999; @.Os=190.23; @.P=30.973761998; @.Pa=231.03588; @.Pb=207.2
@.Pd=106.42; @.Pm=145; @.Po=209; @.Pr=140.90766; @.Pt=195.084; @.Pu=244; @.Ra=226
@.Rb=85.4678; @.Re=186.207; @.Rh=102.9055; @.Rn=222; @.Ru=101.07; @.S=32.06; @.Sb=121.76
@.Sc=44.955908; @.Se=78.971; @.Si=28.085; @.Sm=150.36; @.Sn=118.71; @.Sr=87.62
@.Ta=180.94788; @.Tb=158.92535; @.Te=127.6; @.Th=232.0377; @.Ti=47.867; @.Tl=204.38
@.Tm=168.93422; @.U=238.02891; @.Ubn=299; @.Uue=315; @.V=50.9415; @.W=183.84
@.Xe=131.293; @.Y=88.90584; @.Yb=173.054; @.Zn=65.38; @.Zr=91.224
 
parse arg $
if $='' | $="," then $= 'H H2 H2O H2O2 (HO)2 Na2SO4 C6H12 COOH(C(CH3)2)3CH3 C6H4O2(OH)4',
"C27H46O Uue"
do j=1 for words($); x= word($, j) /*obtain the formula of the molecule. */
mm= chemcalc(x) /*get the molar mass " " " */
if mm<0 then iterate /*if function had an error, skip output*/
say right('molar mass of ' x, 50) " is " mm /*display the molar mass of a chemical.*/
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
chemcalc: procedure expose @.; parse arg z /*obtain chemical formula of molecule. */
lev= 0 /*indicates level of parentheses depth.*/
$.= 0 /*the sum of the molar mass (so far). */
do k=1 to length(z); y= substr(z, k, 1) /*get a thing*/
if y=='(' then do; lev= lev + 1; iterate k; end
if y==')' then do; y= substr(z, k+1, 1)
if \datatype(y, 'W') then do; say "illegal number:" y
return -1
end
n= getNum() /*get )number*/
$.lev= $.lev * n; $$= $.lev; $.lev= 0 /*sum level. */
lev= lev - 1; $.lev= $.lev + $$ /*add to prev*/
k= k + length(n) /*adjust K. */
iterate /*k*/
end /*[↑] get ele*/
e=y; e= getEle(); upper e /* and upper.*/
if e==. then do; say 'missing element: ' e; return -2; end
if @.e==. then do; say 'invalid element: ' e; return -3; end
y= substr(z, k+length(e), 1)
k= k + length(e) - 1 /*adjust K. */
n= getNum() /*get number.*/
if n\==. then k= k + length(n) /*adjust K. */
else n= 1 /*no number. */
$.lev= $.lev + n * @.e /*add product*/
end /*k*/
return $.lev
/*──────────────────────────────────────────────────────────────────────────────────────*/
getEle: if \datatype(y, 'U') then do; say err "illegal element: " y; return .; end
do i=1 until \datatype(q, 'L'); q= substr(z, k+i, 1)
if datatype(q, 'L') then e= e || q /*lowercase? */
end /*i*/
return e
/*──────────────────────────────────────────────────────────────────────────────────────*/
getNum: if \datatype(y, 'W') then return .; n=
do i=1 until \datatype(q, 'W'); q= substr(z, k+i, 1)
if datatype(q, 'W') then n= n || q /*is a digit?*/
end /*i*/
return n</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
molar mass of H is 1.008
molar mass of H2 is 2.016
molar mass of H2O is 18.015
molar mass of H2O2 is 34.014
molar mass of (HO)2 is 34.014
molar mass of Na2SO4 is 142.03553856
molar mass of C6H12 is 84.162
molar mass of COOH(C(CH3)2)3CH3 is 186.295
molar mass of C6H4O2(OH)4 is 176.124
molar mass of C27H46O is 386.664
molar mass of Uue is 315
</pre>
 
=={{header|zkl}}==