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. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
mendelevium (Md), nobelium (No), lawrencium (Lr), rutherfordium (Rf), dubnium (Db), |
|||
⚫ | |||
mendelevium (Md), nobelium (No), lawrencium (Lr), rutherfordium (Rf), dubnium (Db), |
|||
⚫ | |||
⚫ | |||
roentgenium (Rg), copernicium (Cn), nihoniym (Nh), flerovium (Fl), moscovium (Mc), |
|||
⚫ | |||
<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 $ |
|||
say center(' chemical formula {common name} ', 45) center("molar mass", 16) |
|||
⚫ | |||
say center('' , 45, _) center('' , 16, _) |
|||
⚫ | |||
'(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. */ |
||
parse var x x '{' -0 name /* " " " and also a name. */ |
|||
⚫ | |||
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 |
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= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
chemical formula {common name} molar mass |
|||
───────────────────────────────────────────── ──────────────── |
|||
⚫ | |||
H {hydrogen} 1.00794 |
|||
H2 {molecular hydrogen} 2.01588 |
|||
H2O2 {hydrogen peroxide} 34.01468 |
|||
(HO)2 {hydrogen peroxide} 34.01468 |
|||
H2O {water} 18.01528 |
|||
Na2SO4 {sodium sulfate} 142.04213856 |
|||
C6H12 {cyclohexane} 84.15948 |
|||
COOH(C(CH3)2)3CH3 {butyric acid} 186.29118 |
|||
⚫ | |||
C6H4O2(OH)4 {vitamin C} 176.12412 |
|||
C27H46O {cholesterol} 386.65354 |
|||
Uue {ununennium} 315 |
|||
Mg3Si4O10(OH)2 {talc} 379.26568 |
|||
</pre> |
</pre> |
||