public static string CalculateM(string formula)
        {
            double M = 0;

            try
            {
                //Load the formula into ElementGroup object
                ElementGroup molecule = new ElementGroup(formula, 1, true);

                //Disassemble the formula in the ElementGroup object to single elements
                Dictionary <string, int> elementsTable = molecule.CountElements();

                //Add the molar masses of all the atoms together
                M = AddMolarMasses(elementsTable);
            }
            catch (InvalidInputException e)
            {
                return(e.Message + "\nPlease, check the Help menu for rules about writing formulas.");
            }
            catch (Exception e)
            {
                return("An unexpected exception occured.\nThis shouldn't be happening.\nCan you please submit a bug report via the Feedback menu (accessible via the help button).\nPlease, don't forget to include the formula you entered in your report.\n\nException details: " + e.ToString());
                //return "An exception occured.\nThis is most likely caused by invalid input.\nPlease, check the Help menu for rules about writing formulas and if this problem persist, submit a bug report via the Feedback menu.\nPlease, include the following informationg in your report. Thanks.\n\nAn exception occured while calculating molar mass of " + formula + ": " + e.ToString();
            }

            return("Molar mass of " + formula + ": " + M + " g/mol");
        }
예제 #2
0
        /**
         * Method taking a whole formula, starting index and characters terminating end of element abbreviation or it's count
         * Returns the first loaded element group as object and index, where in the formula it ended as out parameter
         */
        private ElementGroup ReadElementGroup(string content, int startIndex, string contentTerminatingChars, string countTerminatingChars, out int endIndex)
        {
            int    index         = startIndex;
            int    bracketsLevel = 0;
            string resultContent = "";
            string resultCount   = "";

            do
            {
                resultContent += content[index];
                if (content[index] == '(')
                {
                    bracketsLevel++;
                }
                if (content[index] == ')')
                {
                    bracketsLevel--;
                }
                index++;
            } while (index < content.Length && (!contentTerminatingChars.Contains(content[index].ToString()) || (bracketsLevel > 0)));

            //Checking for balanced brackets
            if (bracketsLevel != 0)
            {
                throw new InvalidInputException("Unbalanced brackets.");
            }

            while (index < content.Length && (!countTerminatingChars.Contains(content[index].ToString())))
            {
                resultCount += content[index];
                index++;
            }
            if (resultCount.Length == 0)
            {
                resultCount = "1";
            }
            try
            {
                int resultCountInt = Convert.ToInt32(resultCount);
            }
            catch (FormatException e)
            {
                throw new InvalidInputException("Count of element or element group " + resultContent + " contains a non-number character.");
            }

            bool containsGroup = false;

            if (resultContent.Contains("("))
            {
                //Resulting object contains element group, not a single element
                containsGroup = true;
            }
            endIndex = index;
            ElementGroup result = new ElementGroup(resultContent, Convert.ToInt32(resultCount), containsGroup);

            return(result);
        }