Chemical calculator: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 315:
UueCl : 350.450
</pre>
 
=={{header|C sharp|C#}}==
{{trans|D}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ChemicalCalculator {
class Program {
static Dictionary<string, double> atomicMass = new Dictionary<string, double>() {
{"H", 1.008 },
{"He", 4.002602},
{"Li", 6.94},
{"Be", 9.0121831},
{"B", 10.81},
{"C", 12.011},
{"N", 14.007},
{"O", 15.999},
{"F", 18.998403163},
{"Ne", 20.1797},
{"Na", 22.98976928},
{"Mg", 24.305},
{"Al", 26.9815385},
{"Si", 28.085},
{"P", 30.973761998},
{"S", 32.06},
{"Cl", 35.45},
{"Ar", 39.948},
{"K", 39.0983},
{"Ca", 40.078},
{"Sc", 44.955908},
{"Ti", 47.867},
{"V", 50.9415},
{"Cr", 51.9961},
{"Mn", 54.938044},
{"Fe", 55.845},
{"Co", 58.933194},
{"Ni", 58.6934},
{"Cu", 63.546},
{"Zn", 65.38},
{"Ga", 69.723},
{"Ge", 72.630},
{"As", 74.921595},
{"Se", 78.971},
{"Br", 79.904},
{"Kr", 83.798},
{"Rb", 85.4678},
{"Sr", 87.62},
{"Y", 88.90584},
{"Zr", 91.224},
{"Nb", 92.90637},
{"Mo", 95.95},
{"Ru", 101.07},
{"Rh", 102.90550},
{"Pd", 106.42},
{"Ag", 107.8682},
{"Cd", 112.414},
{"In", 114.818},
{"Sn", 118.710},
{"Sb", 121.760},
{"Te", 127.60},
{"I", 126.90447},
{"Xe", 131.293},
{"Cs", 132.90545196},
{"Ba", 137.327},
{"La", 138.90547},
{"Ce", 140.116},
{"Pr", 140.90766},
{"Nd", 144.242},
{"Pm", 145},
{"Sm", 150.36},
{"Eu", 151.964},
{"Gd", 157.25},
{"Tb", 158.92535},
{"Dy", 162.500},
{"Ho", 164.93033},
{"Er", 167.259},
{"Tm", 168.93422},
{"Yb", 173.054},
{"Lu", 174.9668},
{"Hf", 178.49},
{"Ta", 180.94788},
{"W", 183.84},
{"Re", 186.207},
{"Os", 190.23},
{"Ir", 192.217},
{"Pt", 195.084},
{"Au", 196.966569},
{"Hg", 200.592},
{"Tl", 204.38},
{"Pb", 207.2},
{"Bi", 208.98040},
{"Po", 209},
{"At", 210},
{"Rn", 222},
{"Fr", 223},
{"Ra", 226},
{"Ac", 227},
{"Th", 232.0377},
{"Pa", 231.03588},
{"U", 238.02891},
{"Np", 237},
{"Pu", 244},
{"Am", 243},
{"Cm", 247},
{"Bk", 247},
{"Cf", 251},
{"Es", 252},
{"Fm", 257},
{"Uue", 315},
{"Ubn", 299},
};
 
static double Evaluate(string s) {
s += "[";
double sum = 0.0;
string symbol = "";
string number = "";
for (int i = 0; i < s.Length; ++i) {
var c = s[i];
if ('@' <= c && c <= '[') {
// @, A-Z
int n = 1;
if (number != "") {
n = int.Parse(number);
}
if (symbol != "") {
sum += atomicMass[symbol] * n;
}
if (c == '[') {
break;
}
symbol = c.ToString();
number = "";
} else if ('a' <= c && c <= 'z') {
symbol += c;
} else if ('0' <= c && c <= '9') {
number += c;
} else {
throw new Exception(string.Format("Unexpected symbol {0} in molecule", c));
}
}
return sum;
}
 
// Taken from return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
static string ReplaceFirst(string text, string search, string replace) {
int pos = text.IndexOf(search);
if (pos < 0) {
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
 
static string ReplaceParens(string s) {
char letter = 's';
while (true) {
var start = s.IndexOf('(');
if (start == -1) {
break;
}
 
for (int i = start + 1; i < s.Length; ++i) {
if (s[i] == ')') {
var expr = s.Substring(start + 1, i - start - 1);
var symbol = string.Format("@{0}", letter);
s = ReplaceFirst(s, s.Substring(start, i + 1 - start), symbol);
atomicMass[symbol] = Evaluate(expr);
letter++;
break;
}
if (s[i] == '(') {
start = i;
continue;
}
}
}
return s;
}
 
static void Main() {
var molecules = new string[]{
"H", "H2", "H2O", "H2O2", "(HO)2", "Na2SO4", "C6H12",
"COOH(C(CH3)2)3CH3", "C6H4O2(OH)4", "C27H46O", "Uue"
};
foreach (var molecule in molecules) {
var mass = Evaluate(ReplaceParens(molecule));
Console.WriteLine("{0,17} -> {1,7:0.000}", molecule, mass);
}
}
}
}</lang>
{{out}}
<pre> H -> 1.008
H2 -> 2.016
H2O -> 18.015
H2O2 -> 34.014
(HO)2 -> 34.014
Na2SO4 -> 142.036
C6H12 -> 84.162
COOH(C(CH3)2)3CH3 -> 186.295
C6H4O2(OH)4 -> 176.124
C27H46O -> 386.664
Uue -> 315.000</pre>
 
=={{header|C++}}==
Line 515 ⟶ 721:
 
return 0;
}</lang>
{{out}}
<pre> H -> 1.008
H2 -> 2.016
H2O -> 18.015
H2O2 -> 34.014
(HO)2 -> 34.014
Na2SO4 -> 142.036
C6H12 -> 84.162
COOH(C(CH3)2)3CH3 -> 186.295
C6H4O2(OH)4 -> 176.124
C27H46O -> 386.664
Uue -> 315.000</pre>
 
=={{header|C#}}==
{{trans|D}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ChemicalCalculator {
class Program {
static Dictionary<string, double> atomicMass = new Dictionary<string, double>() {
{"H", 1.008 },
{"He", 4.002602},
{"Li", 6.94},
{"Be", 9.0121831},
{"B", 10.81},
{"C", 12.011},
{"N", 14.007},
{"O", 15.999},
{"F", 18.998403163},
{"Ne", 20.1797},
{"Na", 22.98976928},
{"Mg", 24.305},
{"Al", 26.9815385},
{"Si", 28.085},
{"P", 30.973761998},
{"S", 32.06},
{"Cl", 35.45},
{"Ar", 39.948},
{"K", 39.0983},
{"Ca", 40.078},
{"Sc", 44.955908},
{"Ti", 47.867},
{"V", 50.9415},
{"Cr", 51.9961},
{"Mn", 54.938044},
{"Fe", 55.845},
{"Co", 58.933194},
{"Ni", 58.6934},
{"Cu", 63.546},
{"Zn", 65.38},
{"Ga", 69.723},
{"Ge", 72.630},
{"As", 74.921595},
{"Se", 78.971},
{"Br", 79.904},
{"Kr", 83.798},
{"Rb", 85.4678},
{"Sr", 87.62},
{"Y", 88.90584},
{"Zr", 91.224},
{"Nb", 92.90637},
{"Mo", 95.95},
{"Ru", 101.07},
{"Rh", 102.90550},
{"Pd", 106.42},
{"Ag", 107.8682},
{"Cd", 112.414},
{"In", 114.818},
{"Sn", 118.710},
{"Sb", 121.760},
{"Te", 127.60},
{"I", 126.90447},
{"Xe", 131.293},
{"Cs", 132.90545196},
{"Ba", 137.327},
{"La", 138.90547},
{"Ce", 140.116},
{"Pr", 140.90766},
{"Nd", 144.242},
{"Pm", 145},
{"Sm", 150.36},
{"Eu", 151.964},
{"Gd", 157.25},
{"Tb", 158.92535},
{"Dy", 162.500},
{"Ho", 164.93033},
{"Er", 167.259},
{"Tm", 168.93422},
{"Yb", 173.054},
{"Lu", 174.9668},
{"Hf", 178.49},
{"Ta", 180.94788},
{"W", 183.84},
{"Re", 186.207},
{"Os", 190.23},
{"Ir", 192.217},
{"Pt", 195.084},
{"Au", 196.966569},
{"Hg", 200.592},
{"Tl", 204.38},
{"Pb", 207.2},
{"Bi", 208.98040},
{"Po", 209},
{"At", 210},
{"Rn", 222},
{"Fr", 223},
{"Ra", 226},
{"Ac", 227},
{"Th", 232.0377},
{"Pa", 231.03588},
{"U", 238.02891},
{"Np", 237},
{"Pu", 244},
{"Am", 243},
{"Cm", 247},
{"Bk", 247},
{"Cf", 251},
{"Es", 252},
{"Fm", 257},
{"Uue", 315},
{"Ubn", 299},
};
 
static double Evaluate(string s) {
s += "[";
double sum = 0.0;
string symbol = "";
string number = "";
for (int i = 0; i < s.Length; ++i) {
var c = s[i];
if ('@' <= c && c <= '[') {
// @, A-Z
int n = 1;
if (number != "") {
n = int.Parse(number);
}
if (symbol != "") {
sum += atomicMass[symbol] * n;
}
if (c == '[') {
break;
}
symbol = c.ToString();
number = "";
} else if ('a' <= c && c <= 'z') {
symbol += c;
} else if ('0' <= c && c <= '9') {
number += c;
} else {
throw new Exception(string.Format("Unexpected symbol {0} in molecule", c));
}
}
return sum;
}
 
// Taken from return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
static string ReplaceFirst(string text, string search, string replace) {
int pos = text.IndexOf(search);
if (pos < 0) {
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
 
static string ReplaceParens(string s) {
char letter = 's';
while (true) {
var start = s.IndexOf('(');
if (start == -1) {
break;
}
 
for (int i = start + 1; i < s.Length; ++i) {
if (s[i] == ')') {
var expr = s.Substring(start + 1, i - start - 1);
var symbol = string.Format("@{0}", letter);
s = ReplaceFirst(s, s.Substring(start, i + 1 - start), symbol);
atomicMass[symbol] = Evaluate(expr);
letter++;
break;
}
if (s[i] == '(') {
start = i;
continue;
}
}
}
return s;
}
 
static void Main() {
var molecules = new string[]{
"H", "H2", "H2O", "H2O2", "(HO)2", "Na2SO4", "C6H12",
"COOH(C(CH3)2)3CH3", "C6H4O2(OH)4", "C27H46O", "Uue"
};
foreach (var molecule in molecules) {
var mass = Evaluate(ReplaceParens(molecule));
Console.WriteLine("{0,17} -> {1,7:0.000}", molecule, mass);
}
}
}
}</lang>
{{out}}
Line 1,968:
84.162 C6H12 C6H12
186.295 C11H22O2 COOH(C(CH3)2)3CH3</pre>
 
=={{header|Perl 6}}==
<lang perl6>my %ATOMIC_MASS =
H => 1.008 , Fe => 55.845 , Te => 127.60 , Ir => 192.217 ,
He => 4.002602 , Co => 58.933194 , I => 126.90447 , Pt => 195.084 ,
Li => 6.94 , Ni => 58.6934 , Xe => 131.293 , Au => 196.966569 ,
Be => 9.0121831 , Cu => 63.546 , Cs => 132.90545196 , Hg => 200.592 ,
B => 10.81 , Zn => 65.38 , Ba => 137.327 , Tl => 204.38 ,
C => 12.011 , Ga => 69.723 , La => 138.90547 , Pb => 207.2 ,
N => 14.007 , Ge => 72.630 , Ce => 140.116 , Bi => 208.98040 ,
O => 15.999 , As => 74.921595 , Pr => 140.90766 , Po => 209 ,
F => 18.998403163 , Se => 78.971 , Nd => 144.242 , At => 210 ,
Ne => 20.1797 , Br => 79.904 , Pm => 145 , Rn => 222 ,
Na => 22.98976928 , Kr => 83.798 , Sm => 150.36 , Fr => 223 ,
Mg => 24.305 , Rb => 85.4678 , Eu => 151.964 , Ra => 226 ,
Al => 26.9815385 , Sr => 87.62 , Gd => 157.25 , Ac => 227 ,
Si => 28.085 , Y => 88.90584 , Tb => 158.92535 , Th => 232.0377 ,
P => 30.973761998 , Zr => 91.224 , Dy => 162.500 , Pa => 231.03588 ,
S => 32.06 , Nb => 92.90637 , Ho => 164.93033 , U => 238.02891 ,
Cl => 35.45 , Mo => 95.95 , Er => 167.259 , Np => 237 ,
Ar => 39.948 , Ru => 101.07 , Tm => 168.93422 , Pu => 244 ,
K => 39.0983 , Rh => 102.90550 , Yb => 173.054 , Am => 243 ,
Ca => 40.078 , Pd => 106.42 , Lu => 174.9668 , Cm => 247 ,
Sc => 44.955908 , Ag => 107.8682 , Hf => 178.49 , Bk => 247 ,
Ti => 47.867 , Cd => 112.414 , Ta => 180.94788 , Cf => 251 ,
V => 50.9415 , In => 114.818 , W => 183.84 , Es => 252 ,
Cr => 51.9961 , Sn => 118.710 , Re => 186.207 , Fm => 257 ,
Mn => 54.938044 , Sb => 121.760 , Os => 190.23 ,
;
grammar Chemical_formula {
my @ATOMIC_SYMBOLS = %ATOMIC_MASS.keys.sort;
 
rule TOP { ^ (<lparen>|<rparen>|<element>)+ $ }
token quantity { \d+ }
token lparen { '(' }
token rparen { ')' <quantity>? }
token element { $<e>=[@ATOMIC_SYMBOLS] <quantity>? }
}
class Chemical_formula_actions {
has @stack = 0;
method TOP ($/) { $/.make: @stack }
method lparen ($/) { push @stack, 0 }
method rparen ($/) { my $m = @stack.pop;
@stack[*-1] += ($<quantity> // 1) * $m }
method element ($/) { @stack[*-1] += ($<quantity> // 1) * %ATOMIC_MASS{~$<e>} }
}
sub molar_mass ( Str $formula --> Real ) {
Chemical_formula.parse( $formula, :actions(Chemical_formula_actions.new) )
orelse die "Chemical formula not recognized: '$formula'";
return $/.made.[0];
}
say .&molar_mass.fmt('%7.3f '), $_ for <H H2 H2O Na2SO4 C6H12 COOH(C(CH3)2)3CH3>;</lang>
{{Out}}
<pre> 1.008 H
2.016 H2
18.015 H2O
142.036 Na2SO4
84.162 C6H12
186.295 COOH(C(CH3)2)3CH3</pre>
 
=={{header|Phix}}==
Line 2,226 ⟶ 2,167:
Uue: 315
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>my %ATOMIC_MASS =
H => 1.008 , Fe => 55.845 , Te => 127.60 , Ir => 192.217 ,
He => 4.002602 , Co => 58.933194 , I => 126.90447 , Pt => 195.084 ,
Li => 6.94 , Ni => 58.6934 , Xe => 131.293 , Au => 196.966569 ,
Be => 9.0121831 , Cu => 63.546 , Cs => 132.90545196 , Hg => 200.592 ,
B => 10.81 , Zn => 65.38 , Ba => 137.327 , Tl => 204.38 ,
C => 12.011 , Ga => 69.723 , La => 138.90547 , Pb => 207.2 ,
N => 14.007 , Ge => 72.630 , Ce => 140.116 , Bi => 208.98040 ,
O => 15.999 , As => 74.921595 , Pr => 140.90766 , Po => 209 ,
F => 18.998403163 , Se => 78.971 , Nd => 144.242 , At => 210 ,
Ne => 20.1797 , Br => 79.904 , Pm => 145 , Rn => 222 ,
Na => 22.98976928 , Kr => 83.798 , Sm => 150.36 , Fr => 223 ,
Mg => 24.305 , Rb => 85.4678 , Eu => 151.964 , Ra => 226 ,
Al => 26.9815385 , Sr => 87.62 , Gd => 157.25 , Ac => 227 ,
Si => 28.085 , Y => 88.90584 , Tb => 158.92535 , Th => 232.0377 ,
P => 30.973761998 , Zr => 91.224 , Dy => 162.500 , Pa => 231.03588 ,
S => 32.06 , Nb => 92.90637 , Ho => 164.93033 , U => 238.02891 ,
Cl => 35.45 , Mo => 95.95 , Er => 167.259 , Np => 237 ,
Ar => 39.948 , Ru => 101.07 , Tm => 168.93422 , Pu => 244 ,
K => 39.0983 , Rh => 102.90550 , Yb => 173.054 , Am => 243 ,
Ca => 40.078 , Pd => 106.42 , Lu => 174.9668 , Cm => 247 ,
Sc => 44.955908 , Ag => 107.8682 , Hf => 178.49 , Bk => 247 ,
Ti => 47.867 , Cd => 112.414 , Ta => 180.94788 , Cf => 251 ,
V => 50.9415 , In => 114.818 , W => 183.84 , Es => 252 ,
Cr => 51.9961 , Sn => 118.710 , Re => 186.207 , Fm => 257 ,
Mn => 54.938044 , Sb => 121.760 , Os => 190.23 ,
;
grammar Chemical_formula {
my @ATOMIC_SYMBOLS = %ATOMIC_MASS.keys.sort;
 
rule TOP { ^ (<lparen>|<rparen>|<element>)+ $ }
token quantity { \d+ }
token lparen { '(' }
token rparen { ')' <quantity>? }
token element { $<e>=[@ATOMIC_SYMBOLS] <quantity>? }
}
class Chemical_formula_actions {
has @stack = 0;
method TOP ($/) { $/.make: @stack }
method lparen ($/) { push @stack, 0 }
method rparen ($/) { my $m = @stack.pop;
@stack[*-1] += ($<quantity> // 1) * $m }
method element ($/) { @stack[*-1] += ($<quantity> // 1) * %ATOMIC_MASS{~$<e>} }
}
sub molar_mass ( Str $formula --> Real ) {
Chemical_formula.parse( $formula, :actions(Chemical_formula_actions.new) )
orelse die "Chemical formula not recognized: '$formula'";
return $/.made.[0];
}
say .&molar_mass.fmt('%7.3f '), $_ for <H H2 H2O Na2SO4 C6H12 COOH(C(CH3)2)3CH3>;</lang>
{{Out}}
<pre> 1.008 H
2.016 H2
18.015 H2O
142.036 Na2SO4
84.162 C6H12
186.295 COOH(C(CH3)2)3CH3</pre>
 
=={{header|REXX}}==
10,333

edits