Skip to content

Latest commit

 

History

History
194 lines (153 loc) · 5.64 KB

readme.md

File metadata and controls

194 lines (153 loc) · 5.64 KB

Molecular Mass Weight Calculator

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.

Features

  • Calculate the molecular weight of chemical compounds.
  • Support for standard atomic weights of elements.
  • Simple and intuitive DLL for integration into your projects.

Installation

You can install the Molecular Mass Calculator library via NuGet Package Manager Console:

NuGet\Install-Package MolecularWeightCalculator

Usage

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);




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));
}

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
**************************************************

ChangeLog

1.0.2

  1. fix: change ComputeMass method return type from double to object

1.0.3

  1. change Periodic Table from https://iupac.org/what-we-do/periodic-table-of-elements/

1.0.4

  1. Add filtering to only calculate the molecular weight of compounds with certain chemical elements, such as only contain C (Carbon)

1.0.5

  1. fix Provide analytical expressions and obtain parameter information issue
  2. Add MolecularWeightCalculator.Tests.csproj

Checkmarx Report

Checkmarx Report:None High, medium and low risk

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.

License

License: MIT