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();
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
Muhammad Imran AnsariPosted Oct 2, 2024, 7:26 PM
Hi Luv,
Here is the updated version of code which handled all your scenarios which shared in last comments and in the attached input file. Now, calculation is working fine with single and double digit.
Luv LPosted Oct 2, 2024, 6:33 AM
Dear @Muhammad Imran Ansari
Thanks for kind help.but single digit is work well.if i change to double digit like 12 then it will caluculated with second digit 2 only not 12.this is the main issue with our code.
i would like to request you to i change my logic to like below.pls help with code.
if i entered input then it will copy to another textbox like below given.
For example-1
TEXTBOX-1
[8-A
8-B]2 =====> (TIMING RANGE BETWEEN 1-999) OR WITHOUT BRACKET
4-C
-----
36
------
ABOVE TEXTBOX INPUT CHANGE TO TEXTBOX-2 LIKE BELOW
TEXTBOX-2
8-A
8-B
8-A
8-B
4-C
-----
36
----
then our problem is solved.
hope you understand my logic.
Thankyou.
Muhammad Imran AnsariPosted Oct 1, 2024, 8:58 PM
Hi Luv,
You bracket/exception issue is fixed and here is the complete code. however, there is some calculation issue with 2 digits, that part check because you know the actual logic better than me. :)
Thank you.
Luv LPosted Oct 1, 2024, 5:27 PM
Dear @Muhammad Imran Ansari
Please find theupload sample file
Thanks
Muhammad Imran AnsariPosted Oct 1, 2024, 3:43 PM
Hi Luv,
As per my last comments "Can you share the sample input where it's working and where not? so that I can suggest a solution."
Share the sample Inputs
Thank You!
Luv LPosted Oct 1, 2024, 3:15 PM
Thankyou @Muhammad Imran Ansari
isame proble sir its not working.only without bracket is get correct result after said changes.but
if i change to single digit error will come System.InvalidOperationException: 'Stack empty.'
and if i change to double digit result will be 0.
Help with correct code and share full revised code.
Thanks for your kind support
Muhammad Imran AnsariPosted Sep 30, 2024, 5:36 PM
Hi Luv,
Can you share the sample input where its working and where not? so that I can suggest a solution.
Luv LPosted Sep 30, 2024, 1:14 PM
Thankyou @Muhammad Imran Ansari
Iam tried revised changes code but same above System.InvalidOperationException: 'Stack empty.' problem is comming while iam using single digit timing and double digit timing.without backet and without timing is working perfectly.
so please check and send the revised code for moving my application.
Thankyou
Muhammad Imran AnsariPosted Sep 30, 2024, 10:35 AM
Sure, here is final code after changes.
Luv LPosted Sep 30, 2024, 3:55 AM
Thankyou @Muhammad Imran Ansari
if possible make the correction and send the full code for better idea.
Thankyou.
Muhammad Imran AnsariPosted Sep 29, 2024, 5:27 PM
Hi Luv,
The issue in your code occurs when you retrieve the bracket in the following line (in both If and Else clause):
This works fine for single-digit numbers, but when you enter a two-digit number, it doesn't return the bracket because temp.Length is 6, and subtracting 2 gives the value at index 4, which is '1'. As a result, your do-while loop runs indefinitely since no bracket is found. To resolve this, you need to add a condition to check the length and ensure you're retrieving the correct bracket.
Thank you.