Luv L

Luv L

  • 1.6k
  • 84
  • 5.6k

C# textbox sum with ASCII with multiple timing Winform

Sep 26 2024 5:28 AM

Dear All,

I am Trying the code below.

input in textbox

[12-A

12-B]3 =======> IF I WANT TO CHANGE TWO DIGITS OR THREE DIGITS IN PLACE OF 3 (ONE DIGIT ) THEN NOT GET                               CORRECT SUM.

---------

72

Below is my code

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

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

            foreach (string line in txtWpPattern.Lines)
            {
                string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces
                if (temp == "") continue; // ignore blank line
                bool containsLetter = (line.IndexOf(letter) > -1);
                char first = temp[0];
                char 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;
        }
        
        txtWpResult.Text = String.Format(combinedSum + " = E/R".ToString());
        label1.Text = String.Format(combinedSum.ToString());
       
    }
    catch (Exception ex)
    {
        MessageBox.Show("Please Enter Correct Inpit Strng Like 2-A");
    }
}

Please help i want correct sum of textbox input methord wherre i can change multi timing  with two/three digits.like between 10-999

Varta


Answers (1)