Esempio n. 1
0
        static Dictionary <string, int> FullElements(MoleculesList list)
        {
            Dictionary <string, int> result = new Dictionary <string, int>();

            foreach (Dictionary <string, int> elem in list)
            {
                foreach (var atom in elem.Keys)
                {
                    if (result.ContainsKey(atom))
                    {
                        result[atom] += elem[atom];
                    }
                    else
                    {
                        result.Add(atom, elem[atom]);
                    }
                }
            }
            return(result);
        }
Esempio n. 2
0
        static MoleculesList GetElements(string[] molecules)
        {
            MoleculesList result    = new MoleculesList(molecules.Length);
            int           molNumber = 0;
            int           atomNumber;
            int           bracketsNumber = 0;

            ;
            foreach (var mol in molecules)
            {
                atomNumber = 0;
                for (int i = 0; i < mol.Length; i++)
                {
                    if (mol[i] == '(')
                    {
                        bracketsNumber = atomNumber;
                        continue;
                    }
                    if (mol[i] == ')')
                    {
                        i++;
                        if (++i < mol.Length && mol[i] >= '0' && mol[i] <= '9')
                        {
                            result.AddBrackets(molNumber, bracketsNumber, atomNumber, int.Parse(mol.Substring(i - 1, 2)));
                        }
                        else
                        {
                            result.AddBrackets(molNumber, bracketsNumber, atomNumber, int.Parse(mol[--i].ToString()));
                        }
                        continue;
                    }
                    if (mol[i] >= 'A' && mol[i] <= 'Z')
                    {
                        string element = new string(mol[i], 1);
                        i++;
                        if (i < mol.Length && mol[i] >= 'a' && mol[i] <= 'z')
                        {
                            element += mol[i++];
                        }
                        if (i < mol.Length && mol[i] > '0' && mol[i] <= '9')
                        {
                            if (++i < mol.Length && mol[i] >= '0' && mol[i] <= '9')
                            {
                                result.Add(molNumber, element, int.Parse(mol.Substring(i - 1, 2)));
                            }
                            else
                            {
                                result.Add(molNumber, element, int.Parse(mol[--i].ToString()));
                            }
                        }
                        else if (i >= mol.Length || (mol[i] >= 'A' && mol[i] <= 'Z') || mol[i] == '(' || mol[i] == ')')
                        {
                            result.Add(molNumber, element, 1);
                            i--;
                        }
                        else
                        {
                            return(null);
                        }
                        atomNumber++;
                    }
                }
                molNumber++;
            }
            return(result);
        }