Chemical calculator: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 3 users not shown)
Line 1,756:
No assertion errors.
 
<!--
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Chemical_calculator}}
In [http://wiki.formulae.org/Chemical_Calculator this] page you can see the solution of this task.
 
'''Solution'''
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for transportation effects more than visualization and edition.
 
Fōrmulæ has a module for chemistry. Notice that it is not a library, it effectively adds chemical elements as first class citizens to the language, and useful functions that operate with them, for example, to get their atomic masses.
 
There is an expression for a '''homonuclear compound''', a compound made from the union of several atoms of the same element, such as O<sub>2</sub>
 
There is also an expression for a '''heteronuclear compound''', a compound made from the union of several atoms of different elements, such as NaCl
 
[[File: Fōrmulæ - Chemical calculator 01.png]]
 
'''Notes'''
 
* The Tag(Expression) expression retrieves the tag of an expression. For example, when it is called on an homonuclear compound expression, it retrieves the string expression representing the string "Chemistry.HomonuclearCompound"
 
* The |Expression| retrieves the cardinality of the expression, this is, the number of subexpressions it has. If the expression is a heteronuclear compound it gives the number of elements being composed.
 
* If the expression given as parameter is a heteronuclear compound expression, the molar mass is the sum of the molar masses of each component. Note that this function is recursively called.
 
* If the expression given as parameter is a homonuclear compound expression, the molar mass is the product of the number of the group (the second component) and the molar mass of the expression (the first component). Note that this function is recursively called.
 
* Elsewhere, the result is the call of the GetAtomicMass(Expression) with the expression given as parameter.
 
'''Test cases'''
 
[[File: Fōrmulæ - Chemical calculator 02.png]]
 
[[File: Fōrmulæ - Chemical calculator 03.png]]
 
'''Using it symbolically'''
 
Fōrmulæ is a symbolic language. Although chemical elements expressions are intended to be used to create chemical formulae, other expressions can be used, specially symbols, as in the following examples:
 
Example 1. Using a symbol to denote and unspecified number of repetitions in a homonuclear compound expression. For this exercise, n is a free symbol (a symbol with no associated value).
 
[[File: Fōrmulæ - Chemical calculator 04.png]]
 
[[File: Fōrmulæ - Chemical calculator 05.png]]
 
Example 2. Using a symbol to denote an unspecified chemical element. For this exercise, X is a free symbol (a symbol with no associated value).
 
[[File: Fōrmulæ - Chemical calculator 06.png]]
 
[[File: Fōrmulæ - Chemical calculator 07.png]]
 
Example 3. Using symbols to denote an unspecified chemical element and an unspecified number of repetitions. For this exercise, X and n are free symbols (symbols with no associated values).
 
[[File: Fōrmulæ - Chemical calculator 08.png]]
 
[[File: Fōrmulæ - Chemical calculator 09.png]]
 
Example 4. Using symbols to denote different unspecified chemical elements. For this exercise, X, Y and Z are free symbols (symbols with no associated values).
 
[[File: Fōrmulæ - Chemical calculator 10.png]]
 
[[File: Fōrmulæ - Chemical calculator 11.png]]
 
Example 5. Other combinations. For this exercise, X, Y, Z, n and m are free symbols (symbols with no associated values).
 
[[File: Fōrmulæ - Chemical calculator 12.png]]
 
[[File: Fōrmulæ - Chemical calculator 13.png]]
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
-->
=={{header|Go}}==
This doesn't use regular expressions, RPN or eval (which Go doesn't have). It's just a simple molar mass evaluator written from scratch.
Line 3,886 ⟶ 3,943:
 
=={{header|Rust}}==
Rust is precompiled for execution, so itthere hasis notno runtime eval for arbitrary Rust code. The `eval` crate allows Rust to process syntax similar to JSON while executing.
This allows the example to run an `eval` on strings which have been first translated into numeric arithmetic.
<syntaxhighlight lang="rust">use regex::Regex;
Line 4,598 ⟶ 4,655:
{{libheader|Wren-fmt}}
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./str" for Char, Str
 
var atomicMass = {
Line 4,772 ⟶ 4,829:
for (molecule in molecules) {
var mass = evaluate.call(replaceParens.call(molecule))
SystemFmt.print("%(Fmt.s(17, molecule))$17s -> %(Fmt$7.f(73f", massmolecule, 3))"mass)
}</syntaxhighlight>
 
Line 4,789 ⟶ 4,846:
Uue -> 315.000
</pre>
 
=={{header|zkl}}==
Really bad error checking
Line 4,843 ⟶ 4,901:
{ println(cstr," --> ",molarMass(cstr)) }</syntaxhighlight>
{{out}}
Weight of H = 1.008.
Weight of H2 = 2.016.
Weight of H2O = 18.015.
Weight of H2O2 = 34.014.
Weight of (HO)2 = 34.014.
Weight of Na2SO4 = 142.036.
Weight of C6H12 = 84.162.
Weight of COOH(C(CH3)2)3CH3 = 186.295.
Weight of C6H4O2(OH)4 = 176.124.
Weight of C27H46O = 386.664.
Weight of Uue = 315.000.
<pre>
H --> 1.008
Line 4,851 ⟶ 4,920:
COOH(C(CH3)2)3CH3 --> 186.295
</pre>
=={{header|X-script}}==
<syntaxhighlight lang="zkl">
 
<var $weighttab[],
-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|
-K:39.0983|Ar:39.948|Ca:40.078|Sc:44.955908|Ti:47.867|V:50.9415|Cr:51.9961|Mn:54.938044|
-Fe:55.845|Ni:58.6934|Co:58.933194|Cu:63.546|Zn:65.38|Ga:69.723|Ge:72.63|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.9055|Pd:106.42|Ag:107.8682|Cd:112.414|In:114.818|Sn:118.71|Sb:121.76|I:126.90447|Te:127.6|
-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.5|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.9804|Po:209|At:210|Rn:222|Fr:223|Ra:226|Ac:227|
-Pa:231.03588|Th:232.0377|Np:237|U:238.02891|Am:243|Pu:244|Cm:247|Bk:247|Cf:251|Es:252|Fm:257|
-Ubn:299|Uue:315
->
<var $level>
<var $stackTab[]>
 
chemicalCalculator.x
--------------------
 
<def charcode,<htod <stoh $1>>>
<var $name>
<var $multiplier>
<var $unusedCharacters>
 
!"<in <sp 1>,string>
-<set $level,0>
-<set $stackTab[0],0>
-"!
 
(* 1-3 characters in a row plus optional multipier. Examples: "Uee", "NaO", "COO", "Na2" *)
?"<format l><opt <format l>><opt <format l>><opt <integer>>"?
!"
-<set $name,<p 1>>
-<set $multiplier,1>
-<set $unusedCharacters,>
-<ifis <p 2>,
--<if <charcode <p 2>>'>=96,
---(* Lower case char - add to name. *)
---<append $name,<p 2>>
---<ifis <p 3>,
----<if <charcode <p 3>>'>=96,
-----(* Lower case char again - add to name. *)
-----<append $name,<p 3>>
-----,{else}
-----(* Not for this name, put in unread buffer. *)
-----<append $unusedCharacters,<p 3>>
----->
---->
---,{else}
---(* Not for this name, put in unread buffer. *)
---<append $unusedCharacters,<p 2>>
---<append $unusedCharacters,<p 3>>
--->
-->
-(* Multiplier. *)
-<set $multiplier,1>
-<ifis <p 4>,
--<ifis $unusedCharacters,
---(* Multiplier is not for this name, put in unread buffer. *)
---<append $unusedCharacters,<p 4>>
---,{else}
---(* Use as multiplier. *)
---<set $multiplier,<p 4>>
--->
-->
-
-(* Unread unused characters. *)
-<unread $unusedCharacters>
-
-(* Update weight. *)
-<update $stackTab[$level],+$weightTab[$name]*$multiplier,3>
-"!
 
(* Beginning of group. *)
?"("?
!"
-<update $level,+1>
-<set $stackTab[$level],0>
-"!
 
(* End of group. *)
?")<opt <integer>>"?
!"
-<ifis <p 1>,
--<update $stackTab[$level],*<p 1>,3>
-->
-<update $stackTab[<calc $level-1>],+$stackTab[$level],3>
-<update $level,-1>
-"!
 
?"<eof>"?
!"
-<wcons Weight of <sp 1> = $stackTab[$level].>
-<r>
-"!
 
!"<r $stackTab[$level]>"!
--------------
 
<function assert,
-<unless $1=<c chemicalCalculator,$2>,<wcons <c chemicalCalculator,$2>!= $1.>>
->
 
<assert 1.008,H>
<assert 2.016,H2>
<assert 18.015,H2O>
<assert 34.014,H2O2>
<assert 34.014,(HO)2>
<assert 142.036,Na2SO4>
<assert 84.162,C6H12>
<assert 186.295,COOH(C(CH3)2)3CH3>
<assert 176.124,C6H4O2(OH)4>
<assert 386.664,C27H46O>
<assert 315 ,Uue>
</syntaxhighlight>
 
{{out}}
Weight of H = 1.008.<br>
Weight of H2 = 2.016.<br>
Weight of H2O = 18.015.<br>
Weight of H2O2 = 34.014.<br>
Weight of (HO)2 = 34.014.<br>
Weight of Na2SO4 = 142.036.<br>
Weight of C6H12 = 84.162.<br>
Weight of COOH(C(CH3)2)3CH3 = 186.295.<br>
Weight of C6H4O2(OH)4 = 176.124.<br>
Weight of C27H46O = 386.664.<br>
Weight of Uue = 315.000.
9,476

edits