Chemical calculator: Difference between revisions

Content added Content deleted
m (added whitespace, uncapitalized element names.)
m (→‎{{header|REXX}}: added the ability to have a common name for the chemical formula, presented the output better, reduced output clutter.)
Line 2,693: Line 2,693:
This REXX version has some basic error checking to catch malformed chemical formulas.
This REXX version has some basic error checking to catch malformed chemical formulas.


Some extra coding was added to format the output better and to also include a common name for the chemical formula.
Also a more precise atomic mass for the (all) known elements is used   (for instance, '''F''').


Also a more precise atomic mass for the (all) known elements is used;   for instance for   '''F'''   (fluorine).
Some of the elements added for the REXX example are:


Some of the (newer) elements added for the REXX example are:
mendelevium (Md), nobelium (No), lawrencium (Lr), rutherfordium (Rf), dubnium (Db),

seaborgium (Sg), bohrium (Bh), hassium (Hs), meitnerium (Mt), darmstadtium (Ds),
roentgenium (Rg), copernicium (Cn), nihoniym (Nh), flerovium (Fl), moscovium (Mc),
mendelevium (Md), nobelium (No), lawrencium (Lr), rutherfordium (Rf), dubnium (Db),
seaborgium (Sg), bohrium (Bh), hassium (Hs), meitnerium (Mt), darmstadtium (Ds),
livermorium (Lv), tennessine (Ts), and oganesson (Og)
roentgenium (Rg), copernicium (Cn), nihoniym (Nh), flerovium (Fl), moscovium (Mc),
livermorium (Lv), tennessine (Ts), oganesson (Og)
<lang rexx>/*REXX program calculates the molar mass from a specified chemical formula. */
<lang rexx>/*REXX program calculates the molar mass from a specified chemical formula. */
numeric digits 30 /*ensure enough decimal digits for mass*/
numeric digits 30 /*ensure enough decimal digits for mass*/
Line 2,729: Line 2,731:
@.Cn=285 ; @.Hs=277 ; @.No=259 ; @.Sc= 44.955912; @.Zr= 91.224
@.Cn=285 ; @.Hs=277 ; @.No=259 ; @.Sc= 44.955912; @.Zr= 91.224
@.Ubn=299 ; @.Uue=315
@.Ubn=299 ; @.Uue=315
parse arg $; _ = '─'
parse arg $
say center(' chemical formula {common name} ', 45) center("molar mass", 16)
if $='' | $="," then $= ' H H2 H2O H2O2 (HO)2 Na2SO4 C6H12 ' ,
" COOH(C(CH3)2)3CH3 C6H4O2(OH)4 C27H46O Uue"
say center('' , 45, _) center('' , 16, _)
if $='' | $="," then $= 'H{hydrogen} H2{molecular_hydrogen} H2O2{hydrogen_peroxide}',
'(HO)2{hydrogen_peroxide} H2O{water} Na2SO4{sodium_sulfate}',
'C6H12{cyclohexane} COOH(C(CH3)2)3CH3{butyric_acid}' ,
'C6H4O2(OH)4{vitamin_C} C27H46O{cholesterol} Uue{ununennium}',
'Mg3Si4O10(OH)2{talc}'
do j=1 for words($); x= word($, j) /*obtain the formula of the molecule. */
do j=1 for words($); x= word($, j) /*obtain the formula of the molecule. */
mm= chemCalc(x) /*get the molar mass " " " */
parse var x x '{' -0 name /* " " " and also a name. */
mm= chemCalc(x) /* " " molar mass. */
name= strip(x ' 'translate(name, 'ff'x,"_")) /* " " molar mass; fix─up name. */
if mm<0 then iterate /*if function had an error, skip output*/
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.*/
say ' 'justify(name, 45-2) " " mm /*show chemical name and its molar mass*/
end /*j*/
end /*j*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
Line 2,777: Line 2,786:
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
molar mass of H is 1.00794
chemical formula {common name} molar mass
───────────────────────────────────────────── ────────────────
molar mass of H2 is 2.01588
molar mass of H2O is 18.01528
H {hydrogen} 1.00794
molar mass of H2O2 is 34.01468
H2 {molecular hydrogen} 2.01588
molar mass of (HO)2 is 34.01468
H2O2 {hydrogen peroxide} 34.01468
molar mass of Na2SO4 is 142.04213856
(HO)2 {hydrogen peroxide} 34.01468
molar mass of C6H12 is 84.15948
H2O {water} 18.01528
molar mass of COOH(C(CH3)2)3CH3 is 186.29118
Na2SO4 {sodium sulfate} 142.04213856
molar mass of C6H4O2(OH)4 is 176.12412
C6H12 {cyclohexane} 84.15948
COOH(C(CH3)2)3CH3 {butyric acid} 186.29118
molar mass of C27H46O is 386.65354
molar mass of Uue is 315
C6H4O2(OH)4 {vitamin C} 176.12412
C27H46O {cholesterol} 386.65354
Uue {ununennium} 315
Mg3Si4O10(OH)2 {talc} 379.26568
</pre>
</pre>