Jump to content

Chemical calculator: Difference between revisions

m
→‎{{header|REXX}}: added the ability to have a common name for the chemical formula, presented the output better, reduced output clutter.
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:
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),
roentgeniummendelevium (RgMd), copernicium nobelium (CnNo), nihoniym lawrencium (NhLr), flerovium rutherfordium (FlRf), moscovium dubnium (McDb),
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), and oganesson (Og)
<lang rexx>/*REXX program calculates the molar mass from a specified chemical formula. */
numeric digits 30 /*ensure enough decimal digits for mass*/
Line 2,729 ⟶ 2,731:
@.Cn=285 ; @.Hs=277 ; @.No=259 ; @.Sc= 44.955912; @.Zr= 91.224
@.Ubn=299 ; @.Uue=315
parse arg $; molar mass of H2 is 2.01588 _ = '─'
parse arg $
say center(' chemical formula {common name} ', 45) center("molar mass", 16)
if $='' | $="," then $= ' H H2 H2O H2O2 (HO)2 Na2SO4 C6H12 ' ,
say center('' " COOH(C(CH3)2)3CH3 C6H4O2(OH , 45, _)4 center('' C27H46O Uue" , 16, _)
if $='' | $="," then $= ' H {hydrogen} H2 H2O {molecular_hydrogen} H2O2 (HO)2 Na2SO4 C6H12 {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. */
mm=parse chemCalc(x)var x x '{' -0 name /* " /*get the molar" mass " " and also " a name. */
mm= chemCalc(x) /* " " molar mass. of C27H46O is 386.65354 */
name= strip(x ' 'translate(name, 'ff'x,"_")) /* " " molar mass; fix─up name. */
if mm<0 then iterate /*if function had an error, skip output*/
say right('molar mass of ' xjustify(name, 5045-2) " is " mm /*displayshow thechemical molarname massand ofits amolar chemical.mass*/
end /*j*/
exit /*stick a fork in it, we're all done. */
Line 2,777 ⟶ 2,786:
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
chemical formula {common name} molar mass of H is 1.00794
───────────────────────────────────────────── ────────────────
molar mass of H2 is 2.01588
H molar{hydrogen} mass of H2O is 181.0152800794
H2 {molecular hydrogen} molar mass of H2O2 is 342.0146801588
H2O2 {hydrogen peroxide} molar mass of (HO)2 is 34.01468
(HO)2 {hydrogen peroxide} molar mass of Na2SO4 is 14234.0421385601468
H2O molar mass of {water} C6H12 is 8418.1594801528
Na2SO4 molar mass of {sodium sulfate} COOH(C(CH3)2)3CH3 is 186142.2911804213856
C6H12 molar mass{cyclohexane} of C6H4O2(OH)4 is 17684.1241215948
COOH(C(CH3)2)3CH3 {butyric acid} 186.29118
molar mass of C27H46O is 386.65354
C6H4O2(OH)4 {vitamin C} molar mass of Uue is 315176.12412
C27H46O {cholesterol} 386.65354
Uue {ununennium} 315
Mg3Si4O10(OH)2 {talc} 379.26568
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.