-6

I wanted to make a small program, where i essentially have a lot of doubles for the atom mass for each atom. I want to be able to write a molecule formula into a textbox, whereafter the program should be able to calculate the molar mass, however, i dont know how i can cut the string from the textbox, so that for instance i can get from inserting "NaCl" into the textbox, the value of my Na double plus the value of my Cl double.

namespace WindowsFormsApplication33
{
    public partial class Form1 : Form
    {
        double H = 1.00794;
        double He = 4.002602;
        double Li = 6.941;
        double Be = 9.012182;
        ...

These are just all my doubles, now what i want a button to do:

        private void button1_Click(object sender, EventArgs e)
        {
            //take the different atoms in the molecule formula from a textbox,
            //get the value of all those doubles, and add them all together to get
            //a final value, for instance: NaCl = Na + Cl = 22.98976928 + 35.453 = 58.44276928
        }

Also, i want to be able to write H2SO4, which essentially is H*2 + S + O*4, how would i go about doing that?

Thank you in advance

  • 3
    Show us what you tried. – Gusman Feb 08 '16 at 11:09
  • Possible duplicate of [Regular expression, split string by capital letter but ignore TLA](http://stackoverflow.com/questions/1097901/regular-expression-split-string-by-capital-letter-but-ignore-tla) – Aht Feb 08 '16 at 11:11
  • 1
    You have our blessing to go ahead and make this program. if you get stuck, feel free to ask a question about a specific problem complete with what you have tried and researched. – Sayse Feb 08 '16 at 11:14
  • Im sorry, im not the best at explaining, i often end up using a lot of idiosyncratic language, i hope this edit helps a bit understanding my problem, i would like two seperate strings, so that i can extract a value from the double with a name equivalent to the string text. – iBoughtWinrar Feb 08 '16 at 11:50
  • 1
    Try return Regex.Split(source, @"(?<!^)(?=[A-Z])"); – Vinay Pandey Feb 08 '16 at 12:38

2 Answers2

1
Dictionary<string, double> Chemicals = new Dictionary<string, double>() { { "H", 1.00794 }, { "He", 4.002602 }, { "Li", 6.941 }, { "Be", 9.012182 } };
        List<string> Properties = new List<string>();

        Regex reg = new Regex("[A-Z]{1}[a-z0-9]*");
        Properties = reg.Matches(txtInput.Text).Cast<Match>().Select(m => m.Value).ToList();

        double Total = 0;
        foreach (var Property in Properties)
        {
            var result = Regex.Match(Property, @"\d+$").Value;

            int resultAsInt;
            int.TryParse(result, out resultAsInt);

            if (resultAsInt > 0)
            {
                Total += Chemicals[Property.Substring(0, Property.Length - result.Length)] * resultAsInt;
            }
            else
            {
                Total += Chemicals[Property];
            }

        }

        lblOutput.Text = "Total: " + Total.ToString();
Murray Hart
  • 181
  • 1
  • 13
  • Okay, i used this to do what i wanted, is there any way you can now help me calculate something like H2SO4, which essentially is H*2 + S + O*4? – iBoughtWinrar Feb 08 '16 at 15:13
  • I have edited my post to match what you have asked for above. Hope this helps you. Using a dictionary is much better when using key pair value data such as chemical values – Murray Hart Feb 09 '16 at 10:06
  • Thank you so much, you just pretty much made my entire program for me, the only thing i would change is making a try catch for the else part in the if statement, because if someone puts in for instance H2SO0, then it really is just H*2 + S + O*0, which will make en error. – iBoughtWinrar Feb 09 '16 at 12:01
  • Yeah i wasn't going to do the validation for you as well. Take care and don't forget to mark as complete. – Murray Hart Feb 09 '16 at 12:06
0
void Main()
{
    string text = "NiNaCiKi";
    Regex reg = new Regex("[A-Z]{1}[a-z]*");
    var props = reg.Matches(text).Cast<Match>().Select(m=>m.Value).ToList();

    Chem ch = new Chem();

    var sum = typeof(Chem)
    .GetProperties()
    .Where(p=>props.Contains(p.Name))
    .Cast<PropertyInfo>()
    .Select(val=> (double)val.GetValue(ch)).Sum();

    Console.WriteLine(sum);
}

public class Chem
{
    public double Na {get {return 4;}}
    public double N {get {return 2;}}
    public double Ci {get {return 1;}}
}
Helic
  • 907
  • 1
  • 10
  • 25
  • Thank you, very nice, i actually ended up putting all my doubles into a dictionary as suggest by Falling, but its pretty much the same code, could you help with what i asked him about? – iBoughtWinrar Feb 08 '16 at 15:16