Пример #1
0
        /// <summary>
        /// Interprets a properly formatted string to create a chemical equation.
        /// </summary>
        /// <param name="equation">The equation to interpret.</param>
        /// <returns>A chemical equation.</returns>
        private void InterpretEquation(string equation)
        {
            var chemical_equation = new ChemicalEquation();

            var molecules       = Regex.Matches(equation, @"([^+>\-\s]{1,}){1,}");
            var arrow_separator = Regex.Matches(equation, @"[-]{2}[>]{1}");
            var elements        = Regex.Matches(equation, @"[A-Z]{1}[a-z]{0,2}");
            var subscripts      = Regex.Matches(equation, @"\b\d{1,}\b");

            if (elements.Count == subscripts.Count)
            {
                if (arrow_separator.Count == 0) // Only reactants present
                {
                    var reactants = new List <Tuple <Molecule, int> >();

                    foreach (Match molecule in molecules)
                    {
                        reactants.Add(new Tuple <Molecule, int>(CreateMolecule(molecule.Value), 1));
                    }

                    Reactants = reactants;
                }
                else if (arrow_separator.Count == 1) // Reactants and produts present
                {
                    var reactants = new List <Tuple <Molecule, int> >();
                    var products  = new List <Tuple <Molecule, int> >();

                    for (var i = 0; i < molecules.Count; ++i)
                    {
                        // Reactants
                        if (molecules[i].Index < arrow_separator[0].Index)
                        {
                            reactants.Add(Tuple.Create(CreateMolecule(molecules[i].Value), 1));
                        }
                        else // Products
                        {
                            products.Add(Tuple.Create(CreateMolecule(molecules[i].Value), 1));
                        }
                    }

                    Reactants = reactants;
                    Products  = products;
                }
            }
        }
Пример #2
0
        private List <Tuple <Tuple <Molecule, int>, string> > CreateConversionInfo(ChemicalEquation equation)
        {
            List <Tuple <Tuple <Molecule, int>, string> > header = null;

            if (equation.IsBalanced())
            {
                header = new List <Tuple <Tuple <Molecule, int>, string> >();

                var combined = equation.Reactants.Concat(equation.Products);

                foreach (var molecule in combined)
                {
                    header.Add(Tuple.Create(molecule, "mass"));
                    header.Add(Tuple.Create(molecule, "moles"));
                    header.Add(Tuple.Create(molecule, "particles"));
                }
            }

            return(header);
        }
Пример #3
0
        public void CreateConversionTable(ChemicalEquation equation)
        {
            var tableData = new List <List <string> >();

            if (equation.IsBalanced())
            {
                var conversionInfo = CreateConversionInfo(equation);
                var columnHeader   = conversionInfo.Select(molecule => $"{molecule.Item1.Item2}{molecule.Item1.Item1.ToString()} {molecule.Item2}").ToList();
                tableData.Add(columnHeader);

                // Adds the conversion type of the table.
                tableData[0].Insert(0, equation.ToString());

                for (var rowIndex = 1; rowIndex < columnHeader.Count; ++rowIndex)
                {
                    var rowHeader = columnHeader[rowIndex];

                    if (tableData.Count == rowIndex)
                    {
                        tableData.Add(new List <string>());
                        tableData[rowIndex].Add(rowHeader);
                        tableData[rowIndex].AddRange(new string[rowIndex - 1]);
                    }

                    for (var colIndex = rowIndex; colIndex < columnHeader.Count; ++colIndex)
                    {
                        var canceledUnit     = conversionInfo[rowIndex - 1];
                        var remainingUnit    = conversionInfo[colIndex - 1];
                        var conversionScalar = CreateConversionScalar(canceledUnit, remainingUnit);

                        tableData[rowIndex].Add(conversionScalar.ToString());
                    }
                }

                UnitConverter.ConversionTable = UnitConverter.CreateConversionTable(tableData).Transpose();
            }
        }