We help IT Professionals succeed at work.

sum values in a textbox C#

jnsimex
jnsimex asked
on
C# Visual Studio 2005

I have a textbox that a person enters the packaging details in i.e. 1 x 500, 2 x 50

I need to sum the total of the text entered into the textbox, which in this case would be 600.

how should I approach this problem?
Comment
Watch Question

CERTIFIED EXPERT

Commented:
is comma delimited like you show in your question or is that to denote two textboxes?
CERTIFIED EXPERT

Commented:
also will you need to parse the math statement 1x500 or is there a quantity box that you multiply against another number

Author

Commented:
hi pdavis, it is comma delimited.

As for your second question, i will need to parse 1x500 = 500 and then add it to the second math statement 2x50 = 100 for a total sum of 600.
CERTIFIED EXPERT

Commented:
will there be more than one operation per comma?

like

1 + 5 x 500, 2 x 50


or

1 x 500 x 2, 3 x 10

etc...
CERTIFIED EXPERT
Commented:
this isn't thoroughly tested, i just threw it together as one way you could parse -- also if the statements can be more complicated you will have to adjust-- but this could get you started in the right direction if that is the case.
public partial class Form1 : Form
    {
        double sum = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {    
                                                                         // or * if you prever... need to have a standard for the user or else compensate.
            if (!string.IsNullOrEmpty(textBox1.Text) && (textBox1.Text.ToLower().Contains('x') || textBox1.Text.Contains('-') || textBox1.Text.Contains('/') || textBox1.Text.Contains('+')))
            {
                sum = 0;

                sum += ParseStatement(textBox1.Text);

                textBox2.Text = sum.ToString();
            }
        }

        double ParseStatement(String str)
        {
            double retVal = 0;
            string[] equations = str.Split(',');

            for (int i = 0; i < equations.Length; i++)
            {
                if (textBox1.Text.Contains('x'))
                {
                    retVal += Multiply(equations[i]);
                }
                else if (textBox1.Text.Contains('-'))
                {
                    retVal += Subtract(equations[i]);
                }
                else if (textBox1.Text.Contains('/'))
                {
                    retVal += divide(equations[i]);
                }
                else if (textBox1.Text.Contains('+'))
                {
                    retVal += Add(equations[i]);
                }
            }

            return retVal;
        }

        double Multiply(string str)
        {
            double retVal = 0;

            string[] mult = str.Split('x');

            //if only 2 numbers to process

            retVal += Convert.ToDouble(mult[0]) * Convert.ToDouble(mult[1]);

            return retVal;
        }

        double divide(string str)
        {
            double retVal = 0;
            string[] mult = str.Split('/');

            //if only 2 numbers to process

            retVal += Convert.ToDouble(mult[0]) / Convert.ToDouble(mult[1]);
            return retVal;
        }

        double Add(string str)
        {
            double retVal = 0;
            string[] mult = str.Split('+');

            //if only 2 numbers to process

            retVal += Convert.ToDouble(mult[0]) + Convert.ToDouble(mult[1]);
            return retVal;
        }

        double Subtract(string str)
        {
            double retVal = 0;
            string[] mult = str.Split('-');

            //if only 2 numbers to process

            retVal += Convert.ToDouble(mult[0]) - Convert.ToDouble(mult[1]);
            return retVal;
        }


    }
}

Open in new window

Author

Commented:
Only one operation per comma

Thanks for providing the code snippet. I will test later on today and let you know the results.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Here's another one to play with:
private void button1_Click(object sender, EventArgs e)
        {
            int sum = ComputeSum(textBox1.Text);
            label1.Text = sum.ToString();
        }

        private int ComputeSum(string input)
        {
            int sum = 0;
            string[] values;
            int value1, value2;
            string[] equations = input.ToLower().Trim().Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (string equation in equations)
            {
                values = equation.Trim().Split("x".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (values.Length == 2)
                {
                    if (int.TryParse(values[0].Trim(), out value1) && int.TryParse(values[1].Trim(), out value2))
                    {
                        sum = sum + (value1 * value2);
                    }
                    else
                    {
                        // how do you want to handle this?
                        return -1;
                    }
                }
                else
                {
                    // how do you want to handle this?
                    return -1;
                }
            }
            return sum;
        }

Open in new window

Author

Commented:
Thank you both for the coding.

They both worked equally well.