Luv L

Luv L

  • 1.6k
  • 169
  • 8.4k

Textbook sum with multi timing

Sep 29 2024 4:21 PM

Hi

I am trying below code in textbox input values like below

8-B
12-A
[20-C
4-B]5

-------

Sum = 140

----------

if i want to change to 15 or 25 or whatever like between 11-99 in place of 5 then error will come like below image.

Note: if i changeTimng  between 1 - 9 then is working perfectly.the problem is after between change 10-99 then error is comming.

below is my code.

private void button1_Click(object sender, EventArgs e)
{
    int combinedSum = 0;

    for (char letter = 'A'; letter <= 'D'; letter++)
    {
        int sum = 0;
        var stack = new Stack<string>();
        int stackSum = 0;

        foreach (string line in textBox1.Lines)
        {
            string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces
            if (temp.Count() == 0) continue; // ignore blank line
            bool containsLetter = (line.IndexOf(letter) > -1);
            char first = temp.First();//temp[0];
            char last = temp.Last();//temp[temp.Length - 1];

            if (Char.IsDigit(first))
            {
                string numStr = temp.Split('-')[0];
                if (!containsLetter) numStr = "0";

                if (stack.Count == 0)
                {
                    sum += int.Parse(numStr);
                }
                else
                {
                    stack.Push(numStr);
                }

                if (Char.IsDigit(last))
                {
                    int multiplier = last - 48;
                    char bracket = temp[temp.Length - 2];

                    int total = 0;
                    char openChar = '\0';
                    string tempStr;
                    if (bracket == '}')
                        openChar = '{';
                    else if (bracket == ']')
                        openChar = '[';
                    else if (bracket == ')')
                        openChar = '(';
                    else if (bracket == '>')
                        openChar = '<';
                    do
                    {
                        tempStr = stack.Pop();
                        int num;
                        if (int.TryParse(tempStr, out num)) total += num;
                    }
                    while (tempStr[0] != openChar);

                    stackSum = (stackSum + total) * multiplier;

                    if (stack.Count == 0)
                    {
                        sum += stackSum;
                        stackSum = 0;
                    }
                }
            }
            else
            {
                stack.Push(first.ToString());
                int index = 1;

                while (true)
                {
                    if (Char.IsDigit(temp[index])) break;
                    stack.Push(temp[index].ToString());
                    index++;
                }

                string numStr2 = temp.Substring(index).Split('-')[0];
                if (!containsLetter) numStr2 = "0";
                stack.Push(numStr2);

                if (Char.IsDigit(last))
                {
                    int multiplier = last - 48;
                    char bracket = temp[temp.Length - 2];
                    int total = 0;
                    char openChar = '\0';
                    string tempStr;

                    if (bracket == '}')
                        openChar = '{';
                    else if (bracket == ']')
                        openChar = '[';
                    else if (bracket == ')')
                        openChar = '(';
                    else if (bracket == '>')
                        openChar = '<';

                    do
                    {
                        tempStr = stack.Pop();
                        int num;
                        if (int.TryParse(tempStr, out num)) total += num;
                    }
                    while (tempStr[0] != openChar);

                    stackSum += (stackSum + total) * multiplier;

                    if (stack.Count == 0)
                    {
                        sum += stackSum;
                        stackSum = 0;
                    }
                }
            }
        }
        combinedSum += sum;
    }

    textBox6.Text = String.Format("Sum = {0}", combinedSum);
}

Please correct above code and resolve this problem.

Thanks in Advance


Answers (11)