This is a C# library for calculating the molecular mass of chemical compounds. It allows you to input a chemical formula and computes the total molecular weight by summing up the atomic weights of the constituent elements.
$ dotnet add package MolecularWeightCalculatorThis is a C# library for calculating the molecular mass of chemical compounds. It allows you to input a chemical formula and computes the total molecular weight by summing up the atomic weights of the constituent elements.
You can install the Molecular Mass Calculator library via NuGet Package Manager Console:
NuGet\Install-Package MolecularWeightCalculator
Here's how you can use the library to calculate the molecular weight of a chemical compound:
var molecularMath = new MolecularMath();
string exp1 = "CO2";
DisplayExpressionInfo(exp1);
string exp2 = "CaCO3";
DisplayExpressionInfo(exp2);
string exp3 = "CO2 / CaCO3";
DisplayExpressionInfo(exp3);
string exp4 = "C2H2 +2.5 * O2";
DisplayExpressionInfo(exp4);
string exp5 = "2*CO2/C2H2";
DisplayExpressionInfo(exp5);
string exp6 = "1*2*3*4";
DisplayExpressionInfo(exp6);
string exp7 = "CaCO3 * A2 + B3";
DisplayExpressionInfo(exp7);
//KeyNotFoundException: 'A' was not present in the Periodic Table
//calculate only contain C (Carbon)
Console.WriteLine($"===== calculate only contain C (Carbon) =====");
string filterMoleculars = "C";
string exp8 = "CaO + CO2";
DisplayExpressionInfo(exp8, filterMoleculars);
string exp9 = "C2H2 +2.5*O2";
DisplayExpressionInfo(exp9, filterMoleculars);
string expA = "2*CO2 + H2O";
DisplayExpressionInfo(expA, filterMoleculars);
// Date function test
Console.WriteLine($"===== Date function test =====");
string expB = "Days('2023-01-01', '2023/01/10') + 1";
DisplayExpressionInfo2(expB);
string expC = "Days('2023-01-01 10:10:10', '2023/01/10') + 1";
DisplayExpressionInfo2(expC);
string expD = "Pow(Days('2023-01-01', '2023/01/10'),2)";
DisplayExpressionInfo2(expD);
string expE = "Pow(Days('2023-01-10', '2023/01/4') + 1,3)";
DisplayExpressionInfo2(expE);
Console.WriteLine($"===== 民國年 DaysTW function test =====");
//民國年
string expF = "DaysTW('111/12/31', '111/1/1') + 1";
DisplayExpressionInfo2(expF);
string expG = "DaysTW('111/12/31', '111-1-1') + 1";
DisplayExpressionInfo2(expG);
Console.WriteLine($"Press any key to exit.....");
Console.ReadKey();
void DisplayExpressionInfo(string expression, string filterMoleculars="")
{
Console.WriteLine($"==={expression}, filter Moleculars:({filterMoleculars}){new String('=', 10)}");
string[] filterMolecularsArray = filterMoleculars.Split(',', StringSplitOptions.RemoveEmptyEntries);
try
{
var parameters = molecularMath.GetParameters(expression);
Console.WriteLine($"==={expression}:Parameters({parameters.Count}),{new String('=', 10)}");
foreach (var parameter in parameters)
{
Console.WriteLine(parameter);
}
Console.WriteLine(new String('=', 30));
var result = molecularMath.ComputeMass(expression, filterMolecularsArray);
Console.WriteLine($"{expression}=>{result}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine(new String('*', 50));
}
void DisplayExpressionInfo2(string expression)
{
Console.WriteLine($"==={expression}{new String('=', 10)}");
try
{
if (molecularMath == null)
{
Console.WriteLine("Error: molecularMath service is not available.");
return;
}
var parameters = molecularMath.GetParameters(expression);
Console.WriteLine($"==={expression}:Parameters({parameters.Count}),{new String('=', 10)}");
foreach (var parameter in parameters)
{
Console.WriteLine(parameter);
}
Console.WriteLine(new String('=', 30));
var result = molecularMath.Evaluate(expression);
Console.WriteLine($"{expression}=>{result}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine(new String('*', 50));
}
Execute Result:
===CO2, filter Moleculars:()==========
===CO2:Parameters(1),==========
CO2
==============================
CO2=>44.009
**************************************************
===CaCO3, filter Moleculars:()==========
===CaCO3:Parameters(1),==========
CaCO3
==============================
CaCO3=>100.086
**************************************************
===CO2 / CaCO3, filter Moleculars:()==========
===CO2 / CaCO3:Parameters(2),==========
CO2
CaCO3
==============================
CO2 / CaCO3=>0.43971184781088263
**************************************************
===C2H2 +2.5 * O2, filter Moleculars:()==========
===C2H2 +2.5 * O2:Parameters(2),==========
C2H2
O2
==============================
C2H2 +2.5 * O2=>106.033
**************************************************
===2*CO2/C2H2, filter Moleculars:()==========
===2*CO2/C2H2:Parameters(2),==========
CO2
C2H2
==============================
2*CO2/C2H2=>3.3803671556955224
**************************************************
===1*2*3*4, filter Moleculars:()==========
===1*2*3*4:Parameters(0),==========
==============================
1*2*3*4=>24
**************************************************
===CaCO3 * A2 + B3, filter Moleculars:()==========
===CaCO3 * A2 + B3:Parameters(3),==========
CaCO3
A2
B3
==============================
System.Collections.Generic.KeyNotFoundException: 'A' was not present in the Periodic Table
**************************************************
===== calculate only contain C (Carbon) =====
===CaO + CO2, filter Moleculars:(C)==========
===CaO + CO2:Parameters(2),==========
CaO
CO2
==============================
CaO + CO2=>44.009
**************************************************
===C2H2 +2.5*O2, filter Moleculars:(C)==========
===C2H2 +2.5*O2:Parameters(2),==========
C2H2
O2
==============================
C2H2 +2.5*O2=>26.037999999999997
**************************************************
===2*CO2 + H2O, filter Moleculars:(C)==========
===2*CO2 + H2O:Parameters(2),==========
CO2
H2O
==============================
2*CO2 + H2O=>88.018
**************************************************
===== Date function test =====
===Days('2023-01-01', '2023/01/10') + 1==========
===Days('2023-01-01', '2023/01/10') + 1:Parameters(0),==========
==============================
Days('2023-01-01', '2023/01/10') + 1=>-8
**************************************************
===Days('2023-01-01 10:10:10', '2023/01/10') + 1==========
===Days('2023-01-01 10:10:10', '2023/01/10') + 1:Parameters(0),==========
==============================
Days('2023-01-01 10:10:10', '2023/01/10') + 1=>-7.5762731481481485
**************************************************
===Pow(Days('2023-01-01', '2023/01/10'),2)==========
===Pow(Days('2023-01-01', '2023/01/10'),2):Parameters(0),==========
==============================
Pow(Days('2023-01-01', '2023/01/10'),2)=>81
**************************************************
===Pow(Days('2023-01-10', '2023/01/4') + 1,3)==========
===Pow(Days('2023-01-10', '2023/01/4') + 1,3):Parameters(0),==========
==============================
Pow(Days('2023-01-10', '2023/01/4') + 1,3)=>343
**************************************************
===== 民國年 DaysTW function test =====
===DaysTW('111/12/31', '111/1/1') + 1==========
===DaysTW('111/12/31', '111/1/1') + 1:Parameters(0),==========
==============================
DaysTW('111/12/31', '111/1/1') + 1=>365
**************************************************
===DaysTW('111/12/31', '111-1-1') + 1==========
===DaysTW('111/12/31', '111-1-1') + 1:Parameters(0),==========
==============================
DaysTW('111/12/31', '111-1-1') + 1=>365
**************************************************
String literals must be enclosed in single quotes.
Checkmarx Report:None High, medium and low risk
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.